NIHVIVO-3523 Handle multiple roles properly when creating or editing user accounts. For a new account, suggest the default roles. Still using only one role, because we have radio buttons instead of checkboxes.

This commit is contained in:
j2blake 2011-12-23 19:24:51 +00:00
parent c18e14a106
commit a75b15d940
4 changed files with 44 additions and 30 deletions

View file

@ -2,13 +2,17 @@
package edu.cornell.mannlib.vitro.webapp.controller.accounts.admin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.PermissionSet;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status;
@ -55,7 +59,7 @@ public class UserAccountsAddPage extends UserAccountsPage {
private boolean externalAuthOnly;
private String firstName = "";
private String lastName = "";
private String selectedRoleUri = "";
private Collection<String> selectedRoleUris = Collections.emptyList();
private String associatedProfileUri = "";
private String newProfileClassUri = "";
@ -88,7 +92,7 @@ public class UserAccountsAddPage extends UserAccountsPage {
externalAuthOnly = isFlagOnRequest(PARAMETER_EXTERNAL_AUTH_ONLY);
firstName = getStringParameter(PARAMETER_FIRST_NAME, "");
lastName = getStringParameter(PARAMETER_LAST_NAME, "");
selectedRoleUri = getStringParameter(PARAMETER_ROLE, "");
selectedRoleUris = getStringParameters(PARAMETER_ROLE);
associatedProfileUri = getStringParameter(
PARAMETER_ASSOCIATED_PROFILE_URI, "");
newProfileClassUri = getStringParameter(
@ -114,7 +118,7 @@ public class UserAccountsAddPage extends UserAccountsPage {
errorCode = ERROR_NO_FIRST_NAME;
} else if (lastName.isEmpty()) {
errorCode = ERROR_NO_LAST_NAME;
} else if (selectedRoleUri.isEmpty()) {
} else if (selectedRoleUris.isEmpty()) {
errorCode = ERROR_NO_ROLE;
} else {
errorCode = strategy.additionalValidations();
@ -155,7 +159,7 @@ public class UserAccountsAddPage extends UserAccountsPage {
u.setLoginCount(0);
u.setLastLoginTime(0L);
u.setStatus(Status.INACTIVE);
u.setPermissionSetUris(Collections.singleton(selectedRoleUri));
u.setPermissionSetUris(selectedRoleUris);
strategy.setAdditionalProperties(u);
@ -189,11 +193,20 @@ public class UserAccountsAddPage extends UserAccountsPage {
public final ResponseValues showPage() {
Map<String, Object> body = new HashMap<String, Object>();
if (isSubmit()) {
body.put(PARAMETER_EMAIL_ADDRESS, emailAddress);
body.put(PARAMETER_EXTERNAL_AUTH_ID, externalAuthId);
body.put(PARAMETER_FIRST_NAME, firstName);
body.put(PARAMETER_LAST_NAME, lastName);
body.put("selectedRole", selectedRoleUri);
body.put("selectedRoles", selectedRoleUris);
} else {
body.put(PARAMETER_EMAIL_ADDRESS, "");
body.put(PARAMETER_EXTERNAL_AUTH_ID, "");
body.put(PARAMETER_FIRST_NAME, "");
body.put(PARAMETER_LAST_NAME, "");
body.put("selectedRoles", getDefaultRolesForNewUsers());
}
body.put("roles", buildListOfSelectableRoles());
body.put("profileTypes", buildProfileTypesList());
body.put(PARAMETER_NEW_PROFILE_CLASS_URI, newProfileClassUri);
@ -221,6 +234,16 @@ public class UserAccountsAddPage extends UserAccountsPage {
return new TemplateResponseValues(TEMPLATE_NAME, body);
}
private Collection<String> getDefaultRolesForNewUsers() {
List<String> list = new ArrayList<String>();
for (PermissionSet ps : userAccountsDao.getAllPermissionSets()) {
if (ps.isForNewUsers()) {
list.add(ps.getUri());
}
}
return list;
}
public UserAccount getAddedAccount() {
return addedAccount;
}

View file

@ -2,11 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.controller.accounts.admin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -64,7 +65,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
private boolean externalAuthOnly;
private String firstName = "";
private String lastName = "";
private String selectedRoleUri = "";
private Collection<String> selectedRoleUris = new ArrayList<String>();
private String associatedProfileUri = "";
private String newProfileClassUri = "";
@ -101,7 +102,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
externalAuthOnly = isFlagOnRequest(PARAMETER_EXTERNAL_AUTH_ONLY);
firstName = getStringParameter(PARAMETER_FIRST_NAME, "");
lastName = getStringParameter(PARAMETER_LAST_NAME, "");
selectedRoleUri = getStringParameter(PARAMETER_ROLE, "");
selectedRoleUris = getStringParameters(PARAMETER_ROLE);
associatedProfileUri = getStringParameter(
PARAMETER_ASSOCIATED_PROFILE_URI, "");
newProfileClassUri = getStringParameter(
@ -155,7 +156,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
errorCode = ERROR_NO_FIRST_NAME;
} else if (lastName.isEmpty()) {
errorCode = ERROR_NO_LAST_NAME;
} else if (!isRootUser() && selectedRoleUri.isEmpty()) {
} else if (!isRootUser() && selectedRoleUris.isEmpty()) {
errorCode = ERROR_NO_ROLE;
} else {
errorCode = strategy.additionalValidations();
@ -203,7 +204,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
body.put("externalAuthId", externalAuthId);
body.put("firstName", firstName);
body.put("lastName", lastName);
body.put("selectedRole", selectedRoleUri);
body.put("selectedRoles", selectedRoleUris);
body.put(PARAMETER_NEW_PROFILE_CLASS_URI, newProfileClassUri);
if (externalAuthOnly) {
@ -219,7 +220,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
body.put("externalAuthId", userAccount.getExternalAuthId());
body.put("firstName", userAccount.getFirstName());
body.put("lastName", userAccount.getLastName());
body.put("selectedRole", getExistingRoleUri());
body.put("selectedRoles", userAccount.getPermissionSetUris());
body.put(PARAMETER_NEW_PROFILE_CLASS_URI, "");
if (userAccount.isExternalAuthOnly()) {
@ -254,15 +255,6 @@ public class UserAccountsEditPage extends UserAccountsPage {
return new TemplateResponseValues(TEMPLATE_NAME, body);
}
private String getExistingRoleUri() {
Set<String> uris = userAccount.getPermissionSetUris();
if (uris.isEmpty()) {
return "";
} else {
return uris.iterator().next();
}
}
private Map<String, String> buildUrlsMapWithEditUrl() {
Map<String, String> map = buildUrlsMap();
map.put("edit", editAccountUrl(userAccount.getUri()));
@ -287,8 +279,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
userAccount.setPermissionSetUris(Collections.<String> emptySet());
userAccount.setExternalAuthOnly(false);
} else {
userAccount.setPermissionSetUris(Collections
.singleton(selectedRoleUri));
userAccount.setPermissionSetUris(selectedRoleUris);
userAccount.setExternalAuthOnly(externalAuthOnly);
}
strategy.setAdditionalProperties(userAccount);

View file

@ -67,7 +67,7 @@
<p><input id="externalAuthChkBox" type="checkbox" name="externalAuthOnly" <#if externalAuthOnly?? >checked</#if> />Externally Authenticated Only</p>
<p>Roles<span class="requiredHint"> *</span></p>
<#list roles as role>
<input type="radio" name="role" value="${role.uri}" role="radio" <#if selectedRole = role.uri>checked</#if> />
<input type="radio" name="role" value="${role.uri}" role="radio" ${selectedRoles?seq_contains(role.uri)?string("checked", "")} />
<label class="inline" for="${role.label}"> ${role.label}</label>
<br />
</#list>

View file

@ -68,7 +68,7 @@
<#if roles?has_content>
<p>Roles<span class="requiredHint"> *</span></p>
<#list roles as role>
<input type="radio" name="role" value="${role.uri}" role="radio" <#if selectedRole = role.uri>checked</#if> />
<input type="radio" name="role" value="${role.uri}" role="radio" ${selectedRoles?seq_contains(role.uri)?string("checked", "")} />
<label class="inline" for="${role.label}"> ${role.label}</label>
<br />
</#list>