|
14 | 14 | import com.dotcms.rest.api.v1.authentication.IncorrectPasswordException; |
15 | 15 | import com.dotcms.rest.api.v1.authentication.ResponseUtil; |
16 | 16 | import com.dotcms.rest.api.v1.site.ResponseSiteVariablesEntityView; |
| 17 | +import com.dotcms.rest.api.v1.workflow.BulkActionsResultView; |
17 | 18 | import com.dotcms.rest.exception.BadRequestException; |
18 | 19 | import com.dotcms.rest.exception.ForbiddenException; |
19 | 20 | import com.dotcms.rest.exception.mapper.ExceptionMapperUtil; |
|
65 | 66 | import javax.ws.rs.DefaultValue; |
66 | 67 | import javax.ws.rs.GET; |
67 | 68 | import javax.ws.rs.NotFoundException; |
| 69 | +import javax.ws.rs.PATCH; |
68 | 70 | import javax.ws.rs.POST; |
69 | 71 | import javax.ws.rs.PUT; |
70 | 72 | import javax.ws.rs.Path; |
@@ -865,6 +867,175 @@ public final Response udpate(@Context final HttpServletRequest httpServletReques |
865 | 867 | throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users"); |
866 | 868 | } // create. |
867 | 869 |
|
| 870 | + /** |
| 871 | + * Activate an existing user. |
| 872 | + * |
| 873 | + * Only Admin User or have access to Users and Roles Portlets can update an existing user |
| 874 | + * |
| 875 | + * @param httpServletRequest |
| 876 | + * @return User Updated |
| 877 | + * @throws Exception |
| 878 | + */ |
| 879 | + @Operation(summary = "Active an existing user.", |
| 880 | + responses = { |
| 881 | + @ApiResponse( |
| 882 | + responseCode = "200", |
| 883 | + content = @Content(mediaType = "application/json", |
| 884 | + schema = @Schema(implementation = |
| 885 | + ResponseUserMapEntityView.class)), |
| 886 | + description = "If success returns a map with the user + user id."), |
| 887 | + @ApiResponse( |
| 888 | + responseCode = "403", |
| 889 | + content = @Content(mediaType = "application/json", |
| 890 | + schema = @Schema(implementation = |
| 891 | + ResponseUserMapEntityView.class)), |
| 892 | + description = "If the user is not an admin or access to the role + user layouts or does have permission, it will return a 403."), |
| 893 | + @ApiResponse( |
| 894 | + responseCode = "404", |
| 895 | + content = @Content(mediaType = "application/json", |
| 896 | + schema = @Schema(implementation = |
| 897 | + ResponseUserMapEntityView.class)), |
| 898 | + description = "If the user to update does not exist"), |
| 899 | + @ApiResponse( |
| 900 | + responseCode = "400", |
| 901 | + content = @Content(mediaType = "application/json", |
| 902 | + schema = @Schema(implementation = |
| 903 | + ResponseUserMapEntityView.class)), |
| 904 | + description = "If the user information is not valid"), |
| 905 | + }) |
| 906 | + @PATCH |
| 907 | + @Path("/activate/{userId}") |
| 908 | + @JSONP |
| 909 | + @NoCache |
| 910 | + @Produces({MediaType.APPLICATION_JSON, "application/javascript"}) |
| 911 | + public final ResponseUserMapEntityView active(@Context final HttpServletRequest httpServletRequest, |
| 912 | + @Context final HttpServletResponse httpServletResponse, |
| 913 | + @PathParam("userId") @Parameter( |
| 914 | + required = true, |
| 915 | + description = "Identifier of an user.\n\n" + |
| 916 | + "Example value: `b9d89c80-3d88-4311-8365-187323c96436` ", |
| 917 | + schema = @Schema(type = "string")) |
| 918 | + final String userId) |
| 919 | + throws Exception { |
| 920 | + |
| 921 | + final User modUser = new WebResource.InitBuilder(webResource) |
| 922 | + .requiredBackendUser(true) |
| 923 | + .requiredFrontendUser(false) |
| 924 | + .requestAndResponse(httpServletRequest, httpServletResponse) |
| 925 | + .rejectWhenNoUser(true) |
| 926 | + .init().getUser(); |
| 927 | + |
| 928 | + Logger.debug(this, ()-> "Activating user: " + modUser.getUserId()); |
| 929 | + |
| 930 | + final boolean isRoleAdministrator = modUser.isAdmin() || |
| 931 | + ( |
| 932 | + APILocator.getLayoutAPI().doesUserHaveAccessToPortlet(PortletID.ROLES.toString(), modUser) && |
| 933 | + APILocator.getLayoutAPI().doesUserHaveAccessToPortlet(PortletID.USERS.toString(), modUser) |
| 934 | + ); |
| 935 | + |
| 936 | + if (isRoleAdministrator) { |
| 937 | + |
| 938 | + final User userToUpdated = this.userAPI.loadUserById(userId); |
| 939 | + if (Objects.isNull(userToUpdated)) { |
| 940 | + |
| 941 | + throw new NoSuchUserException("User with id " + userId + " does not exist"); |
| 942 | + } |
| 943 | + |
| 944 | + userToUpdated.setActive(true); |
| 945 | + this.userAPI.save(userToUpdated, modUser, false); |
| 946 | + |
| 947 | + return new ResponseUserMapEntityView(Map.of(USER_ID, userToUpdated.getUserId(), |
| 948 | + "user", userToUpdated.toMap())); // 200 |
| 949 | + } |
| 950 | + |
| 951 | + throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users"); |
| 952 | + } // active. |
| 953 | + |
| 954 | + /** |
| 955 | + * Deactivate an existing user. |
| 956 | + * |
| 957 | + * Only Admin User or have access to Users and Roles Portlets can update an existing user |
| 958 | + * |
| 959 | + * @param httpServletRequest |
| 960 | + * @return User Updated |
| 961 | + * @throws Exception |
| 962 | + */ |
| 963 | + @Operation(summary = "Deactivate an existing user.", |
| 964 | + responses = { |
| 965 | + @ApiResponse( |
| 966 | + responseCode = "200", |
| 967 | + content = @Content(mediaType = "application/json", |
| 968 | + schema = @Schema(implementation = |
| 969 | + ResponseUserMapEntityView.class)), |
| 970 | + description = "If success returns a map with the user + user id."), |
| 971 | + @ApiResponse( |
| 972 | + responseCode = "403", |
| 973 | + content = @Content(mediaType = "application/json", |
| 974 | + schema = @Schema(implementation = |
| 975 | + ResponseUserMapEntityView.class)), |
| 976 | + description = "If the user is not an admin or access to the role + user layouts or does have permission, it will return a 403."), |
| 977 | + @ApiResponse( |
| 978 | + responseCode = "404", |
| 979 | + content = @Content(mediaType = "application/json", |
| 980 | + schema = @Schema(implementation = |
| 981 | + ResponseUserMapEntityView.class)), |
| 982 | + description = "If the user to update does not exist"), |
| 983 | + @ApiResponse( |
| 984 | + responseCode = "400", |
| 985 | + content = @Content(mediaType = "application/json", |
| 986 | + schema = @Schema(implementation = |
| 987 | + ResponseUserMapEntityView.class)), |
| 988 | + description = "If the user information is not valid"), |
| 989 | + }) |
| 990 | + @PATCH |
| 991 | + @Path("/deactivate/{userId}") |
| 992 | + @JSONP |
| 993 | + @NoCache |
| 994 | + @Produces({MediaType.APPLICATION_JSON, "application/javascript"}) |
| 995 | + public final ResponseUserMapEntityView deactivate(@Context final HttpServletRequest httpServletRequest, |
| 996 | + @Context final HttpServletResponse httpServletResponse, |
| 997 | + @PathParam("userId") @Parameter( |
| 998 | + required = true, |
| 999 | + description = "Identifier of an user.\n\n" + |
| 1000 | + "Example value: `b9d89c80-3d88-4311-8365-187323c96436` ", |
| 1001 | + schema = @Schema(type = "string")) |
| 1002 | + final String userId) |
| 1003 | + throws Exception { |
| 1004 | + |
| 1005 | + final User modUser = new WebResource.InitBuilder(webResource) |
| 1006 | + .requiredBackendUser(true) |
| 1007 | + .requiredFrontendUser(false) |
| 1008 | + .requestAndResponse(httpServletRequest, httpServletResponse) |
| 1009 | + .rejectWhenNoUser(true) |
| 1010 | + .init().getUser(); |
| 1011 | + |
| 1012 | + Logger.debug(this, ()-> "Deactivating user: " + modUser.getUserId()); |
| 1013 | + |
| 1014 | + final boolean isRoleAdministrator = modUser.isAdmin() || |
| 1015 | + ( |
| 1016 | + APILocator.getLayoutAPI().doesUserHaveAccessToPortlet(PortletID.ROLES.toString(), modUser) && |
| 1017 | + APILocator.getLayoutAPI().doesUserHaveAccessToPortlet(PortletID.USERS.toString(), modUser) |
| 1018 | + ); |
| 1019 | + |
| 1020 | + if (isRoleAdministrator) { |
| 1021 | + |
| 1022 | + final User userToUpdated = this.userAPI.loadUserById(userId); |
| 1023 | + if (Objects.isNull(userToUpdated)) { |
| 1024 | + |
| 1025 | + throw new NoSuchUserException("User with id " + userId + " does not exist"); |
| 1026 | + } |
| 1027 | + |
| 1028 | + userToUpdated.setActive(false); |
| 1029 | + this.userAPI.save(userToUpdated, modUser, false); |
| 1030 | + |
| 1031 | + return new ResponseUserMapEntityView(Map.of(USER_ID, userToUpdated.getUserId(), |
| 1032 | + "user", userToUpdated.toMap())); // 200 |
| 1033 | + } |
| 1034 | + |
| 1035 | + throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users"); |
| 1036 | + } // deactivate. |
| 1037 | + |
| 1038 | + |
868 | 1039 | @WrapInTransaction |
869 | 1040 | private User updateUser(final User modUser, final HttpServletRequest request, |
870 | 1041 | final UserForm updateUserForm) throws DotDataException, DotSecurityException, |
@@ -1068,5 +1239,5 @@ public final void delete(@Context final HttpServletRequest httpServletRequest, |
1068 | 1239 |
|
1069 | 1240 | throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users"); |
1070 | 1241 | } |
1071 | | - } // active. |
| 1242 | + } // delete. |
1072 | 1243 | } |
0 commit comments