* @param userBean {@link UserAccountBean}
* @throws EnMeExpcetion personalize exception
* @return if password is not notified is returned
*/
public void createUser(final UserAccountBean userBean, final String username) throws EnMeExpcetion {
final UserAccount userAccount = new UserAccount();
final Account account = getUserAccount(username).getAccount();
//validate email and password
if (userBean.getEmail() != null && userBean.getUsername() != null) {
userAccount.setUserEmail(userBean.getEmail());
userAccount.setUsername(userBean.getUsername());
userAccount.setAccount(account);
} else {
throw new EnMeExpcetion("needed email and username to create user");
}
String password = null;
if (userBean.getPassword() != null) {
password = userBean.getPassword();
userAccount.setPassword(EnMePasswordUtils.encryptPassworD(password));
}
else{
password = generatePassword();
userAccount.setPassword(EnMePasswordUtils.encryptPassworD(password));
}
//TODO: maybe we need create a table for editor permissions
userAccount.setCompleteName(userBean.getName() == null ? "" : userBean.getUsername());
userAccount.setUserStatus(Boolean.TRUE);
userAccount.setEnjoyDate(Calendar.getInstance().getTime());
// send to user the password to her emails
final SignUpBean singUpBean = new SignUpBean();
singUpBean.setEmail(userBean.getEmail());
singUpBean.setFullName(userAccount.getCompleteName());
singUpBean.setUsername(userBean.getUsername());
singUpBean.setPassword(password);
final String inviteCode = UUID.randomUUID().toString();
userAccount.setInviteCode(inviteCode);
try {
getMailService().sendConfirmYourAccountEmail(singUpBean, inviteCode);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
getMailService().sendPasswordConfirmationEmail(singUpBean);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// save user
getAccountDao().saveOrUpdate(userAccount);
// assing first default group to user
final UserAccount retrievedUser = getAccountDao().getUserAccountById(userAccount.getUid());
final Permission permission = getPermissionByName(SecurityService.DEFAULT);
if (permission != null) {
final List<Permission> all = getPermissionDao().findAllPermissions();
log.info("all permission "+all.size());
log.info("default permission "+permission);
retrievedUser.getSecUserPermissions().add(permission);
}
else{
log.warn("error assing default permissions");
}
log.info("saving user");