Skip to content

Instantly share code, notes, and snippets.

@LightGuard
Created October 28, 2011 00:09
Show Gist options
  • Save LightGuard/1321262 to your computer and use it in GitHub Desktop.
Save LightGuard/1321262 to your computer and use it in GitHub Desktop.
seam security create group and add user to group
@Inject
private IdentitySession identitySession;
@Inject
private Identity identity;
@Inject
private UserTransaction tx;
@Before
public void setupTestUser() throws IdentityException, FeatureNotSupportedException, SystemException, NotSupportedException, RollbackException, HeuristicRollbackException, HeuristicMixedException {
final PersistenceManager pm = identitySession.getPersistenceManager();
final AttributesManager am = identitySession.getAttributesManager();
final RelationshipManager rm = identitySession.getRelationshipManager();
// Group creation must be done in a separate transaction so it's flushed to the DB before adding members to that group
tx.begin();
// Setup the group we want our user to belong to
final Group memberGroup = pm.createGroup("member", "USER");
final User user = pm.createUser("test");
am.updatePassword(user, "password");
tx.commit();
// Now we can go ahead and create the association
tx.begin();
rm.associateUser(memberGroup, user);
tx.commit();
}
@Test
public void assertUserCanAuthenticate(Credentials credentials) {
credentials.setUsername("test");
credentials.setCredential(new PasswordCredential("password"));
assertThat(identity.login(), is(Identity.RESPONSE_LOGIN_SUCCESS));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment