How to persist user?
praty Sep 12, 2009 3:26 PMDear Team,
Using seam examples I tried to persist user using UserHome. However it throws entity detached exception. Please guide as how to store the user with hashed password and generated salt.
Even guide how to deal with creating and persisting Roles and RoleGroups.
Thank you in advance.
@Name("userHome")
public class UserHome extends EntityHome<User> {
.
.
@PrePersist
@Transactional
public void handlePrePersist() {
createUser();
}
@Transactional
private void createUser() {
instance.setPassword(generateHashedPassword());
identityManager.createUser(instance.getUserName(),instance.getPassword());
identityManager.grantRole(instance.getUserName(), "user");
}
@Transactional
private String generateHashedPassword() {
byte[] salt;
String passwordSalt = instance.getPasswordSalt();
if (passwordSalt == null || "".equals(passwordSalt.trim())) {
salt = PasswordHash.instance().generateRandomSalt();
passwordSalt = BinTools.bin2hex(salt);
instance.setPasswordSalt(passwordSalt);
} else {
salt = BinTools.hex2bin(passwordSalt);
}
return identityStore.generatePasswordHash(instance.getPassword(), salt);
}
}
@Entity
@Table(name = "USER", schema = "security", catalog = "philately")
public class User implements Serializable {
private static final long serialVersionUID = -520371581731672359L;
private Long id;
private String userName;
private String password;
private String passwordSalt;
private boolean enabled;
private Set<Role> roles;
public User(){}
public User(Long id, String userName, String password, boolean enabled) {
this.id = id;
this.userName = userName;
this.password = password;
this.enabled = enabled;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
@NotNull
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "USER_NAME", unique = true, nullable = false, length = 15)
@NotNull
@UserPrincipal
@Length(max = 15)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "PASSWORD", nullable = false, length = 50)
@NotNull
@UserPassword(hash = "MD5")
@Length(max = 50)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
System.out.println("password" + password);
}
@Column(name = "PASSWORD_SALT", nullable = true, length = 50)
@PasswordSalt
public String getPasswordSalt() {
return passwordSalt;
}
public void setPasswordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
}
@Column(name = "ENABLED", nullable = true)
@UserEnabled
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@UserRoles
@ManyToMany(targetEntity = Role.class)
@JoinTable(name = "security.USER_ROLE",
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;
}
}
Using seam examples I tried to persist user using UserHome. However it throws entity detached exception. Please guide as how to store the user with hashed password and generated salt.
Even guide how to deal with creating and persisting Roles and RoleGroups.
Thank you in advance.
@Name("userHome")
public class UserHome extends EntityHome<User> {
.
.
@PrePersist
@Transactional
public void handlePrePersist() {
createUser();
}
@Transactional
private void createUser() {
instance.setPassword(generateHashedPassword());
identityManager.createUser(instance.getUserName(),instance.getPassword());
identityManager.grantRole(instance.getUserName(), "user");
}
@Transactional
private String generateHashedPassword() {
byte[] salt;
String passwordSalt = instance.getPasswordSalt();
if (passwordSalt == null || "".equals(passwordSalt.trim())) {
salt = PasswordHash.instance().generateRandomSalt();
passwordSalt = BinTools.bin2hex(salt);
instance.setPasswordSalt(passwordSalt);
} else {
salt = BinTools.hex2bin(passwordSalt);
}
return identityStore.generatePasswordHash(instance.getPassword(), salt);
}
}
@Entity
@Table(name = "USER", schema = "security", catalog = "philately")
public class User implements Serializable {
private static final long serialVersionUID = -520371581731672359L;
private Long id;
private String userName;
private String password;
private String passwordSalt;
private boolean enabled;
private Set<Role> roles;
public User(){}
public User(Long id, String userName, String password, boolean enabled) {
this.id = id;
this.userName = userName;
this.password = password;
this.enabled = enabled;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
@NotNull
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "USER_NAME", unique = true, nullable = false, length = 15)
@NotNull
@UserPrincipal
@Length(max = 15)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "PASSWORD", nullable = false, length = 50)
@NotNull
@UserPassword(hash = "MD5")
@Length(max = 50)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
System.out.println("password" + password);
}
@Column(name = "PASSWORD_SALT", nullable = true, length = 50)
@PasswordSalt
public String getPasswordSalt() {
return passwordSalt;
}
public void setPasswordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
}
@Column(name = "ENABLED", nullable = true)
@UserEnabled
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@UserRoles
@ManyToMany(targetEntity = Role.class)
@JoinTable(name = "security.USER_ROLE",
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;
}
}