Improving user management API implementation

merge-requests/7/head
prabathabey 9 years ago
parent 627d99e913
commit c9a2dc4399

@ -27,58 +27,10 @@ import java.util.List;
@ApiModel(value = "List of users", description = "This contains a set of users that matches a given " +
"criteria as a collection")
public class UserList {
private int count;
private String next;
private String previous;
public class UserList extends BasePaginatedResult {
private List<UserWrapper> users = new ArrayList<>();
/**
* Number of Devices returned.
*/
@ApiModelProperty(value = "Number of users returned.")
@JsonProperty("count")
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
/**
* Link to the next subset of resources qualified. \nEmpty if no more resources are to be returned.
*/
@ApiModelProperty(value = "Link to the next subset of resources qualified. \n " +
"Empty if no more resources are to be returned.")
@JsonProperty("next")
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
/**
* Link to the previous subset of resources qualified. \nEmpty if current subset is the first subset returned.
*/
@ApiModelProperty(value = "Link to the previous subset of resources qualified. \n" +
"Empty if current subset is the first subset returned.")
@JsonProperty("previous")
public String getPrevious() {
return previous;
}
public void setPrevious(String previous) {
this.previous = previous;
}
/**
**/
@ApiModelProperty(value = "List of devices returned")
@JsonProperty("users")
public List<UserWrapper> getList() {
@ -94,9 +46,9 @@ public class UserList {
StringBuilder sb = new StringBuilder();
sb.append("{\n");
sb.append(" count: ").append(count).append(",\n");
sb.append(" next: ").append(next).append(",\n");
sb.append(" previous: ").append(previous).append(",\n");
sb.append(" count: ").append(getCount()).append(",\n");
sb.append(" next: ").append(getNext()).append(",\n");
sb.append(" previous: ").append(getPrevious()).append(",\n");
sb.append(" users: [").append(users).append("\n");
sb.append("]}\n");
return sb.toString();

@ -317,7 +317,7 @@ public interface UserManagementService {
@ApiParam(
name = "filter",
value = "Username of the user details to be fetched.",
required = true)
required = false)
@QueryParam("filter") String filter,
@ApiParam(
name = "If-Modified-Since",

@ -26,10 +26,12 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserList;
import org.wso2.carbon.device.mgt.jaxrs.beans.UserWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.api.UserManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
@ -86,11 +88,13 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) {
String msg = "Exception in trying to add user '" + userWrapper.getUsername() + "' to the user store";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (DeviceManagementException e) {
String msg = "ErrorResponse occurred while inviting user to enroll the device";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -188,7 +192,8 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving information of the user '" + username + "'";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -246,7 +251,8 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException | UnsupportedEncodingException e) {
String msg = "Exception in trying to update user by username: " + userWrapper.getUsername();
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -294,7 +300,8 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) {
String msg = "Exception in trying to remove user by username: " + username;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -318,7 +325,8 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) {
String msg = "Exception in trying to retrieve roles for user by username: " + username;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -356,9 +364,9 @@ public class UserManagementServiceImpl implements UserManagementService {
offsetList = new ArrayList<>();
}
if (offsetList.size() <= 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No users available for retrieval").build();
}
// if (offsetList.size() <= 0) {
// return Response.status(Response.Status.NOT_FOUND).entity("No users available for retrieval").build();
// }
UserList result = new UserList();
result.setList(offsetList);
@ -368,7 +376,8 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving the list of users.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -394,14 +403,15 @@ public class UserManagementServiceImpl implements UserManagementService {
user.setLastname(getClaimValue(username, Constants.USER_CLAIM_LAST_NAME));
userList.add(user);
}
if (userList.size() <= 0) {
return Response.status(Response.Status.NOT_FOUND).entity("No user is available to be retrieved").build();
}
// if (userList.size() <= 0) {
// return Response.status(Response.Status.NOT_FOUND).entity("No user is available to be retrieved").build();
// }
return Response.status(Response.Status.OK).entity(userList).build();
} catch (UserStoreException e) {
String msg = "ErrorResponse occurred while retrieving the list of users using the filter : " + filter;
String msg = "Error occurred while retrieving the list of users using the filter : " + filter;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}

@ -21,7 +21,9 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.util;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@ -245,4 +247,12 @@ public class RequestValidationUtil {
}
}
public static void validateCredentials(OldPasswordResetWrapper credentials) {
if (credentials == null || credentials.getNewPassword() == null || credentials.getOldPassword() == null) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Old or New password " +
"fields cannot be empty").build());
}
}
}

@ -20,8 +20,12 @@ package org.wso2.carbon.device.mgt.jaxrs.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
@ -48,17 +52,17 @@ public class CredentialManagementResponseBuilder {
try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (!userStoreManager.isExistingUser(username)) {
return Response.status(Response.Status.BAD_REQUEST).entity("No user found with the username "
+ username).build();
}
if (credentials == null || credentials.getNewPassword() == null || credentials.getOldPassword() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Password cannot be empty.")
.build();
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username "
+ username).build());
}
RequestValidationUtil.validateCredentials(credentials);
if (!validateCredential(credentials.getNewPassword())) {
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
return Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build();
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build());
}
userStoreManager.updateCredential(username, credentials.getNewPassword(),
credentials.getOldPassword());
@ -66,12 +70,14 @@ public class CredentialManagementResponseBuilder {
username + " was successfully changed.").build();
} catch (UserStoreException e) {
log.error(e.getMessage(), e);
return Response.status(Response.Status.BAD_REQUEST).entity("Old password does not match.").build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(e.getMessage()).build());
} catch (UnsupportedEncodingException e) {
String errorMsg = "Could not change the password of the user: " + username +
String msg = "Could not change the password of the user: " + username +
". The Character Encoding is not supported.";
log.error(errorMsg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMsg).build();
log.error(msg, e);
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}
@ -85,17 +91,20 @@ public class CredentialManagementResponseBuilder {
try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
if (!userStoreManager.isExistingUser(username)) {
return Response.status(Response.Status.BAD_REQUEST).entity("No user found with the username "
+ username).build();
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username "
+ username).build());
}
if (credentials == null || credentials.getNewPassword() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Password cannot be empty.")
.build();
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Password cannot be empty."
+ username).build());
}
if (!validateCredential(credentials.getNewPassword())) {
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
return Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build();
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build());
}
userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword());
return Response.status(Response.Status.OK).entity("UserImpl password by username: " +
@ -103,12 +112,14 @@ public class CredentialManagementResponseBuilder {
} catch (UserStoreException e) {
String msg = "ErrorResponse occurred while updating the credentials of user '" + username + "'";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
} catch (UnsupportedEncodingException e) {
String msg = "Could not change the password of the user: " + username +
". The Character Encoding is not supported.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
}
}

Loading…
Cancel
Save