if (facet!=null && !facet.equals(GROUPS_FACET)) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("Unknown facet "+facet);
}
AuthService auth = (AuthService)getRequest().getAttributes().get(App.AUTH_SERVICE_ATTR);
ChallengeResponse transCred = getRequest().getChallengeResponse();
AuthCredentials cred = new AuthCredentials(transCred.getScheme().toString(),transCred.getIdentifier(),new String(transCred.getSecret()));
if (facet==null) {
if (!top.getName().equals(NM_USER)) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Document element "+top.getName()+" not allowed on user.");
}
String password = top.getAttributeValue("password");
if (password!=null) {
password = password.trim();
}
Element nameE = top.getFirstElementNamed(NM_NAME);
String name = null;
if (nameE!=null) {
name = nameE.getText();
}
Element emailE = top.getFirstElementNamed(NM_EMAIL);
String email = null;
if (emailE!=null) {
email = emailE.getText();
}
try {
User user = auth.getUser(cred,alias);
if (user!=null) {
try {
if (nameE!=null || emailE!=null) {
// The name starts the same and can only be changed. It can't be deleted
String newName = user.getName();
// Set it to the new name only if it was specified
if (name!=null) {
newName = name;
}
String newEmail = user.getEmail();
// Set to new e-mail if it exists
if (email!=null) {
newEmail = email;
}
// If the e-mail was missing, remove it
if (emailE==null) {
newEmail = null;
}
auth.updateUser(cred,alias,newName,newEmail);
}
if (password!=null) {
if (!auth.setPassword(cred,alias,password)) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
}
getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
return null;
} catch (AuthException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot modify user "+alias+": "+ex.getMessage(),ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("Internal error, see logs.");
}
} else {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("User "+alias+" does not exist.");
}
} catch (AuthException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot check user "+alias+": "+ex.getMessage(),ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("Internal error, see logs.");
}
} else if (facetAlias!=null) {
getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
return null;
} else {
if (!top.getName().equals(NM_GROUP)) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Document element "+top.getName()+" not allowed to groups.");
}
String group = top.getAttributeValue("alias");
if (group==null) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("The 'alias' attribute is missing.");
}
try {
if (auth.addUserToGroup(cred,alias,group)) {
getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
} else {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
}
return null;