use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UserDetailsReset method send_reset_email.
/* find csid for email, create token, email token to the user */
private void send_reset_email(Storage storage, UIRequest request, Request in) throws UIException {
JSONObject data = null;
data = request.getJSONBody();
// mock login else service layer gets upset = not working
// XXX ARGH
AdminData ad = spec.getAdminData();
request.getSession().setValue(UISession.USERID, ad.getAuthUser());
request.getSession().setValue(UISession.PASSWORD, ad.getAuthPass());
in.reset();
JSONObject outputJSON = new JSONObject();
if (testSuccess(in.getStorage())) {
String emailparam = "";
/* get csid of email address */
try {
emailparam = data.getString("email");
JSONObject userdetails = getcsID(storage, emailparam);
if (!userdetails.getBoolean("isError")) {
String csid = userdetails.getString("csid");
/* for debug purposes */
if (data.has("debug") && data.getBoolean("debug")) {
// only send email if debug is false/null see unit test TestGeneral testPasswordReset
outputJSON.put("token", createToken(csid));
outputJSON.put("email", emailparam);
} else {
doEmail(csid, emailparam, in, userdetails);
}
outputJSON.put("isError", false);
JSONObject messages = new JSONObject();
messages.put("message", "Password reset sent to " + emailparam);
messages.put("severity", "info");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
} else {
outputJSON = userdetails;
}
request.getSession().setValue(UISession.USERID, "");
request.getSession().setValue(UISession.PASSWORD, "");
in.reset();
} catch (UIException e) {
// throw new UIException("Failed to send email",e);
try {
outputJSON.put("isError", true);
JSONObject messages = new JSONObject();
messages.put("message", "Failed to send email: " + e.getMessage());
messages.put("severity", "error");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
} catch (JSONException e1) {
throw new UIException("JSONException during error messaging", e);
}
} catch (JSONException e) {
throw new UIException("JSONException during search on email address", e);
}
} else {
try {
outputJSON.put("isError", true);
JSONObject messages = new JSONObject();
messages.put("message", "The admin details in cspace-config.xml failed");
messages.put("severity", "error");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
}
}
request.sendJSONResponse(outputJSON);
request.setOperationPerformed(Operation.CREATE);
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UserDetailsReset method reset_password.
/* check token and if matches csid then reset password
* */
private void reset_password(Storage storage, UIRequest request, Request in) throws UIException {
// mock login else service layer gets upset
// XXX ARGH
AdminData ad = spec.getAdminData();
request.getSession().setValue(UISession.USERID, ad.getAuthUser());
request.getSession().setValue(UISession.PASSWORD, ad.getAuthPass());
in.reset();
JSONObject outputJSON = new JSONObject();
if (testSuccess(in.getStorage())) {
JSONObject data = null;
data = request.getJSONBody();
String token;
try {
token = data.getString("token");
String password = data.getString("password");
String email = data.getString("email");
JSONObject userdetails = getcsID(storage, email);
if (!userdetails.getBoolean("isError")) {
String csid = userdetails.getString("csid");
if (testToken(csid, token)) {
/* update userdetails */
String path = csid;
JSONObject fields = userdetails.getJSONObject("fields");
try {
JSONObject changedata = new JSONObject();
JSONObject updatefields = fields;
updatefields.put("password", password);
changedata.put("fields", updatefields);
changedata.put("csid", csid);
sendJSON(storage, path, changedata);
outputJSON.put("isError", false);
JSONObject messages = new JSONObject();
messages.put("message", "Your Password has been succesfully changed, Please login");
messages.put("severity", "info");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
} catch (ExistException x) {
throw new UIException("Existence exception: ", x);
} catch (UnimplementedException x) {
throw new UIException("Unimplemented exception: ", x);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
outputJSON = uiexception.getJSON();
}
} else {
outputJSON.put("isError", false);
JSONObject messages = new JSONObject();
messages.put("message", "Token was not valid");
messages.put("severity", "error");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
}
} else {
outputJSON = userdetails;
}
request.getSession().setValue(UISession.USERID, "");
request.getSession().setValue(UISession.PASSWORD, "");
in.reset();
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
}
} else {
try {
outputJSON.put("isError", false);
JSONObject messages = new JSONObject();
messages.put("message", "The admin details in cspace-config.xml failed");
messages.put("severity", "error");
JSONArray arr = new JSONArray();
arr.put(messages);
outputJSON.put("messages", arr);
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
}
}
/* should we automagically log them in or let them do that?,
* I think we should let them login, it has the advantage
* that they find out straight away if they can't remember the new password */
request.sendJSONResponse(outputJSON);
request.setOperationPerformed(Operation.CREATE);
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UserDetailsReset method createHash.
private String createHash(String csid) throws UIException {
try {
byte[] buffer = csid.getBytes();
byte[] result = null;
StringBuffer buf = null;
MessageDigest md5 = MessageDigest.getInstance("MD5");
result = new byte[md5.getDigestLength()];
md5.reset();
md5.update(buffer);
result = md5.digest(tokensalt.getBytes());
// create hex string from the 16-byte hash
buf = new StringBuffer(result.length * 2);
for (int i = 0; i < result.length; i++) {
int intVal = result[i] & 0xff;
if (intVal < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intVal).toUpperCase());
}
return buf.toString().substring(0, 5);
} catch (NoSuchAlgorithmException e) {
throw new UIException("There were problems with the algorithum");
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UserDetailsSearchList method search_or_list.
private void search_or_list(Storage storage, UIRequest ui, String param, String pageSize, String pageNum) throws UIException {
try {
JSONObject restriction = new JSONObject();
String key = "items";
if (param != null) {
restriction.put("screenName", param);
key = "results";
}
if (pageSize != null) {
restriction.put("pageSize", pageSize);
}
if (pageNum != null) {
restriction.put("pageNum", pageNum);
}
JSONObject data = storage.getPathsJSON(base, restriction);
String[] paths = (String[]) data.get("listItems");
JSONObject pagination = new JSONObject();
if (data.has("pagination")) {
pagination = data.getJSONObject("pagination");
}
JSONObject resultsObject = new JSONObject();
resultsObject = pathsToJSON(storage, base, paths, key, pagination);
ui.sendJSONResponse(resultsObject);
} catch (JSONException e) {
throw new UIException("JSONException during autocompletion", e);
} catch (ExistException e) {
throw new UIException("ExistException during autocompletion", e);
} catch (UnimplementedException e) {
throw new UIException("UnimplementedException during autocompletion", e);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
ui.sendJSONResponse(uiexception.getJSON());
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UserRolesCreate method store_set.
private void store_set(Storage storage, UIRequest request, String path) throws UIException {
JSONObject data = null;
data = request.getJSONBody();
boolean notfailed = true;
String msg = "";
try {
path = sendJSON(storage, null, data);
if (path == null) {
throw new UIException("Insufficient data for create (no fields?)");
}
data.put("csid", path);
boolean isError = !notfailed;
data.put("isError", isError);
JSONObject messages = new JSONObject();
messages.put("message", msg);
if (notfailed) {
messages.put("severity", "info");
} else {
messages.put("severity", "error");
}
JSONArray arr = new JSONArray();
arr.put(messages);
data.put("messages", arr);
request.sendJSONResponse(data);
request.setOperationPerformed(Operation.CREATE);
if (notfailed)
request.setSecondaryRedirectPath(new String[] { url_base, path });
} catch (JSONException x) {
throw new UIException("Failed to parse json: " + x, x);
} catch (ExistException x) {
throw new UIException("Existence exception: ", x);
} catch (UnimplementedException x) {
throw new UIException("Unimplemented exception: ", x);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
}
}
Aggregations