blob: 415d98dcef57003cfbe20cbd0c92df492cbea6ae [file] [log] [blame]
margaretha0b63de42017-12-20 18:48:09 +01001package de.ids_mannheim.korap.web.controller;
2
3import java.util.List;
4
margaretha9d3eb042017-12-22 11:02:30 +01005import javax.ws.rs.Consumes;
margaretha45667922018-01-25 21:23:03 +01006import javax.ws.rs.DELETE;
margaretha9d3eb042017-12-22 11:02:30 +01007import javax.ws.rs.FormParam;
margaretha0b63de42017-12-20 18:48:09 +01008import javax.ws.rs.GET;
margaretha9d3eb042017-12-22 11:02:30 +01009import javax.ws.rs.POST;
margarethaca7cff82019-11-12 12:06:37 +010010import javax.ws.rs.PUT;
margaretha0b63de42017-12-20 18:48:09 +010011import javax.ws.rs.Path;
margaretha44573832018-03-21 16:59:59 +010012import javax.ws.rs.PathParam;
margaretha0b63de42017-12-20 18:48:09 +010013import javax.ws.rs.Produces;
margaretha45667922018-01-25 21:23:03 +010014import javax.ws.rs.QueryParam;
margaretha0b63de42017-12-20 18:48:09 +010015import javax.ws.rs.core.Context;
16import javax.ws.rs.core.MediaType;
17import javax.ws.rs.core.Response;
18import javax.ws.rs.core.SecurityContext;
19
margarethaca7cff82019-11-12 12:06:37 +010020import org.apache.http.HttpStatus;
margaretha0b63de42017-12-20 18:48:09 +010021import org.springframework.beans.factory.annotation.Autowired;
22import org.springframework.stereotype.Controller;
23
24import com.sun.jersey.spi.container.ResourceFilters;
25
margaretha2df06602018-11-14 19:10:30 +010026import de.ids_mannheim.korap.constant.OAuth2Scope;
margaretha293ee032018-03-20 20:11:52 +010027import de.ids_mannheim.korap.constant.UserGroupStatus;
margaretha0b63de42017-12-20 18:48:09 +010028import de.ids_mannheim.korap.dto.UserGroupDto;
29import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha835178d2018-08-15 19:04:03 +020030import de.ids_mannheim.korap.oauth2.service.OAuth2ScopeService;
margaretha0e8f4e72018-04-05 14:11:52 +020031import de.ids_mannheim.korap.security.context.TokenContext;
margaretha0b63de42017-12-20 18:48:09 +010032import de.ids_mannheim.korap.service.UserGroupService;
margarethaf7abb362018-09-18 20:09:37 +020033import de.ids_mannheim.korap.web.KustvaktResponseHandler;
margaretha398f4722019-01-09 19:07:20 +010034import de.ids_mannheim.korap.web.filter.APIVersionFilter;
margaretha0b63de42017-12-20 18:48:09 +010035import de.ids_mannheim.korap.web.filter.AuthenticationFilter;
margaretha23aae222017-12-22 15:08:23 +010036import de.ids_mannheim.korap.web.filter.BlockingFilter;
margaretha0b63de42017-12-20 18:48:09 +010037import de.ids_mannheim.korap.web.filter.PiwikFilter;
38
margaretha45667922018-01-25 21:23:03 +010039/**
40 * UserGroupController defines web APIs related to user groups,
41 * such as creating a user group, listing groups of a user,
42 * adding members to a group and subscribing (confirming an
43 * invitation) to a group.
44 *
margaretha20f31232018-07-09 17:49:39 +020045 * These APIs are only available to logged-in users and not available
46 * via third-party apps.
margaretha45667922018-01-25 21:23:03 +010047 *
margarethaa0d4d3c2018-01-02 12:06:11 +010048 * @author margaretha
49 *
50 */
margaretha0b63de42017-12-20 18:48:09 +010051@Controller
margarethaee0cbfe2018-08-28 17:47:14 +020052@Path("{version}/group")
53@ResourceFilters({ APIVersionFilter.class, AuthenticationFilter.class,
54 BlockingFilter.class, PiwikFilter.class })
margaretha0b63de42017-12-20 18:48:09 +010055public class UserGroupController {
56
margaretha0b63de42017-12-20 18:48:09 +010057 @Autowired
margarethada3c7852018-06-14 20:35:11 +020058 private KustvaktResponseHandler kustvaktResponseHandler;
margaretha0b63de42017-12-20 18:48:09 +010059 @Autowired
60 private UserGroupService service;
margaretha835178d2018-08-15 19:04:03 +020061 @Autowired
62 private OAuth2ScopeService scopeService;
margaretha9d3eb042017-12-22 11:02:30 +010063
margaretha20f31232018-07-09 17:49:39 +020064 /**
65 * Returns all user-groups in which a user is an active or a
66 * pending member.
margarethaa18ab2b2019-11-11 12:55:26 +010067 *
margarethaca7cff82019-11-12 12:06:37 +010068 * Not suitable for system-admin, instead use
69 * {@link UserGroupController#
margaretha20f31232018-07-09 17:49:39 +020070 * getUserGroupBySystemAdmin(SecurityContext, String, UserGroupStatus)}
margarethae6c711b2018-02-06 21:55:04 +010071 *
72 * @param securityContext
73 * @return a list of user-groups
margaretha293ee032018-03-20 20:11:52 +010074 *
margarethae6c711b2018-02-06 21:55:04 +010075 */
margaretha0b63de42017-12-20 18:48:09 +010076 @GET
margaretha0e8f4e72018-04-05 14:11:52 +020077 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
margarethaca7cff82019-11-12 12:06:37 +010078 public List<UserGroupDto> listUserGroups (
margaretha0e8f4e72018-04-05 14:11:52 +020079 @Context SecurityContext securityContext) {
margaretha0b63de42017-12-20 18:48:09 +010080 TokenContext context =
81 (TokenContext) securityContext.getUserPrincipal();
82 try {
margaretha835178d2018-08-15 19:04:03 +020083 scopeService.verifyScope(context, OAuth2Scope.USER_GROUP_INFO);
margaretha3ccaeb72019-02-28 18:40:22 +010084 return service.retrieveUserGroupDto(context.getUsername());
margaretha0b63de42017-12-20 18:48:09 +010085 }
86 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +020087 throw kustvaktResponseHandler.throwit(e);
margaretha0b63de42017-12-20 18:48:09 +010088 }
margaretha0b63de42017-12-20 18:48:09 +010089 }
margaretha9d3eb042017-12-22 11:02:30 +010090
margaretha20f31232018-07-09 17:49:39 +020091 /**
margarethaca7cff82019-11-12 12:06:37 +010092 * Lists user-groups for system-admin purposes. If username is
93 * specified, lists user-groups of the given user, otherwise list
94 * user-groups of all users. If status specified, list only
95 * user-groups with the given status, otherwise list user-groups
96 * regardless of their status.
margaretha293ee032018-03-20 20:11:52 +010097 *
98 * @param securityContext
margaretha20f31232018-07-09 17:49:39 +020099 * @param username
margarethaca7cff82019-11-12 12:06:37 +0100100 * a username
margaretha20f31232018-07-09 17:49:39 +0200101 * @param status
102 * {@link UserGroupStatus}
margaretha293ee032018-03-20 20:11:52 +0100103 * @return a list of user-groups
104 */
105 @GET
106 @Path("list/system-admin")
margaretha0e8f4e72018-04-05 14:11:52 +0200107 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
108 public List<UserGroupDto> getUserGroupBySystemAdmin (
margaretha293ee032018-03-20 20:11:52 +0100109 @Context SecurityContext securityContext,
110 @QueryParam("username") String username,
111 @QueryParam("status") UserGroupStatus status) {
112 TokenContext context =
113 (TokenContext) securityContext.getUserPrincipal();
114 try {
margaretha835178d2018-08-15 19:04:03 +0200115 scopeService.verifyScope(context, OAuth2Scope.ADMIN);
margaretha0e8f4e72018-04-05 14:11:52 +0200116 return service.retrieveUserGroupByStatus(username,
117 context.getUsername(), status);
margaretha293ee032018-03-20 20:11:52 +0100118 }
119 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200120 throw kustvaktResponseHandler.throwit(e);
margaretha293ee032018-03-20 20:11:52 +0100121 }
122 }
margaretha9d3eb042017-12-22 11:02:30 +0100123
margaretha20f31232018-07-09 17:49:39 +0200124 /**
margarethaa18ab2b2019-11-11 12:55:26 +0100125 * Retrieves a specific user-group. Only system admins are
126 * allowed.
margaretha44573832018-03-21 16:59:59 +0100127 *
128 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100129 * @param groupName
130 * group name
margaretha44573832018-03-21 16:59:59 +0100131 * @return a user-group
132 */
133 @GET
margaretha03b195a2019-11-12 14:57:15 +0100134 @Path("@{groupName}")
margaretha0e8f4e72018-04-05 14:11:52 +0200135 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
margarethaa18ab2b2019-11-11 12:55:26 +0100136 public UserGroupDto retrieveUserGroup (
margaretha0e8f4e72018-04-05 14:11:52 +0200137 @Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100138 @PathParam("groupName") String groupName) {
margaretha44573832018-03-21 16:59:59 +0100139 TokenContext context =
140 (TokenContext) securityContext.getUserPrincipal();
141 try {
margaretha835178d2018-08-15 19:04:03 +0200142 scopeService.verifyScope(context, OAuth2Scope.ADMIN);
margarethaa18ab2b2019-11-11 12:55:26 +0100143 return service.searchByName(context.getUsername(), groupName);
margaretha44573832018-03-21 16:59:59 +0100144 }
145 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200146 throw kustvaktResponseHandler.throwit(e);
margaretha44573832018-03-21 16:59:59 +0100147 }
margaretha0e8f4e72018-04-05 14:11:52 +0200148
margaretha44573832018-03-21 16:59:59 +0100149 }
margaretha18533fd2018-03-28 16:01:06 +0200150
margaretha20f31232018-07-09 17:49:39 +0200151 /**
margarethad8aa1352019-12-19 11:04:41 +0100152 * Creates a user group with the group owner as the only group
153 * member. The group owner is the authenticated user in the token
154 * context.
margaretha9d3eb042017-12-22 11:02:30 +0100155 *
156 * @param securityContext
margarethad8aa1352019-12-19 11:04:41 +0100157 * @param groupName the name of the group
margarethaca7cff82019-11-12 12:06:37 +0100158 * @return if a new group created, HTTP response status 201
159 * Created, otherwise 204 No Content.
margaretha9d3eb042017-12-22 11:02:30 +0100160 */
margarethaca7cff82019-11-12 12:06:37 +0100161 @PUT
margaretha03b195a2019-11-12 14:57:15 +0100162 @Path("@{groupName}")
margarethaca7cff82019-11-12 12:06:37 +0100163 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
margarethad8aa1352019-12-19 11:04:41 +0100164 public Response createUpdateUserGroup (@Context SecurityContext securityContext,
margarethaca7cff82019-11-12 12:06:37 +0100165 @PathParam("groupName") String groupName,
margarethad8aa1352019-12-19 11:04:41 +0100166 @FormParam("description") String description) {
margaretha9d3eb042017-12-22 11:02:30 +0100167 TokenContext context =
168 (TokenContext) securityContext.getUserPrincipal();
169 try {
margaretha835178d2018-08-15 19:04:03 +0200170 scopeService.verifyScope(context, OAuth2Scope.CREATE_USER_GROUP);
margarethad8aa1352019-12-19 11:04:41 +0100171 boolean groupExists = service.createUpdateUserGroup(groupName,
172 description, context.getUsername());
margarethaca7cff82019-11-12 12:06:37 +0100173 if (groupExists) {
174 return Response.noContent().build();
175 }
176 else {
177 return Response.status(HttpStatus.SC_CREATED).build();
178 }
margaretha9d3eb042017-12-22 11:02:30 +0100179 }
180 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200181 throw kustvaktResponseHandler.throwit(e);
margaretha9d3eb042017-12-22 11:02:30 +0100182 }
margaretha9d3eb042017-12-22 11:02:30 +0100183 }
184
margaretha20f31232018-07-09 17:49:39 +0200185 /**
margarethaca7cff82019-11-12 12:06:37 +0100186 * Deletes a user-group specified by the group name. Only group
margaretha835178d2018-08-15 19:04:03 +0200187 * owner and system admins can delete groups.
margaretha2c019fa2018-02-01 19:50:51 +0100188 *
189 * @param securityContext
margarethaca7cff82019-11-12 12:06:37 +0100190 * @param groupName
191 * the name of the group to delete
margaretha2c019fa2018-02-01 19:50:51 +0100192 * @return HTTP 200, if successful.
193 */
margaretha45667922018-01-25 21:23:03 +0100194 @DELETE
margaretha03b195a2019-11-12 14:57:15 +0100195 @Path("@{groupName}")
margarethaa18ab2b2019-11-11 12:55:26 +0100196 public Response deleteUserGroup (@Context SecurityContext securityContext,
margaretha39cec602019-02-05 19:48:49 +0100197 @PathParam("groupName") String groupName) {
198 TokenContext context =
199 (TokenContext) securityContext.getUserPrincipal();
200 try {
201 scopeService.verifyScope(context, OAuth2Scope.DELETE_USER_GROUP);
202 service.deleteGroup(groupName, context.getUsername());
203 return Response.ok().build();
204 }
205 catch (KustvaktException e) {
206 throw kustvaktResponseHandler.throwit(e);
207 }
208 }
margarethae6c711b2018-02-06 21:55:04 +0100209
margaretha20f31232018-07-09 17:49:39 +0200210 /**
margarethaca7cff82019-11-12 12:06:37 +0100211 * Removes a user-group member. Group owner cannot be removed.
margarethaa18ab2b2019-11-11 12:55:26 +0100212 * Only group admins, system admins and the member himself can
margarethaca7cff82019-11-12 12:06:37 +0100213 * remove a member.
margaretha45667922018-01-25 21:23:03 +0100214 *
215 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100216 * @param memberUsername
margaretha20f31232018-07-09 17:49:39 +0200217 * a username of a group member
margarethaa18ab2b2019-11-11 12:55:26 +0100218 * @param groupName
219 * a group name
margaretha45667922018-01-25 21:23:03 +0100220 * @return if successful, HTTP response status OK
221 */
222 @DELETE
margaretha03b195a2019-11-12 14:57:15 +0100223 @Path("@{groupName}/~{memberUsername}")
margarethaa18ab2b2019-11-11 12:55:26 +0100224 public Response removeUserFromGroup (
margaretha45667922018-01-25 21:23:03 +0100225 @Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100226 @PathParam("memberUsername") String memberUsername,
227 @PathParam("groupName") String groupName) {
margaretha45667922018-01-25 21:23:03 +0100228 TokenContext context =
229 (TokenContext) securityContext.getUserPrincipal();
230 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200231 scopeService.verifyScope(context,
232 OAuth2Scope.DELETE_USER_GROUP_MEMBER);
margarethaa18ab2b2019-11-11 12:55:26 +0100233 service.deleteGroupMember(memberUsername, groupName,
234 context.getUsername());
margaretha45667922018-01-25 21:23:03 +0100235 return Response.ok().build();
236 }
237 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200238 throw kustvaktResponseHandler.throwit(e);
margaretha45667922018-01-25 21:23:03 +0100239 }
240 }
241
margaretha20f31232018-07-09 17:49:39 +0200242 /**
margarethaca7cff82019-11-12 12:06:37 +0100243 * Invites users to join a user-group specified by the
margarethad8aa1352019-12-19 11:04:41 +0100244 * groupName. Only user-group admins and system admins are
245 * allowed to use this service.
246 *
247 * The invited users are added as group members with status
248 * GroupMemberStatus.PENDING.
249 *
250 * If a user accepts the invitation by using the service:
251 * {@link UserGroupController#subscribeToGroup(SecurityContext, String)},
252 * his GroupMemberStatus will be updated to
253 * GroupMemberStatus.ACTIVE.
254 *
255 * If a user rejects the invitation by using the service:
256 * {@link UserGroupController#unsubscribeFromGroup(SecurityContext, String)},
257 * his GroupMemberStatus will be updated to
258 * GroupMemberStatus.DELETED.
margaretha31a9f522018-04-03 20:40:45 +0200259 *
margaretha18533fd2018-03-28 16:01:06 +0200260 * @param securityContext
margarethaca7cff82019-11-12 12:06:37 +0100261 * @param members
262 * usernames separated by comma
margaretha18533fd2018-03-28 16:01:06 +0200263 * @return if successful, HTTP response status OK
264 */
margarethab874ef52018-01-23 20:26:31 +0100265 @POST
margaretha03b195a2019-11-12 14:57:15 +0100266 @Path("@{groupName}/invite")
margarethaca7cff82019-11-12 12:06:37 +0100267 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
margaretha4edc70e2018-03-14 22:34:29 +0100268 public Response inviteGroupMembers (
margarethaa18ab2b2019-11-11 12:55:26 +0100269 @Context SecurityContext securityContext,
270 @PathParam("groupName") String groupName,
margarethaca7cff82019-11-12 12:06:37 +0100271 @FormParam("members") String members) {
margarethab874ef52018-01-23 20:26:31 +0100272 TokenContext context =
273 (TokenContext) securityContext.getUserPrincipal();
274 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200275 scopeService.verifyScope(context,
276 OAuth2Scope.ADD_USER_GROUP_MEMBER);
margarethaca7cff82019-11-12 12:06:37 +0100277 service.inviteGroupMembers(groupName, members,
278 context.getUsername());
margarethac9f40e22019-08-07 17:32:19 +0200279 return Response.ok("SUCCESS").build();
margarethab874ef52018-01-23 20:26:31 +0100280 }
281 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200282 throw kustvaktResponseHandler.throwit(e);
margarethab874ef52018-01-23 20:26:31 +0100283 }
284 }
margaretha9d3eb042017-12-22 11:02:30 +0100285
margarethada5a6ab2019-11-08 10:06:05 +0100286 /**
287 * Very similar to addMemberRoles web-service, but allows deletion
margaretha0e1fc552019-08-08 15:31:01 +0200288 * as well.
289 *
290 * @param securityContext
margarethad8aa1352019-12-19 11:04:41 +0100291 * @param groupName
292 * the group name
293 * @param memberUsername
294 * the username of a group-member
margarethad575bd92021-06-14 09:42:18 +0200295 * @param roleId
296 * a role id or multiple role ids
margaretha0e1fc552019-08-08 15:31:01 +0200297 * @return
298 */
margarethaf7abb362018-09-18 20:09:37 +0200299 @POST
margaretha03b195a2019-11-12 14:57:15 +0100300 @Path("@{groupName}/role/edit")
margarethaf7abb362018-09-18 20:09:37 +0200301 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
302 public Response editMemberRoles (@Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100303 @PathParam("groupName") String groupName,
margarethaf7abb362018-09-18 20:09:37 +0200304 @FormParam("memberUsername") String memberUsername,
margarethad575bd92021-06-14 09:42:18 +0200305 @FormParam("roleId") List<Integer> roleIds) {
margarethaf7abb362018-09-18 20:09:37 +0200306 TokenContext context =
307 (TokenContext) securityContext.getUserPrincipal();
308 try {
309 scopeService.verifyScope(context,
310 OAuth2Scope.EDIT_USER_GROUP_MEMBER_ROLE);
margarethaa18ab2b2019-11-11 12:55:26 +0100311 service.editMemberRoles(context.getUsername(), groupName,
margarethaf7abb362018-09-18 20:09:37 +0200312 memberUsername, roleIds);
margarethac9f40e22019-08-07 17:32:19 +0200313 return Response.ok("SUCCESS").build();
margarethaf7abb362018-09-18 20:09:37 +0200314 }
315 catch (KustvaktException e) {
316 throw kustvaktResponseHandler.throwit(e);
317 }
318 }
319
margaretha20f31232018-07-09 17:49:39 +0200320 /**
321 * Adds roles of an active member of a user-group. Only user-group
margaretha0e1fc552019-08-08 15:31:01 +0200322 * admins and system admins are allowed.
margaretha18533fd2018-03-28 16:01:06 +0200323 *
324 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100325 * @param groupName
326 * a group name
margaretha20f31232018-07-09 17:49:39 +0200327 * @param memberUsername
margarethaa18ab2b2019-11-11 12:55:26 +0100328 * a username of a group member
margarethad575bd92021-06-14 09:42:18 +0200329 * @param roleId
330 * a role id or multiple role ids
margaretha18533fd2018-03-28 16:01:06 +0200331 * @return if successful, HTTP response status OK
332 */
333 @POST
margaretha03b195a2019-11-12 14:57:15 +0100334 @Path("@{groupName}/role/add")
margaretha18533fd2018-03-28 16:01:06 +0200335 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
336 public Response addMemberRoles (@Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100337 @PathParam("groupName") String groupName,
margaretha18533fd2018-03-28 16:01:06 +0200338 @FormParam("memberUsername") String memberUsername,
margarethad575bd92021-06-14 09:42:18 +0200339 @FormParam("roleId") List<Integer> roleIds) {
margaretha18533fd2018-03-28 16:01:06 +0200340 TokenContext context =
341 (TokenContext) securityContext.getUserPrincipal();
342 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200343 scopeService.verifyScope(context,
344 OAuth2Scope.ADD_USER_GROUP_MEMBER_ROLE);
margarethaa18ab2b2019-11-11 12:55:26 +0100345 service.addMemberRoles(context.getUsername(), groupName,
margaretha18533fd2018-03-28 16:01:06 +0200346 memberUsername, roleIds);
margarethac9f40e22019-08-07 17:32:19 +0200347 return Response.ok("SUCCESS").build();
margaretha18533fd2018-03-28 16:01:06 +0200348 }
349 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200350 throw kustvaktResponseHandler.throwit(e);
margaretha18533fd2018-03-28 16:01:06 +0200351 }
352 }
353
margaretha20f31232018-07-09 17:49:39 +0200354 /**
margarethaa18ab2b2019-11-11 12:55:26 +0100355 * Updates the roles of a member of a user-group by removing the
margarethaca7cff82019-11-12 12:06:37 +0100356 * given roles. Only user-group admins and system admins are
357 * allowed.
margaretha18533fd2018-03-28 16:01:06 +0200358 *
359 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100360 * @param groupName
361 * a group name
margaretha20f31232018-07-09 17:49:39 +0200362 * @param memberUsername
margarethaa18ab2b2019-11-11 12:55:26 +0100363 * a username of a group member
margarethad575bd92021-06-14 09:42:18 +0200364 * @param roleId
365 * a role id or multiple role ids
margaretha18533fd2018-03-28 16:01:06 +0200366 * @return if successful, HTTP response status OK
367 */
368 @POST
margaretha03b195a2019-11-12 14:57:15 +0100369 @Path("@{groupName}/role/delete")
margaretha18533fd2018-03-28 16:01:06 +0200370 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
371 public Response deleteMemberRoles (@Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100372 @PathParam("groupName") String groupName,
margaretha18533fd2018-03-28 16:01:06 +0200373 @FormParam("memberUsername") String memberUsername,
margarethad575bd92021-06-14 09:42:18 +0200374 @FormParam("roleId") List<Integer> roleIds) {
margaretha18533fd2018-03-28 16:01:06 +0200375 TokenContext context =
376 (TokenContext) securityContext.getUserPrincipal();
377 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200378 scopeService.verifyScope(context,
379 OAuth2Scope.DELETE_USER_GROUP_MEMBER_ROLE);
margarethaa18ab2b2019-11-11 12:55:26 +0100380 service.deleteMemberRoles(context.getUsername(), groupName,
margaretha18533fd2018-03-28 16:01:06 +0200381 memberUsername, roleIds);
margarethac9f40e22019-08-07 17:32:19 +0200382 return Response.ok("SUCCESS").build();
margaretha18533fd2018-03-28 16:01:06 +0200383 }
384 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200385 throw kustvaktResponseHandler.throwit(e);
margaretha18533fd2018-03-28 16:01:06 +0200386 }
387 }
388
margaretha20f31232018-07-09 17:49:39 +0200389 /**
390 * Handles requests to accept membership invitation. Only invited
margarethada5a6ab2019-11-08 10:06:05 +0100391 * users can subscribe to the corresponding user-group.
margaretha18533fd2018-03-28 16:01:06 +0200392 *
393 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100394 * @param groupName
395 * a group name
margaretha18533fd2018-03-28 16:01:06 +0200396 * @return if successful, HTTP response status OK
397 */
margaretha9d3eb042017-12-22 11:02:30 +0100398 @POST
margaretha03b195a2019-11-12 14:57:15 +0100399 @Path("@{groupName}/subscribe")
margaretha9d3eb042017-12-22 11:02:30 +0100400 public Response subscribeToGroup (@Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100401 @PathParam("groupName") String groupName) {
margaretha9d3eb042017-12-22 11:02:30 +0100402 TokenContext context =
403 (TokenContext) securityContext.getUserPrincipal();
404 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200405 scopeService.verifyScope(context,
406 OAuth2Scope.ADD_USER_GROUP_MEMBER);
margarethaa18ab2b2019-11-11 12:55:26 +0100407 service.acceptInvitation(groupName, context.getUsername());
margarethac9f40e22019-08-07 17:32:19 +0200408 return Response.ok("SUCCESS").build();
margaretha9d3eb042017-12-22 11:02:30 +0100409 }
410 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200411 throw kustvaktResponseHandler.throwit(e);
margaretha9d3eb042017-12-22 11:02:30 +0100412 }
margaretha9d3eb042017-12-22 11:02:30 +0100413 }
414
margaretha20f31232018-07-09 17:49:39 +0200415 /**
416 * Handles requests to reject membership invitation. A member can
margarethada5a6ab2019-11-08 10:06:05 +0100417 * only unsubscribe him/herself from a group.
margaretha18533fd2018-03-28 16:01:06 +0200418 *
margarethaca7cff82019-11-12 12:06:37 +0100419 * Implemented identical to
420 * {@link #removeUserFromGroup(SecurityContext, String, String)}.
margaretha18533fd2018-03-28 16:01:06 +0200421 *
422 * @param securityContext
margarethaa18ab2b2019-11-11 12:55:26 +0100423 * @param groupName
margaretha18533fd2018-03-28 16:01:06 +0200424 * @return if successful, HTTP response status OK
425 */
margarethaa18ab2b2019-11-11 12:55:26 +0100426 @DELETE
margaretha03b195a2019-11-12 14:57:15 +0100427 @Path("@{groupName}/unsubscribe")
margaretha9d3eb042017-12-22 11:02:30 +0100428 public Response unsubscribeFromGroup (
429 @Context SecurityContext securityContext,
margarethaa18ab2b2019-11-11 12:55:26 +0100430 @PathParam("groupName") String groupName) {
margaretha9d3eb042017-12-22 11:02:30 +0100431 TokenContext context =
432 (TokenContext) securityContext.getUserPrincipal();
433 try {
margarethaee0cbfe2018-08-28 17:47:14 +0200434 scopeService.verifyScope(context,
435 OAuth2Scope.DELETE_USER_GROUP_MEMBER);
margarethaa18ab2b2019-11-11 12:55:26 +0100436 service.deleteGroupMember(context.getUsername(), groupName,
margarethae6c711b2018-02-06 21:55:04 +0100437 context.getUsername());
margarethac9f40e22019-08-07 17:32:19 +0200438 return Response.ok("SUCCESS").build();
margaretha9d3eb042017-12-22 11:02:30 +0100439 }
440 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +0200441 throw kustvaktResponseHandler.throwit(e);
margaretha9d3eb042017-12-22 11:02:30 +0100442 }
margaretha9d3eb042017-12-22 11:02:30 +0100443 }
margaretha0b63de42017-12-20 18:48:09 +0100444}