IdentityManager.grantRole() not persisting to database
nbhatia.bhatian.comcast.net Dec 29, 2008 3:44 AMI am trying to implement users and roles in my application (similar to SeamSpace). I am able to create users, but I am not able to grant them any roles. I traced through IdentityManager.grantRole() code and it looks like I am hitting JpaIdentityStore.grantRole(String username, String role). In this method, the correct role is added to the user and mergeEntity(user) is called (as expected), but I don't see any insert to the database in my log file! See the log below:
[myapp.OpenAccountController] >>> Entering CreateUserService.createUser() [myapp.CreateUserService] >>> Entering IdentityManager.createUser() [org.hibernate.SQL] select ... from User user0_ where user0_.username=? [org.hibernate.SQL] insert into User (version, enabled, username, passwordHash, person_id) values (?, ?, ?, ?, ?) [myapp.CreateUserService] <<< Exiting IdentityManager.createUser() [myapp.CreateUserService] >>> Entering IdentityManager.grantRole() [org.hibernate.SQL] select ... from User user0_ where user0_.username=? [org.hibernate.SQL] select ... from Role role0_ where role0_.name=? [myapp.CreateUserService] <<< Exiting IdentityManager.grantRole() [myapp.OpenAccountController] <<< Exiting CreateUserService.createUser()
I was expecting the following insert at the end of IdentityManager.grantRole():
insert into UserRole (user_id, role_id) values (?, ?)
Can someone help me figure this out?
My user and role classes are defined as follows:
@Entity @Table(uniqueConstraints = @UniqueConstraint(columnNames = "username")) public class User implements Serializable { private String username; private String passwordHash; private Set<Role> roles; ... @UserRoles @ManyToMany(targetEntity = Role.class) @JoinTable(name = "UserRole", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
@Entity public class Role implements Serializable { ... private String name; @RoleName public String getName() { return name; } public void setName(String name) { this.name = name; } }
CreateUserService is defined as follows:
@Name("createUserService") @AutoCreate public class CreateUserService { @In private IdentityManager identityManager; public void createUser( final String username, final String password, final String role) { new RunAsOperation() { public void execute() { identityManager.createUser(username, password); identityManager.grantRole(username, role); } }.addRole(RoleName.admin.toString()) .run(); } }
Thanks.
Naresh