throws Exception {
UserAccountForm account = (UserAccountForm) form;
PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, request);
SessionInfo info = this.getSessionInfo(request);
UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
User user = null;
if(udb.supportsAccountCreation()) {
PropertyList roleList = account.getRolesList();
int idx = 0;
Role[] roles = new Role[roleList.size()];
for(Iterator i = roleList.iterator(); i.hasNext(); ) {
roles[idx++] = udb.getRole((String)i.next());
}
if (account.getEditing()) {
user = udb.getAccount(account.getUsername());
try {
udb.updateAccount(user, account.getEmail(), account.getFullname(), roles);
CoreEvent coreEvent = new CoreEvent(this, CoreEventConstants.USER_EDITED, user, info)
.addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user.getPrincipalName())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, user.getFullname())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_ACCOUNT_EMAIL, user.getEmail());
if(roles.length != 0) {
for(int i = 0; i < roles.length; i++ ) {
coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_GROUP + Integer.toString(i+1), roles[i].getPrincipalName());
}
}
CoreServlet.getServlet().fireCoreEvent(coreEvent);
} catch (GroupsRequiredForUserException e) {
saveError(request, "createAccount.error.groupsRequired");
return mapping.findForward("display");
} catch (UserDatabaseException e) {
if(UserDatabaseException.INTERNAL_ERROR == e.getCode()) {
handleException(CoreEventConstants.USER_CREATED, account, info, roles, e);
throw e;
} else {
saveError(request, e.getBundleActionMessage());
return mapping.findForward("display");
}
} catch (Exception e) {
handleException(CoreEventConstants.USER_EDITED, account, info, roles, e);
throw e;
}
} else {
try {
user = udb.createAccount(account.getUsername(), String.valueOf((int) (Math.random() * 100000)),
// Set a random password
account.getEmail(), account.getFullname(), roles);
CoreEvent coreEvent = new CoreEvent(this, CoreEventConstants.USER_CREATED, null, info, CoreEvent.STATE_SUCCESSFUL)
.addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, account.getUsername())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, account.getFullname())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_ACCOUNT_EMAIL, account.getEmail());
if(roles.length != 0) {
for(int i = 0; i < roles.length; i++ ) {
coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_GROUP + Integer.toString(i+1), roles[i].getPrincipalName());
}
}
CoreServlet.getServlet().fireCoreEvent(coreEvent);
} catch (GroupsRequiredForUserException e) {
saveError(request, "createAccount.error.groupsRequired");
return mapping.findForward("display");
} catch (UserDatabaseException e) {
if(UserDatabaseException.INTERNAL_ERROR == e.getCode()) {
handleException(CoreEventConstants.USER_CREATED, account, info, roles, e);
throw e;
} else {
saveError(request, e.getBundleActionMessage());
return mapping.findForward("display");
}
} catch (Exception e) {
handleException(CoreEventConstants.USER_CREATED, account, info, roles, e);
throw e;
}
}
}
else {
user = udb.getAccount(account.getUsername());
}
// Update the attributes
for(Iterator i = account.getAttributeValueItems().iterator(); i.hasNext(); ) {
AttributeValueItem v = (AttributeValueItem)i.next();
if(v.getDefinition().getVisibility() != AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
Property.setProperty(new UserAttributeKey(user, v.getDefinition().getName()), v.getDefinition().formatAttributeValue(v.getValue()), info);
}
}
// XXX HACK to ensure user attributes in memory are the same as persisted
for(Iterator j = LogonControllerFactory.getInstance().getActiveSessions().entrySet().iterator(); j.hasNext(); ) {
Map.Entry e = (Map.Entry)j.next();
SessionInfo sinfo = (SessionInfo)e.getValue();
if(sinfo.getUser().getPrincipalName().equals(user.getPrincipalName())) {
sinfo.setUser(user);
}
}
// Reset the enabled state if it is different