Skip to content

Commit 302b81c

Browse files
authored
dotCMS#32129 deactive active user (dotCMS#32206)
Allowing to activate/deactiver users by rest
1 parent 2b0a382 commit 302b81c

File tree

4 files changed

+654
-5
lines changed

4 files changed

+654
-5
lines changed

dotCMS/src/main/java/com/dotcms/rest/api/v1/user/ResponseUserDeletedEntityView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.dotcms.rest.ResponseEntityView;
44

55
/**
6-
* Returns the response entity view for an user deleted view
6+
* Returns a map such as
7+
* userID -> {userId}
8+
* user -> {userMap}
79
*/
8-
public class ResponseUserDeletedEntityView extends ResponseEntityView <UserDeletedView> {
10+
public class ResponseUserDeletedEntityView extends ResponseEntityView<UserDeletedView> {
911
public ResponseUserDeletedEntityView(final UserDeletedView entity) {
1012
super(entity);
1113
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.dotcms.rest.api.v1.user;
2+
3+
import com.dotcms.rest.ResponseEntityView;
4+
5+
import java.util.Map;
6+
7+
/**
8+
* Returns a map such as
9+
* userID -> {userId}
10+
* user -> {userMap}
11+
*/
12+
public class ResponseUserMapEntityView extends ResponseEntityView <Map<String, Object>> {
13+
public ResponseUserMapEntityView(Map<String, Object> entity) {
14+
super(entity);
15+
}
16+
}

dotCMS/src/main/java/com/dotcms/rest/api/v1/user/UserResource.java

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.dotcms.rest.api.v1.authentication.IncorrectPasswordException;
1515
import com.dotcms.rest.api.v1.authentication.ResponseUtil;
1616
import com.dotcms.rest.api.v1.site.ResponseSiteVariablesEntityView;
17+
import com.dotcms.rest.api.v1.workflow.BulkActionsResultView;
1718
import com.dotcms.rest.exception.BadRequestException;
1819
import com.dotcms.rest.exception.ForbiddenException;
1920
import com.dotcms.rest.exception.mapper.ExceptionMapperUtil;
@@ -65,6 +66,7 @@
6566
import javax.ws.rs.DefaultValue;
6667
import javax.ws.rs.GET;
6768
import javax.ws.rs.NotFoundException;
69+
import javax.ws.rs.PATCH;
6870
import javax.ws.rs.POST;
6971
import javax.ws.rs.PUT;
7072
import javax.ws.rs.Path;
@@ -865,6 +867,175 @@ public final Response udpate(@Context final HttpServletRequest httpServletReques
865867
throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users");
866868
} // create.
867869

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+
8681039
@WrapInTransaction
8691040
private User updateUser(final User modUser, final HttpServletRequest request,
8701041
final UserForm updateUserForm) throws DotDataException, DotSecurityException,
@@ -1068,5 +1239,5 @@ public final void delete(@Context final HttpServletRequest httpServletRequest,
10681239

10691240
throw new ForbiddenException(USER_MSG + modUser.getUserId() + " does not have permissions to update users");
10701241
}
1071-
} // active.
1242+
} // delete.
10721243
}

0 commit comments

Comments
 (0)