-/*-\r
- * ============LICENSE_START=======================================================\r
- * SDC\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.openecomp.sdc.be.servlets;\r
-\r
-import java.io.UnsupportedEncodingException;\r
-import java.net.URLDecoder;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import javax.inject.Inject;\r
-import javax.inject.Singleton;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.ws.rs.Consumes;\r
-import javax.ws.rs.DELETE;\r
-import javax.ws.rs.GET;\r
-import javax.ws.rs.HeaderParam;\r
-import javax.ws.rs.POST;\r
-import javax.ws.rs.Path;\r
-import javax.ws.rs.PathParam;\r
-import javax.ws.rs.Produces;\r
-import javax.ws.rs.QueryParam;\r
-import javax.ws.rs.core.Context;\r
-import javax.ws.rs.core.MediaType;\r
-import javax.ws.rs.core.Response;\r
-import org.openecomp.sdc.be.config.BeEcompErrorManager;\r
-import org.openecomp.sdc.be.dao.api.ActionStatus;\r
-import org.openecomp.sdc.be.impl.ComponentsUtils;\r
-import org.openecomp.sdc.be.model.User;\r
-import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;\r
-import org.openecomp.sdc.be.user.UserBusinessLogic;\r
-import org.openecomp.sdc.common.api.Constants;\r
-import org.openecomp.sdc.common.log.wrappers.Logger;\r
-import org.openecomp.sdc.exception.ResponseFormat;\r
-import com.jcabi.aspects.Loggable;\r
-import fj.data.Either;\r
-import io.swagger.v3.oas.annotations.OpenAPIDefinition;\r
-import io.swagger.v3.oas.annotations.Operation;\r
-import io.swagger.v3.oas.annotations.Parameter;\r
-import io.swagger.v3.oas.annotations.info.Info;\r
-import io.swagger.v3.oas.annotations.media.ArraySchema;\r
-import io.swagger.v3.oas.annotations.media.Content;\r
-import io.swagger.v3.oas.annotations.media.Schema;\r
-import io.swagger.v3.oas.annotations.responses.ApiResponse;\r
-import io.swagger.v3.oas.annotations.responses.ApiResponses;\r
-\r
-@Loggable(prepend = true, value = Loggable.DEBUG, trim = false)\r
-@Path("/v1/user")\r
-@OpenAPIDefinition(info = @Info(title = "User Administration", description = "User admininstarator operations"))\r
-@Singleton\r
-public class UserAdminServlet extends BeGenericServlet {\r
-\r
- private static final String UTF_8 = "UTF-8";\r
- private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";\r
- private static final String ROLE_DELIMITER = ",";\r
- private static final Logger log = Logger.getLogger(UserAdminServlet.class);\r
- private final UserBusinessLogic userBusinessLogic;\r
-\r
- @Inject\r
- public UserAdminServlet(UserBusinessLogic userBusinessLogic,\r
- ComponentsUtils componentsUtils) {\r
- super(userBusinessLogic, componentsUtils);\r
- this.userBusinessLogic = userBusinessLogic;\r
- }\r
-\r
- /***************************************\r
- * API start\r
- *************************************************************/\r
-\r
- /* User by userId CRUD start */\r
-\r
- /////////////////////////////////////////////////////////////////////////////////////////////////////\r
- // retrieve all user details\r
- @GET\r
- @Path("/{userId}")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "retrieve user details", method = "GET",\r
- summary = "Returns user details according to userId",responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user Ok"),\r
- @ApiResponse(responseCode = "404", description = "User not found"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response get(\r
- @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,\r
- @Context final HttpServletRequest request) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug("(get) Start handle request of {}", url);\r
-\r
- try {\r
- Either<User, ActionStatus> either = userBusinessLogic.getUser(userId, false);\r
-\r
- if (either.isRight()) {\r
- return buildErrorResponse(\r
- getComponentsUtils().getResponseFormatByUserId(either.right().value(), userId));\r
- } else {\r
- if (either.left().value() != null) {\r
- return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),\r
- either.left().value());\r
- } else {\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get User");\r
- log.debug("get user failed with unexpected error: {}", e.getMessage(), e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
-\r
- @GET\r
- @Path("/{userId}/role")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "retrieve user role", summary = "Returns user role according to userId",\r
- responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user role Ok"),\r
- @ApiResponse(responseCode = "404", description = "User not found"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response getRole(\r
- @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,\r
- @Context final HttpServletRequest request) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug("(getRole) Start handle request of {}", url);\r
-\r
- try {\r
- Either<User, ActionStatus> either = userBusinessLogic.getUser(userId, false);\r
- if (either.isRight()) {\r
- return buildErrorResponse(\r
- getComponentsUtils().getResponseFormatByUserId(either.right().value(), userId));\r
- } else {\r
- if (either.left().value() != null) {\r
- String roleJson = "{ \"role\" : \"" + either.left().value().getRole() + "\" }";\r
- return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), roleJson);\r
- } else {\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get User Role");\r
- log.debug("Get user role failed with unexpected error: {}", e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
-\r
- /////////////////////////////////////////////////////////////////////////////////////////////////////\r
- // update user role\r
- @POST\r
- @Path("/{userId}/role")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "update user role", summary = "Update user role", responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Update user OK"),\r
- @ApiResponse(responseCode = "400", description = "Invalid Content."),\r
- @ApiResponse(responseCode = "403", description = "Missing information/Restricted operation"),\r
- @ApiResponse(responseCode = "404", description = "User not found"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "409", description = "User already exists"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response updateUserRole(\r
- @Parameter(description = "userId of user to get",\r
- required = true) @PathParam("userId") final String userIdUpdateUser,\r
- @Context final HttpServletRequest request,\r
- @Parameter(description = "json describe the update role", required = true) String data,\r
- @HeaderParam(value = Constants.USER_ID_HEADER) String modifierUserId) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug(START_HANDLE_REQUEST_OF, url);\r
-\r
- // get modifier id\r
- User modifier = new User();\r
- modifier.setUserId(modifierUserId);\r
- log.debug("modifier id is {}", modifierUserId);\r
-\r
- Response response = null;\r
-\r
- try {\r
- User updateInfoUser = getComponentsUtils().convertJsonToObject(data, modifier, User.class, AuditingActionEnum.UPDATE_USER).left().value();\r
- Either<User, ResponseFormat> updateUserResponse = userBusinessLogic.updateUserRole(modifier, userIdUpdateUser, updateInfoUser.getRole());\r
-\r
- if (updateUserResponse.isRight()) {\r
- log.debug("failed to update user role");\r
- response = buildErrorResponse(updateUserResponse.right().value());\r
- return response;\r
- }\r
- response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updateUserResponse.left().value());\r
- return response;\r
-\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update User Metadata");\r
- log.debug("Update User Role failed with exception", e);\r
- response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- return response;\r
-\r
- }\r
- }\r
-\r
- /* User role CRUD end */\r
-\r
- /* New user CRUD start */\r
- @POST\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "add user", method = "POST", summary = "Provision new user", responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "New user created"),\r
- @ApiResponse(responseCode = "400", description = "Invalid Content."),\r
- @ApiResponse(responseCode = "403", description = "Missing information"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "409", description = "User already exists"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response createUser(@Context final HttpServletRequest request,\r
- @Parameter(description = "json describe the user", required = true) String newUserData,\r
- @HeaderParam(value = Constants.USER_ID_HEADER) String modifierAttId) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug(START_HANDLE_REQUEST_OF, url);\r
-\r
- // get modifier id\r
- User modifier = new User();\r
- modifier.setUserId(modifierAttId);\r
- log.debug("modifier id is {}", modifierAttId);\r
-\r
- Response response = null;\r
-\r
- try {\r
- User newUserInfo = getComponentsUtils().convertJsonToObject(newUserData, modifier, User.class, AuditingActionEnum.ADD_USER).left().value();\r
- Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, newUserInfo);\r
-\r
- if (createUserResponse.isRight()) {\r
- log.debug("failed to create user");\r
- response = buildErrorResponse(createUserResponse.right().value());\r
- return response;\r
- }\r
- response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), createUserResponse.left().value());\r
- return response;\r
-\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update User Metadata");\r
- log.debug("Create User failed with exception", e);\r
- response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- return response;\r
-\r
- }\r
- }\r
-\r
- /* New user CRUD end */\r
-\r
- /* User authorization start */\r
-\r
- /////////////////////////////////////////////////////////////////////////////////////////////////////\r
- // User Authorization\r
- @GET\r
- @Path("/authorize")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
-\r
- @Operation(description = "authorize", summary = "authorize user", responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Returns user Ok"), @ApiResponse(responseCode = "403", description = "Restricted Access"), @ApiResponse(responseCode = "500", description = "Internal Server Error") })\r
- public Response authorize(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @HeaderParam("HTTP_CSP_FIRSTNAME") String firstName, @HeaderParam("HTTP_CSP_LASTNAME") String lastName,\r
- @HeaderParam("HTTP_CSP_EMAIL") String email) {\r
-\r
- try {\r
- userId = userId != null ? URLDecoder.decode(userId, UTF_8) : null;\r
- firstName = firstName != null ? URLDecoder.decode(firstName, UTF_8) : null;\r
- lastName = lastName != null ? URLDecoder.decode(lastName, UTF_8) : null;\r
- email = email != null ? URLDecoder.decode(email, UTF_8) : null;\r
- } catch (UnsupportedEncodingException e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Authorize User - decode headers");\r
- ResponseFormat errorResponseWrapper = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);\r
- log.error("#authorize - authorization decoding failed with error: ", e);\r
- return buildErrorResponse(errorResponseWrapper);\r
- }\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug(START_HANDLE_REQUEST_OF, url);\r
-\r
- User authUser = new User();\r
- authUser.setUserId(userId);\r
- authUser.setFirstName(firstName);\r
- authUser.setLastName(lastName);\r
- authUser.setEmail(email);\r
- log.debug("auth user id is {}", userId);\r
-\r
- Response response = null;\r
- try {\r
- Either<User, ResponseFormat> authorize = userBusinessLogic.authorize(authUser);\r
-\r
- if (authorize.isRight()) {\r
- log.debug("authorize user failed");\r
- response = buildErrorResponse(authorize.right().value());\r
- return response;\r
- }\r
- response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), authorize.left().value());\r
- return response;\r
-\r
- } catch (Exception e) {\r
- log.debug("authorize user failed with unexpected error: {}", e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
-\r
- /* User authorization end */\r
-\r
- @GET\r
- @Path("/admins")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "retrieve all administrators", method = "GET", summary = "Returns all administrators",\r
- responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user Ok"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response getAdminsUser(@Context final HttpServletRequest request) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug("(get) Start handle request of {}", url);\r
-\r
- try {\r
- Either<List<User>, ResponseFormat> either = userBusinessLogic.getAllAdminUsers();\r
-\r
- if (either.isRight()) {\r
- log.debug("Failed to get all admin users");\r
- return buildErrorResponse(either.right().value());\r
- } else {\r
- if (either.left().value() != null) {\r
- return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),\r
- either.left().value());\r
- } else {\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Administrators");\r
- log.debug("get all admins failed with unexpected error: {}", e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
-\r
- @GET\r
- @Path("/users")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "Retrieve the list of all active ASDC users or only group of users having specific roles.",\r
- method = "GET",\r
- summary = "Returns list of users with the specified roles, or all of users in the case of empty 'roles' header",\r
- responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns users Ok"),\r
- @ApiResponse(responseCode = "204", description = "No provisioned ASDC users of requested role"),\r
- @ApiResponse(responseCode = "403", description = "Restricted Access"),\r
- @ApiResponse(responseCode = "400", description = "Missing content"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response getUsersList(@Context final HttpServletRequest request, @Parameter(\r
- description = "Any active user's USER_ID ") @HeaderParam(Constants.USER_ID_HEADER) final String userId,\r
- @Parameter(\r
- description = "TESTER,DESIGNER,PRODUCT_STRATEGIST,OPS,PRODUCT_MANAGER,GOVERNOR, ADMIN OR all users by not typing anything") @QueryParam("roles") final String roles) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug("Start handle request of {} modifier id is {}", url, userId);\r
-\r
- List<String> rolesList = new ArrayList<>();\r
- if (roles != null && !roles.trim().isEmpty()) {\r
- String[] rolesArr = roles.split(ROLE_DELIMITER);\r
- for (String role : rolesArr) {\r
- rolesList.add(role.trim());\r
- }\r
- }\r
-\r
- try {\r
- Either<List<User>, ResponseFormat> either = userBusinessLogic.getUsersList(userId, rolesList, roles);\r
-\r
- if (either.isRight()) {\r
- log.debug("Failed to get ASDC users");\r
- return buildErrorResponse(either.right().value());\r
- } else {\r
- return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), either.left().value());\r
- }\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get ASDC users");\r
- log.debug("get users failed with unexpected error: {}", e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
-\r
- }\r
-\r
- /////////////////////////////////////////////////////////////////////////////////////////////////////\r
- // delete user\r
- @DELETE\r
- @Path("/{userId}")\r
- @Consumes(MediaType.APPLICATION_JSON)\r
- @Produces(MediaType.APPLICATION_JSON)\r
- @Operation(description = "delete user", summary = "Delete user", responses = @ApiResponse(\r
- content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))\r
- @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Update deleted OK"),\r
- @ApiResponse(responseCode = "400", description = "Invalid Content."),\r
- @ApiResponse(responseCode = "403", description = "Missing information"),\r
- @ApiResponse(responseCode = "404", description = "User not found"),\r
- @ApiResponse(responseCode = "405", description = "Method Not Allowed"),\r
- @ApiResponse(responseCode = "409", description = "Restricted operation"),\r
- @ApiResponse(responseCode = "500", description = "Internal Server Error")})\r
- public Response deActivateUser(\r
- @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,\r
- @Context final HttpServletRequest request,\r
- @HeaderParam(value = Constants.USER_ID_HEADER) String userIdHeader) {\r
-\r
- String url = request.getMethod() + " " + request.getRequestURI();\r
- log.debug("Start handle request of {} modifier id is {}", url, userIdHeader);\r
-\r
- User modifier = new User();\r
- modifier.setUserId(userIdHeader);\r
-\r
- Response response = null;\r
- try {\r
- Either<User, ResponseFormat> deactiveUserResponse = userBusinessLogic.deActivateUser(modifier, userId);\r
-\r
- if (deactiveUserResponse.isRight()) {\r
- log.debug("Failed to deactivate user");\r
- response = buildErrorResponse(deactiveUserResponse.right().value());\r
- return response;\r
- }\r
- response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), deactiveUserResponse.left().value());\r
- return response;\r
-\r
- } catch (Exception e) {\r
- BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get ASDC users");\r
- log.debug("deactivate user failed with unexpected error: {}", e);\r
- return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));\r
- }\r
- }\r
-}\r
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.be.servlets;
+
+import com.jcabi.aspects.Loggable;
+import io.swagger.v3.oas.annotations.OpenAPIDefinition;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.info.Info;
+import io.swagger.v3.oas.annotations.media.ArraySchema;
+import io.swagger.v3.oas.annotations.media.Content;
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+import io.swagger.v3.oas.annotations.responses.ApiResponses;
+import org.eclipse.jetty.http.HttpStatus;
+import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
+import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
+import org.openecomp.sdc.be.impl.ComponentsUtils;
+import org.openecomp.sdc.be.model.User;
+import org.openecomp.sdc.be.user.Role;
+import org.openecomp.sdc.be.user.UserBusinessLogic;
+import org.openecomp.sdc.be.user.UserBusinessLogicExt;
+import org.openecomp.sdc.common.api.Constants;
+import org.openecomp.sdc.common.log.wrappers.Logger;
+import org.springframework.stereotype.Controller;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.List;
+@Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
+@Path("/v1/user")
+@OpenAPIDefinition(info = @Info(title = "User Administration", description = "User admininstarator operations"))
+@Controller
+public class UserAdminServlet extends BeGenericServlet {
+
+ private static final String UTF_8 = "UTF-8";
+ private static final String ROLE_DELIMITER = ",";
+ private static final Logger log = Logger.getLogger(UserAdminServlet.class);
+ private final UserBusinessLogic userBusinessLogic;
+ private final UserBusinessLogicExt userBusinessLogicExt;
+
+ static class UserRole {
+ Role role;
+
+ public Role getRole() {
+ return role;
+ }
+
+ public void setRole(Role role) {
+ this.role = role;
+ }
+
+ }
+
+ UserAdminServlet(UserBusinessLogic userBusinessLogic,
+ ComponentsUtils componentsUtils, UserBusinessLogicExt userBusinessLogicExt) {
+ super(userBusinessLogic, componentsUtils);
+ this.userBusinessLogic = userBusinessLogic;
+ this.userBusinessLogicExt = userBusinessLogicExt;
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////////////////
+ // retrieve all user details
+ @GET
+ @Path("/{userId}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "retrieve user details", method = "GET",
+ summary = "Returns user details according to userId",responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user Ok"),
+ @ApiResponse(responseCode = "404", description = "User not found"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public User get(
+ @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,
+ @Context final HttpServletRequest request) {
+ return userBusinessLogic.getUser(userId, false);
+ }
+
+ @GET
+ @Path("/{userId}/role")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "retrieve user role", summary = "Returns user role according to userId",
+ responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user role Ok"),
+ @ApiResponse(responseCode = "404", description = "User not found"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public String getRole(
+ @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,
+ @Context final HttpServletRequest request) {
+ User user = userBusinessLogic.getUser(userId, false);
+ return "{ \"role\" : \"" + user.getRole() + "\" }";
+ }
+
+ /////////////////////////////////////////////////////////////////////////////////////////////////////
+ // update user role
+ @POST
+ @Path("/{userId}/role")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "update user role", summary = "Update user role", responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Update user OK"),
+ @ApiResponse(responseCode = "400", description = "Invalid Content."),
+ @ApiResponse(responseCode = "403", description = "Missing information/Restricted operation"),
+ @ApiResponse(responseCode = "404", description = "User not found"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "409", description = "User already exists"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public User updateUserRole(
+ @Parameter(description = "userId of user to get",
+ required = true) @PathParam("userId") final String userIdUpdateUser,
+ @Context final HttpServletRequest request,
+ @Parameter(description = "json describe the update role", required = true) UserRole newRole,
+ @HeaderParam(value = Constants.USER_ID_HEADER) String modifierUserId) {
+
+ return userBusinessLogic.updateUserRole(modifierUserId, userIdUpdateUser, newRole.getRole().name());
+ }
+
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "add user", method = "POST", summary = "Provision new user", responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "New user created"),
+ @ApiResponse(responseCode = "400", description = "Invalid Content."),
+ @ApiResponse(responseCode = "403", description = "Missing information"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "409", description = "User already exists"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ public Response createUser(@Context final HttpServletRequest request,
+ @Parameter(description = "json describe the user", required = true) User newUser,
+ @HeaderParam(value = Constants.USER_ID_HEADER) String modifierAttId) {
+
+ log.debug("modifier id is {}", modifierAttId);
+ User user = userBusinessLogic.createUser(modifierAttId, newUser);
+ return Response.status(HttpStatus.CREATED_201)
+ .entity(user)
+ .build();
+ }
+
+ @GET
+ @Path("/authorize")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+
+ @Operation(description = "authorize", summary = "authorize user", responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Returns user Ok"), @ApiResponse(responseCode = "403", description = "Restricted Access"), @ApiResponse(responseCode = "500", description = "Internal Server Error") })
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public User authorize(@HeaderParam(value = Constants.USER_ID_HEADER) String userId,
+ @HeaderParam("HTTP_CSP_FIRSTNAME") String firstName,
+ @HeaderParam("HTTP_CSP_LASTNAME") String lastName,
+ @HeaderParam("HTTP_CSP_EMAIL") String email) throws UnsupportedEncodingException {
+
+ userId = userId != null ? URLDecoder.decode(userId, UTF_8) : null;
+ firstName = firstName != null ? URLDecoder.decode(firstName, UTF_8) : null;
+ lastName = lastName != null ? URLDecoder.decode(lastName, UTF_8) : null;
+ email = email != null ? URLDecoder.decode(email, UTF_8) : null;
+
+ User authUser = new User();
+ authUser.setUserId(userId);
+ authUser.setFirstName(firstName);
+ authUser.setLastName(lastName);
+ authUser.setEmail(email);
+ return userBusinessLogic.authorize(authUser);
+ }
+
+ @GET
+ @Path("/admins")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "retrieve all administrators", method = "GET", summary = "Returns all administrators",
+ responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns user Ok"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public List<User> getAdminsUser(@Context final HttpServletRequest request) {
+ return userBusinessLogic.getAllAdminUsers();
+ }
+
+ @GET
+ @Path("/users")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "Retrieve the list of all active ASDC users or only group of users having specific roles.",
+ method = "GET",
+ summary = "Returns list of users with the specified roles, or all of users in the case of empty 'roles' header",
+ responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Returns users Ok"),
+ @ApiResponse(responseCode = "204", description = "No provisioned ASDC users of requested role"),
+ @ApiResponse(responseCode = "403", description = "Restricted Access"),
+ @ApiResponse(responseCode = "400", description = "Missing content"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ public List<User> getUsersList(@Context final HttpServletRequest request, @Parameter(
+ description = "Any active user's USER_ID ") @HeaderParam(Constants.USER_ID_HEADER) final String userId,
+ @Parameter(
+ description = "TESTER,DESIGNER,PRODUCT_STRATEGIST,OPS,PRODUCT_MANAGER,GOVERNOR, ADMIN OR all users by not typing anything") @QueryParam("roles") final String roles) {
+
+ String url = request.getMethod() + " " + request.getRequestURI();
+ log.debug("Start handle request of {} modifier id is {}", url, userId);
+
+ List<String> rolesList = new ArrayList<>();
+ if (roles != null && !roles.trim().isEmpty()) {
+ String[] rolesArr = roles.split(ROLE_DELIMITER);
+ for (String role : rolesArr) {
+ rolesList.add(role.trim());
+ }
+ }
+ return userBusinessLogic.getUsersList(userId, rolesList, roles);
+ }
+
+ @DELETE
+ @Path("/{userId}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Operation(description = "delete user", summary = "Delete user", responses = @ApiResponse(
+ content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))))
+ @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Update deleted OK"),
+ @ApiResponse(responseCode = "400", description = "Invalid Content."),
+ @ApiResponse(responseCode = "403", description = "Missing information"),
+ @ApiResponse(responseCode = "404", description = "User not found"),
+ @ApiResponse(responseCode = "405", description = "Method Not Allowed"),
+ @ApiResponse(responseCode = "409", description = "Restricted operation"),
+ @ApiResponse(responseCode = "500", description = "Internal Server Error")})
+ @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
+ public User deActivateUser(
+ @Parameter(description = "userId of user to get", required = true) @PathParam("userId") final String userId,
+ @Context final HttpServletRequest request,
+ @HeaderParam(value = Constants.USER_ID_HEADER) String modifierId) {
+ return userBusinessLogicExt.deActivateUser(modifierId, userId);
+ }
+}