NIHVIVO-2279 first steps toward associating a UserAccount with a Profile.

This commit is contained in:
j2blake 2011-06-29 15:36:14 +00:00
parent 239256187d
commit 87dc7698e1
8 changed files with 380 additions and 33 deletions

View file

@ -130,6 +130,7 @@ public abstract class UserAccountsPage {
map.put("createPassword", UrlBuilder.getUrl("/accounts/createPassword"));
map.put("resetPassword", UrlBuilder.getUrl("/accounts/resetPassword"));
map.put("firstTimeExternal", UrlBuilder.getUrl("/accounts/firstTimeExternal"));
map.put("accountsAjax", UrlBuilder.getUrl("/accountsAjax"));
return map;
}

View file

@ -0,0 +1,26 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.admin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
/**
* TODO
*/
public class UserAccountsAssociatedProfileHelper {
private static final Log log = LogFactory
.getLog(UserAccountsAssociatedProfileHelper.class);
/**
* This profile (if it exists) should be associated with this UserAccount.
* No other profile should be associated with this UserAccount. Make it so.
*/
public static void reconcile(UserAccount userAccount,
String associatedProfileUri) {
log.error("UserAccountsAssociatedProfileHelper.reconcile() not implemented.");
}
}

View file

@ -32,7 +32,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
private static final String PARAMETER_FIRST_NAME = "firstName";
private static final String PARAMETER_LAST_NAME = "lastName";
private static final String PARAMETER_ROLE = "role";
private static final String PARAMETER_ASSOCIATE_WITH_PROFILE = "associate";
private static final String PARAMETER_ASSOCIATED_PROFILE_URI = "associatedProfileUri";
private static final String ERROR_NO_EMAIL = "errorEmailIsEmpty";
private static final String ERROR_EMAIL_IN_USE = "errorEmailInUse";
@ -54,7 +54,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
private String firstName = "";
private String lastName = "";
private String selectedRoleUri = "";
private boolean associateWithProfile;
private String associatedProfileUri = "";
private UserAccount userAccount;
@ -86,8 +86,8 @@ public class UserAccountsEditPage extends UserAccountsPage {
firstName = getStringParameter(PARAMETER_FIRST_NAME, "");
lastName = getStringParameter(PARAMETER_LAST_NAME, "");
selectedRoleUri = getStringParameter(PARAMETER_ROLE, "");
associateWithProfile = isParameterAsExpected(
PARAMETER_ASSOCIATE_WITH_PROFILE, "yes");
associatedProfileUri = getStringParameter(
PARAMETER_ASSOCIATED_PROFILE_URI, "");
strategy.parseAdditionalParameters();
}
@ -186,9 +186,6 @@ public class UserAccountsEditPage extends UserAccountsPage {
body.put("roles", buildRolesList());
}
if (associateWithProfile) {
body.put("associate", Boolean.TRUE);
}
body.put("formUrls", buildUrlsMapWithEditUrl());
if (!errorCode.isEmpty()) {
@ -216,6 +213,7 @@ public class UserAccountsEditPage extends UserAccountsPage {
}
public void updateAccount() {
// Assemble the fields of the account.
userAccount.setEmailAddress(emailAddress);
userAccount.setFirstName(firstName);
userAccount.setLastName(lastName);
@ -227,11 +225,15 @@ public class UserAccountsEditPage extends UserAccountsPage {
userAccount.setPermissionSetUris(Collections
.singleton(selectedRoleUri));
}
strategy.setAdditionalProperties(userAccount);
// Update the account.
userAccountsDao.updateUserAccount(userAccount);
// Associate the profile, as appropriate.
UserAccountsAssociatedProfileHelper.reconcile(userAccount, associatedProfileUri);
// Tell the user.
strategy.notifyUser();
}

View file

@ -0,0 +1,128 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.admin.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageUserAccounts;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.VitroAjaxController;
/**
* Handle the AJAX functions that are specific to the UserAccounts pages.
*/
public class UserAccountsAjaxController extends VitroAjaxController {
private static final String PARAMETER_FUNCTION = "function";
@Override
protected Actions requiredActions(VitroRequest vreq) {
return new Actions(new ManageUserAccounts());
}
@Override
protected void doRequest(VitroRequest vreq, HttpServletResponse resp)
throws ServletException, IOException {
String function = vreq.getParameter(PARAMETER_FUNCTION);
if ("checkExternalAuth".equals(function)) {
new ExternalAuthChecker(this, vreq, resp).processRequest();
} else {
new ErrorResponder(this, vreq, resp).processRequest();
}
}
static abstract class AjaxResponder {
protected final HttpServlet parent;
protected final VitroRequest vreq;
protected final HttpServletResponse resp;
public AjaxResponder(HttpServlet parent, VitroRequest vreq,
HttpServletResponse resp) {
this.parent = parent;
this.vreq = vreq;
this.resp = resp;
}
public abstract void processRequest() throws IOException;
protected String getStringParameter(String key, String defaultValue) {
String value = vreq.getParameter(key);
return (value == null) ? defaultValue : value;
}
}
/**
* What is our reaction to this possible External Auth ID?
*
* Is somebody already using it (other than ourselves)? Does it match an
* existing Profile? Neither?
*/
private static class ExternalAuthChecker extends AjaxResponder {
private static final String PARAMETER_USER_ACCOUNT_URI = "userAccountUri";
private static final String PARAMETER_ETERNAL_AUTH_ID = "externalAuthId";
private static final String RESPONSE_ID_IN_USE = "idInUse";
private static final String RESPONSE_MATCHES_PROFILE = "matchesProfile";
private static final String RESPONSE_PROFILE_URI = "profileUri";
private static final String RESPONSE_PROFILE_URL = "profileUrl";
private static final String RESPONSE_PROFILE_LABEL = "profileLabel";
private final String userAccountUri;
private final String externalAuthId;
public ExternalAuthChecker(HttpServlet parent, VitroRequest vreq,
HttpServletResponse resp) {
super(parent, vreq, resp);
userAccountUri = getStringParameter(PARAMETER_USER_ACCOUNT_URI, "");
externalAuthId = getStringParameter(PARAMETER_ETERNAL_AUTH_ID, "");
}
@Override
public void processRequest() throws IOException {
// TODO For now, a totally bogus response:
// If "A", somebody else is already using the externalAuthId
// If "B", matches "Joe Blow"
// Anything else, no match.
try {
if ("A".equals(externalAuthId)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(RESPONSE_ID_IN_USE, true);
resp.getWriter().write(jsonObject.toString());
} else if ("B".equals(externalAuthId)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(RESPONSE_MATCHES_PROFILE, true);
jsonObject.put(RESPONSE_PROFILE_URI,
"http://some.bogus.profile");
jsonObject.put(RESPONSE_PROFILE_URL,
"http://some.bogus.profileUrl");
jsonObject.put(RESPONSE_PROFILE_LABEL, "bogus label");
resp.getWriter().write(jsonObject.toString());
} else {
resp.getWriter().write("[]");
}
} catch (JSONException e) {
resp.getWriter().write("[]");
}
}
}
private static class ErrorResponder extends AjaxResponder {
public ErrorResponder(HttpServlet parent, VitroRequest vreq,
HttpServletResponse resp) {
super(parent, vreq, resp);
}
@Override
public void processRequest() throws IOException {
resp.getWriter().write("[]");
}
}
}