-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic DSM user admin service and endpoints (#2626)
* Add beginnings of register participant admin command * Fix checkstyle violation * Add support for multiple users per request * Add user create and remove support * Remove participant registration feature * Initial implementation of DSM user administration * Updates due to code review, mostly reworked UserDao
- Loading branch information
1 parent
b20e29a
commit 91ec798
Showing
17 changed files
with
1,893 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/admin/StudyRoleRoute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.broadinstitute.dsm.route.admin; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.broadinstitute.dsm.exception.DSMBadRequestException; | ||
import org.broadinstitute.dsm.exception.DsmInternalError; | ||
import org.broadinstitute.dsm.security.RequestHandler; | ||
import org.broadinstitute.dsm.service.admin.StudyRoleResponse; | ||
import org.broadinstitute.dsm.service.admin.UserAdminService; | ||
import org.broadinstitute.dsm.statics.RoutePath; | ||
import org.broadinstitute.lddp.handlers.util.Result; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
@Slf4j | ||
public class StudyRoleRoute extends RequestHandler { | ||
|
||
@Override | ||
public Object processRequest(Request request, Response response, String userId) { | ||
String studyGroup; | ||
try { | ||
studyGroup = UserAdminService.getStudyGroup(request.queryMap().toMap()); | ||
} catch (Exception e) { | ||
return UserRoleRoute.handleError(e, "getting study group", response); | ||
} | ||
|
||
UserAdminService adminService = new UserAdminService(userId, studyGroup); | ||
|
||
if (!request.requestMethod().equals(RoutePath.RequestMethod.GET.toString())) { | ||
String msg = "Invalid HTTP method for UserRoleRoute: " + request.requestMethod(); | ||
log.error(msg); | ||
response.status(500); | ||
return msg; | ||
} | ||
|
||
try { | ||
return adminService.getStudyRoles(); | ||
} catch (Exception e) { | ||
return UserRoleRoute.handleError(e, "getting study roles", response); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
pepper-apis/dsm-core/src/main/java/org/broadinstitute/dsm/route/admin/UserRoleRoute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.broadinstitute.dsm.route.admin; | ||
|
||
import com.google.gson.Gson; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.broadinstitute.dsm.exception.DSMBadRequestException; | ||
import org.broadinstitute.dsm.exception.DsmInternalError; | ||
import org.broadinstitute.dsm.security.RequestHandler; | ||
import org.broadinstitute.dsm.service.admin.UserRequest; | ||
import org.broadinstitute.dsm.service.admin.UserRoleRequest; | ||
import org.broadinstitute.dsm.service.admin.UserAdminService; | ||
import org.broadinstitute.dsm.statics.RoutePath; | ||
import org.broadinstitute.lddp.handlers.util.Result; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
@Slf4j | ||
public class UserRoleRoute extends RequestHandler { | ||
|
||
@Override | ||
public Object processRequest(Request request, Response response, String userId) { | ||
String studyGroup; | ||
try { | ||
studyGroup = UserAdminService.getStudyGroup(request.queryMap().toMap()); | ||
} catch (Exception e) { | ||
return handleError(e, "getting study group", response); | ||
} | ||
|
||
UserAdminService service = new UserAdminService(userId, studyGroup); | ||
|
||
String requestMethod = request.requestMethod(); | ||
String body = request.body(); | ||
boolean hasBody = !StringUtils.isBlank(body); | ||
|
||
if (requestMethod.equals(RoutePath.RequestMethod.GET.toString())) { | ||
UserRequest req = null; | ||
if (hasBody) { | ||
try { | ||
req = new Gson().fromJson(body, UserRequest.class); | ||
} catch (Exception e) { | ||
log.info("Invalid request format for {}", body); | ||
response.status(400); | ||
return "Invalid request format"; | ||
} | ||
} | ||
try { | ||
return service.getUserRoles(req); | ||
} catch (Exception e) { | ||
return handleError(e, "getting user roles", response); | ||
} | ||
} | ||
|
||
if (!hasBody) { | ||
response.status(400); | ||
return "Request body is blank"; | ||
} | ||
|
||
UserRoleRequest req; | ||
try { | ||
req = new Gson().fromJson(body, UserRoleRequest.class); | ||
} catch (Exception e) { | ||
log.info("Invalid request format for {}", body); | ||
response.status(400); | ||
return "Invalid request format"; | ||
} | ||
|
||
if (requestMethod.equals(RoutePath.RequestMethod.POST.toString())) { | ||
try { | ||
service.addUserRoles(req); | ||
} catch (Exception e) { | ||
return handleError(e, "adding user roles", response); | ||
} | ||
} else if (requestMethod.equals(RoutePath.RequestMethod.DELETE.toString())) { | ||
try { | ||
service.removeUserRoles(req); | ||
} catch (Exception e) { | ||
return handleError(e, "removing user roles", response); | ||
} | ||
} else { | ||
String msg = "Invalid HTTP method for UserRoleRoute: " + requestMethod; | ||
log.error(msg); | ||
response.status(500); | ||
return msg; | ||
} | ||
|
||
return new Result(200); | ||
} | ||
|
||
protected static String handleError(Throwable e, String operation, Response response) { | ||
if (e instanceof DSMBadRequestException) { | ||
response.status(400); | ||
log.info("DSMBadRequestException {}: {}", operation, e.getMessage()); | ||
return e.getMessage(); | ||
} else if (e instanceof DsmInternalError) { | ||
log.error("Error {}: {}", operation, e.getMessage()); | ||
response.status(500); | ||
return "Internal error. Contact development team"; | ||
} | ||
|
||
// any other exception | ||
log.error("Error {}: {}", operation, e.getMessage()); | ||
response.status(500); | ||
return e.getMessage(); | ||
} | ||
} |
Oops, something went wrong.