final String templateFolder = preferences.getValue(NEW_USER_TEMPLATE_DIR, "/_user/template/");
final String subsiteRoot = preferences.getValue(SUB_SITE_ROOT,"");
profilingRule = defaultProfile.toString();
add(new FeedbackPanel("feedback"));
Form userForm = new Form("newUserForm");
add(userForm);
RequiredTextField userName = new RequiredTextField("userName",
new PropertyModel(this, "userName"));
userName.add(new PrincipalNameValidator());
userForm.add(userName);
TextField password = new PasswordTextField("password",
new PropertyModel(this, "password"));
userForm.add(password);
CheckBox changePassword = new CheckBox("checkpass",
new PropertyModel(this, "checkpass"));
userForm.add(changePassword);
DropDownChoice profilingtRole = new DropDownChoice("profilingRule",
new PropertyModel(this, "profilingRule"), getProfileList());
userForm.add(profilingtRole);
Button newUser = new Button("addUser",new ResourceModel(principalParam +".add.button"))
{
@Override
public void onSubmit()
{
UserManager userManager = (UserManager)getManager();
JetspeedPrincipal principal = getManager().newPrincipal(
getUserName(), false);
RoleManager roleManager = ((AbstractAdminWebApplication)getApplication()).getServiceLocator().getRoleManager();
PageManager pageManager = ((AbstractAdminWebApplication) getApplication()).getServiceLocator().getPageManager();
try
{
getManager().addPrincipal(principal, null);
User user = userManager.getUser(getUserName());
if(!StringUtils.isEmpty(defaultRole))
{
roleManager.addRoleToUser(getUserName(), defaultRole);
}
if(!StringUtils.isEmpty(requiredRole))
{
roleManager.addRoleToUser(getUserName(), requiredRole);
}
Profiler profiler = getServiceLocator().getProfiler();
if (!StringUtils.isEmpty(getProfilingRule()))
{
ProfilingRule profilingRule = profiler.getRule(getProfilingRule());
if (profilingRule != null)
{
profiler.setRuleForPrincipal(principal, profilingRule, "default");
}
else
{
log.error("Failed to set profiling rule for principal. Invalid profiling rule: " + getProfilingRule());
}
}
else if (!StringUtils.isEmpty(defaultProfile))
{
ProfilingRule defaultProfilingRule = profiler.getRule(defaultProfile);
if (defaultProfilingRule != null)
{
profiler.setRuleForPrincipal(principal, defaultProfilingRule, "default");
}
else
{
if (log.isDebugEnabled())
{
log.debug("Default profiling rule is not applied to the principal because the default profiling rule is not found: " + defaultProfile);
}
}
}
String subSite;
if (!StringUtils.isEmpty(defaultSubsite))
{
user.getSecurityAttributes().getAttribute(User.JETSPEED_USER_SUBSITE_ATTRIBUTE,true).setStringValue(defaultSubsite);
user.getSecurityAttributes().getAttribute(User.JETSPEED_USER_SUBSITE_ATTRIBUTE, true).setStringValue(defaultSubsite);
subSite = subsiteRoot + defaultSubsite + Folder.USER_FOLDER + user.getName();
}
else
{
subSite = Folder.USER_FOLDER + user.getName();;
}
if (!StringUtils.isEmpty(templateFolder))
{
try
{
Folder source = pageManager.getFolder(templateFolder);
pageManager.deepCopyFolder(source, subSite, user.getName());
}
catch (FolderNotFoundException e)
{
error(e.getMessage());
}
catch (NodeException e)
{
error(e.getMessage());
}
}
userManager.updateUser(user);
PasswordCredential credential = userManager
.getPasswordCredential(user);
if (!StringUtils.isEmpty(getPassword()))
{
credential.setPassword(getPassword(), false);
}
credential.setUpdateRequired(isCheckpass());
userManager.storePasswordCredential(credential);
setPrincipal(user);
controlPannels(true);
}
catch (SecurityException jSx)
{
log.error("Failed to update user.", jSx);
}
principalDataProvider.refresh(getManager(),getRoleManager(),getSearchString());
}
};
userForm.add(newUser);
}