1
|
package eu.dnetlib.openaire.usermanagement.api;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonObject;
|
5
|
import com.google.gson.JsonParser;
|
6
|
import eu.dnetlib.openaire.user.login.utils.AuthoritiesUpdater;
|
7
|
import eu.dnetlib.openaire.user.pojos.RoleVerification;
|
8
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
9
|
import eu.dnetlib.openaire.usermanagement.dto.Role;
|
10
|
import eu.dnetlib.openaire.usermanagement.utils.AuthorizationService;
|
11
|
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
|
12
|
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
|
13
|
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.http.HttpStatus;
|
18
|
import org.springframework.security.access.method.P;
|
19
|
import org.springframework.security.access.prepost.PreAuthorize;
|
20
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
21
|
import org.springframework.security.core.context.SecurityContextHolder;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
import org.springframework.web.bind.annotation.RequestBody;
|
24
|
|
25
|
import javax.mail.MessagingException;
|
26
|
import javax.ws.rs.*;
|
27
|
import javax.ws.rs.core.MediaType;
|
28
|
import javax.ws.rs.core.Response;
|
29
|
import java.util.Collection;
|
30
|
import java.util.HashSet;
|
31
|
import java.util.List;
|
32
|
|
33
|
@Component(value = "RegistryService")
|
34
|
@Path("/registry")
|
35
|
public class RegistryService {
|
36
|
|
37
|
private static final Logger logger = Logger.getLogger(RegistryService.class);
|
38
|
|
39
|
@Autowired
|
40
|
private RegistryCalls calls;
|
41
|
|
42
|
@Autowired
|
43
|
private JsonUtils jsonUtils;
|
44
|
|
45
|
@Autowired
|
46
|
private EmailSender emailSender;
|
47
|
|
48
|
@Autowired
|
49
|
private VerificationUtils verificationUtils;
|
50
|
|
51
|
@Autowired
|
52
|
private AuthoritiesUpdater authoritiesUpdater;
|
53
|
|
54
|
@Autowired
|
55
|
private AuthorizationService authorizationService;
|
56
|
|
57
|
/**
|
58
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
59
|
*/
|
60
|
@Path("/subscribe/{type}/{id}")
|
61
|
@POST
|
62
|
@Produces(MediaType.APPLICATION_JSON)
|
63
|
@PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
|
64
|
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
|
65
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
66
|
Integer couId = calls.getCouId(type, id);
|
67
|
if (couId != null) {
|
68
|
Integer role = calls.getRoleId(coPersonId, couId);
|
69
|
calls.assignMemberRole(coPersonId, couId, role);
|
70
|
authoritiesUpdater.update(authorizationService.getEmail(), old -> {
|
71
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
72
|
authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
73
|
return authorities;
|
74
|
});
|
75
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
76
|
} else {
|
77
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
78
|
}
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Unsubscribe from type(Community, etc.) with id(ee, egi, etc.).
|
83
|
* If user has manager role for this entity, it will be removed too.
|
84
|
*/
|
85
|
@Path("/unsubscribe/{type}/{id}")
|
86
|
@POST
|
87
|
@Produces(MediaType.APPLICATION_JSON)
|
88
|
@PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
|
89
|
public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) {
|
90
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
91
|
Integer couId = calls.getCouId(type, id);
|
92
|
if (couId != null) {
|
93
|
Integer role = calls.getRoleId(coPersonId, couId);
|
94
|
if (role != null) {
|
95
|
calls.removeAdminRole(coPersonId, couId);
|
96
|
calls.removeMemberRole(coPersonId, couId, role);
|
97
|
authoritiesUpdater.update(authorizationService.getEmail(), old -> {
|
98
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
99
|
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
|
100
|
authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
101
|
return authorities;
|
102
|
});
|
103
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
104
|
} else
|
105
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User does not have this role").toString()).type(MediaType.APPLICATION_JSON).build();
|
106
|
} else {
|
107
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Create a new role with the given name and description.
|
113
|
**/
|
114
|
@Path("/createRole")
|
115
|
@POST
|
116
|
@Produces(MediaType.APPLICATION_JSON)
|
117
|
@Consumes(MediaType.APPLICATION_JSON)
|
118
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
119
|
public Response createRole(@RequestBody Role role) {
|
120
|
if (calls.getCouId(role.getName()) == null) {
|
121
|
calls.createRole(role);
|
122
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
123
|
} else {
|
124
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build();
|
125
|
}
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
|
130
|
* Auto generated link and code will be sent as response.
|
131
|
*/
|
132
|
@Path("/invite/{type}/{id}/manager")
|
133
|
@POST
|
134
|
@Produces(MediaType.APPLICATION_JSON)
|
135
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
136
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
137
|
public Response inviteManager(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
|
138
|
Integer couId = calls.getCouId(type, id);
|
139
|
if (couId != null) {
|
140
|
JsonObject details = new JsonParser().parse(body).getAsJsonObject();
|
141
|
JsonObject email = details.get("email").getAsJsonObject();
|
142
|
String recipient = email.get("recipient").getAsString();
|
143
|
Integer coPersonId = calls.getCoPersonIdByEmail(recipient);
|
144
|
if (coPersonId == null || calls.getUserAdminGroup(coPersonId, couId) == null) {
|
145
|
JsonObject invitation = verificationUtils.createManagerInvitation(recipient, type, id);
|
146
|
return sendEmail(details, email, coPersonId, invitation);
|
147
|
} else {
|
148
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
|
149
|
}
|
150
|
} else {
|
151
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
152
|
}
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Invite user with email to be a member of a type(Community, etc.) with id(ee, egi, etc.)
|
157
|
* Auto generated link and code will be sent as response.
|
158
|
*/
|
159
|
@Path("/invite/{type}/{id}/member")
|
160
|
@POST
|
161
|
@Produces(MediaType.APPLICATION_JSON)
|
162
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
163
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
164
|
public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
|
165
|
Integer couId = calls.getCouId(type, id, false);
|
166
|
if (couId != null) {
|
167
|
JsonObject details = new JsonParser().parse(body).getAsJsonObject();
|
168
|
JsonObject email = details.get("email").getAsJsonObject();
|
169
|
String recipient = email.get("recipient").getAsString();
|
170
|
Integer coPersonId = calls.getCoPersonIdByEmail(recipient);
|
171
|
if (coPersonId == null || calls.getRoleId(coPersonId, couId) == null) {
|
172
|
JsonObject invitation = verificationUtils.createMemberInvitation(recipient, type, id);
|
173
|
return sendEmail(details, email, coPersonId, invitation);
|
174
|
} else {
|
175
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already member of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
|
176
|
}
|
177
|
} else {
|
178
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
179
|
}
|
180
|
}
|
181
|
|
182
|
private Response sendEmail(JsonObject details, JsonObject email, Integer coPersonId, JsonObject invitation) {
|
183
|
String name = (coPersonId != null) ? calls.getUserNames(coPersonId) : "User";
|
184
|
String link = details.get("link").getAsString() + invitation.get("link").getAsString();
|
185
|
String subject = email.get("subject").getAsString();
|
186
|
String message = email.get("body").getAsString().
|
187
|
replace("((__user__))", name).
|
188
|
replace("((__link__))", link).
|
189
|
replace("((__code__))", invitation.get("code").getAsString());
|
190
|
try {
|
191
|
emailSender.sendEmail(email.get("recipient").getAsString(), subject, message);
|
192
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
|
193
|
} catch (MessagingException e) {
|
194
|
logger.error(e.getMessage());
|
195
|
verificationUtils.deleteVerification(invitation.get("link").getAsString());
|
196
|
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build();
|
197
|
}
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
|
202
|
*/
|
203
|
@Path("/invite/{type}/{id}/manager/{email}")
|
204
|
@DELETE
|
205
|
@Produces(MediaType.APPLICATION_JSON)
|
206
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
207
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
208
|
public Response cancelManagerInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
|
209
|
Integer couId = calls.getCouId(type, id);
|
210
|
if (couId != null) {
|
211
|
verificationUtils.deleteManagerVerifications(email, type, id);
|
212
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
|
213
|
} else {
|
214
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
215
|
}
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* Cancel invitation to user with email for being member of a type(Community, etc.) with id(ee, egi, etc.)
|
220
|
*/
|
221
|
@Path("/invite/{type}/{id}/member/{email}")
|
222
|
@DELETE
|
223
|
@Produces(MediaType.APPLICATION_JSON)
|
224
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
225
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
226
|
public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
|
227
|
Integer couId = calls.getCouId(type, id, false);
|
228
|
if (couId != null) {
|
229
|
verificationUtils.deleteMemberVerifications(email, type, id);
|
230
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
|
231
|
} else {
|
232
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
233
|
}
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
|
238
|
*/
|
239
|
@Path("/invite/{type}/{id}/managers/")
|
240
|
@GET
|
241
|
@Produces(MediaType.APPLICATION_JSON)
|
242
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
243
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
244
|
public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
|
245
|
JsonArray invited = verificationUtils.getInvitedManagers(type, id);
|
246
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Get the invited members for a type(Community, etc.) with id(ee, egi, etc.)
|
251
|
*/
|
252
|
@Path("/invite/{type}/{id}/members/")
|
253
|
@GET
|
254
|
@Produces(MediaType.APPLICATION_JSON)
|
255
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
|
256
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
257
|
public Response getInviteMembers(@PathParam("type") String type, @PathParam("id") String id) {
|
258
|
JsonArray invited = verificationUtils.getInvitedMembers(type, id);
|
259
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* Get the verification with a specific id only if it refers to the logged in user
|
264
|
*/
|
265
|
@Path("verification/{id}")
|
266
|
@GET
|
267
|
@Produces(MediaType.APPLICATION_JSON)
|
268
|
@PreAuthorize("isAuthenticated()")
|
269
|
public Response getVerification(@PathParam("id") String id) {
|
270
|
RoleVerification verification = verificationUtils.getVerification(id);
|
271
|
if (verification != null) {
|
272
|
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
|
273
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
|
274
|
} else {
|
275
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
|
276
|
}
|
277
|
} else {
|
278
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
279
|
}
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Delete the verification with a specific id.
|
284
|
*/
|
285
|
@Path("verification/{id}")
|
286
|
@DELETE
|
287
|
@Produces(MediaType.APPLICATION_JSON)
|
288
|
@PreAuthorize("isAuthenticated() && @VerificationUtils.ownedVerification(#id)")
|
289
|
public Response deleteVerification(@PathParam("id") String id) {
|
290
|
if (verificationUtils.getVerification(id) != null) {
|
291
|
verificationUtils.deleteVerification(id);
|
292
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
|
293
|
} else {
|
294
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
|
295
|
}
|
296
|
}
|
297
|
|
298
|
/**
|
299
|
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
|
300
|
* Manager role is assigned to this user, along with the member role.
|
301
|
*/
|
302
|
@Path("verification/manager/{id}")
|
303
|
@POST
|
304
|
@Produces(MediaType.APPLICATION_JSON)
|
305
|
@PreAuthorize("isAuthenticated()")
|
306
|
public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
|
307
|
RoleVerification verification = verificationUtils.getVerification(id);
|
308
|
if (verification != null && verification.getVerificationType().equals("manager")) {
|
309
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
310
|
if (coPersonId != null) {
|
311
|
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
|
312
|
if (verification.getVerificationCode().equals(code)) {
|
313
|
Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
|
314
|
if (couId != null) {
|
315
|
Integer role = calls.getRoleId(coPersonId, couId);
|
316
|
calls.assignMemberRole(coPersonId, couId, role);
|
317
|
if (verification.getType().equals("community") || verification.getType().equals("ri")) {
|
318
|
Integer riCouId = calls.getCouId("ri", verification.getEntity(), false);
|
319
|
if (riCouId != null) {
|
320
|
calls.assignMemberRole(coPersonId, riCouId, calls.getRoleId(coPersonId, riCouId));
|
321
|
verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
|
322
|
verificationUtils.deleteMemberVerifications(verification.getEmail(), "ri", verification.getEntity());
|
323
|
} else {
|
324
|
verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
|
325
|
}
|
326
|
} else {
|
327
|
verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
|
328
|
}
|
329
|
if (calls.getUserAdminGroup(coPersonId, couId) == null) {
|
330
|
if (verification.getType().equals("community") || verification.getType().equals("ri")) {
|
331
|
verificationUtils.deleteManagerVerifications(verification.getEmail(), "community", verification.getEntity());
|
332
|
verificationUtils.deleteManagerVerifications(verification.getEmail(), "ri", verification.getEntity());
|
333
|
} else {
|
334
|
verificationUtils.deleteManagerVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
|
335
|
}
|
336
|
calls.assignAdminRole(coPersonId, couId);
|
337
|
authoritiesUpdater.update(verification.getEmail(), old -> {
|
338
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
339
|
authorities.add(new SimpleGrantedAuthority(authorizationService.member(verification.getType(), verification.getEntity())));
|
340
|
authorities.add(new SimpleGrantedAuthority(authorizationService.manager(verification.getType(), verification.getEntity())));
|
341
|
return authorities;
|
342
|
});
|
343
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
344
|
} else {
|
345
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User is already admin of this cou").toString()).type(MediaType.APPLICATION_JSON).build();
|
346
|
}
|
347
|
} else {
|
348
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
349
|
}
|
350
|
} else {
|
351
|
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
|
352
|
}
|
353
|
} else {
|
354
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
|
355
|
}
|
356
|
} else {
|
357
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
358
|
}
|
359
|
} else {
|
360
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
361
|
}
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
|
366
|
* Member role is assigned to this user, along with the member role.
|
367
|
*/
|
368
|
@Path("verification/member/{id}")
|
369
|
@POST
|
370
|
@Produces(MediaType.APPLICATION_JSON)
|
371
|
@PreAuthorize("isAuthenticated()")
|
372
|
public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
|
373
|
RoleVerification verification = verificationUtils.getVerification(id);
|
374
|
if (verification != null && verification.getVerificationType().equals("member")) {
|
375
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
376
|
if (coPersonId != null) {
|
377
|
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
|
378
|
if (verification.getVerificationCode().equals(code)) {
|
379
|
Integer couId = calls.getCouId(verification.getType(), verification.getEntity(), false);
|
380
|
if (couId != null) {
|
381
|
Integer role = calls.getRoleId(coPersonId, couId);
|
382
|
calls.assignMemberRole(coPersonId, couId, role);
|
383
|
authoritiesUpdater.update(verification.getEmail(), old -> {
|
384
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
385
|
authorities.add(new SimpleGrantedAuthority(authorizationService.member(verification.getType(), verification.getEntity())));
|
386
|
return authorities;
|
387
|
});
|
388
|
verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
|
389
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
390
|
} else {
|
391
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
392
|
}
|
393
|
} else {
|
394
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
|
395
|
}
|
396
|
} else {
|
397
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
|
398
|
}
|
399
|
} else {
|
400
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
401
|
}
|
402
|
} else {
|
403
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
404
|
}
|
405
|
}
|
406
|
|
407
|
/**
|
408
|
* Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
409
|
*/
|
410
|
@Path("/{type}/{id}/manager/{email}")
|
411
|
@DELETE
|
412
|
@Produces(MediaType.APPLICATION_JSON)
|
413
|
@Consumes(MediaType.APPLICATION_JSON)
|
414
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
415
|
public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
|
416
|
id, @PathParam("email") String email) {
|
417
|
List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
|
418
|
if (coPersonIds.size() > 0) {
|
419
|
Integer couId = calls.getCouId(type, id);
|
420
|
if (couId != null) {
|
421
|
coPersonIds.forEach(coPersonId -> {
|
422
|
calls.removeAdminRole(coPersonId, couId);
|
423
|
});
|
424
|
authoritiesUpdater.update(email, old -> {
|
425
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
426
|
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
|
427
|
return authorities;
|
428
|
});
|
429
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
430
|
} else {
|
431
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
432
|
}
|
433
|
} else {
|
434
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
435
|
}
|
436
|
}
|
437
|
|
438
|
/**
|
439
|
* Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
440
|
*/
|
441
|
@Path("/{type}/{id}/member/{email}")
|
442
|
@DELETE
|
443
|
@Produces(MediaType.APPLICATION_JSON)
|
444
|
@Consumes(MediaType.APPLICATION_JSON)
|
445
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
446
|
public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
|
447
|
id, @PathParam("email") String email) {
|
448
|
List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
|
449
|
if (coPersonIds.size() > 0) {
|
450
|
Integer couId = calls.getCouId(type, id, false);
|
451
|
if (couId != null) {
|
452
|
coPersonIds.forEach(coPersonId -> {
|
453
|
Integer role = calls.getRoleId(coPersonId, couId);
|
454
|
calls.removeAdminRole(coPersonId, couId);
|
455
|
calls.removeMemberRole(coPersonId, couId, role);
|
456
|
});
|
457
|
authoritiesUpdater.update(email, old -> {
|
458
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
459
|
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
|
460
|
authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
461
|
return authorities;
|
462
|
});
|
463
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
464
|
} else {
|
465
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
466
|
}
|
467
|
} else {
|
468
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
469
|
}
|
470
|
}
|
471
|
|
472
|
/**
|
473
|
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
474
|
*/
|
475
|
@Path("/{type}/{id}/members")
|
476
|
@GET
|
477
|
@Produces(MediaType.APPLICATION_JSON)
|
478
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
|
479
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
480
|
public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
|
481
|
Integer couId = calls.getCouId(type, id, false);
|
482
|
if (couId != null) {
|
483
|
JsonArray members = calls.getUserNamesByCouId(couId, false);
|
484
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
|
485
|
} else {
|
486
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
487
|
}
|
488
|
}
|
489
|
|
490
|
/**
|
491
|
* Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
492
|
*/
|
493
|
@Path("/{type}/{id}/members/email")
|
494
|
@GET
|
495
|
@Produces(MediaType.APPLICATION_JSON)
|
496
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
|
497
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
498
|
public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
|
499
|
Integer couId = calls.getCouId(type, id, false);
|
500
|
if (couId != null) {
|
501
|
JsonArray members = calls.getUserEmailByCouId(couId, false);
|
502
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
|
503
|
} else {
|
504
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
505
|
}
|
506
|
}
|
507
|
|
508
|
/**
|
509
|
* Get the Identifiers of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
510
|
*/
|
511
|
@Path("/{type}/{id}/members/id")
|
512
|
@GET
|
513
|
@Produces(MediaType.APPLICATION_JSON)
|
514
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
|
515
|
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
|
516
|
public Response getMembersId(@PathParam("type") String type, @PathParam("id") String id) {
|
517
|
Integer couId = calls.getCouId(type, id, false);
|
518
|
if (couId != null) {
|
519
|
JsonArray members = calls.getUserIdByCouId(couId, false);
|
520
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
|
521
|
} else {
|
522
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
523
|
}
|
524
|
}
|
525
|
|
526
|
/**
|
527
|
* Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
528
|
*/
|
529
|
@Path("/{type}/{id}/members/count")
|
530
|
@GET
|
531
|
@Produces(MediaType.APPLICATION_JSON)
|
532
|
public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
|
533
|
Integer couId = calls.getCouId(type, id, false);
|
534
|
int count = 0;
|
535
|
if (couId != null) {
|
536
|
count = calls.getUserNamesByCouId(couId, false).size();
|
537
|
}
|
538
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
|
539
|
}
|
540
|
|
541
|
/**
|
542
|
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
|
543
|
*/
|
544
|
@Path("/{type}/{id}/managers")
|
545
|
@GET
|
546
|
@Produces(MediaType.APPLICATION_JSON)
|
547
|
public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
|
548
|
Integer couId = calls.getCouId(type, id);
|
549
|
if (couId != null) {
|
550
|
JsonArray managers = calls.getUserNamesByCouId(couId, true);
|
551
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
|
552
|
} else {
|
553
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
554
|
}
|
555
|
}
|
556
|
|
557
|
/**
|
558
|
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
|
559
|
*/
|
560
|
@Path("/{type}/{id}/managers/email")
|
561
|
@GET
|
562
|
@Produces(MediaType.APPLICATION_JSON)
|
563
|
public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
|
564
|
Integer couId = calls.getCouId(type, id);
|
565
|
if (couId != null) {
|
566
|
JsonArray managers = calls.getUserEmailByCouId(couId, true);
|
567
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
|
568
|
} else {
|
569
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
570
|
}
|
571
|
}
|
572
|
|
573
|
/**
|
574
|
* Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.)
|
575
|
*/
|
576
|
@Path("/{type}/{id}/managers/id")
|
577
|
@GET
|
578
|
@Produces(MediaType.APPLICATION_JSON)
|
579
|
public Response getManagersId(@PathParam("type") String type, @PathParam("id") String id) {
|
580
|
Integer couId = calls.getCouId(type, id);
|
581
|
if (couId != null) {
|
582
|
JsonArray managers = calls.getUserIdByCouId(couId, true);
|
583
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
|
584
|
} else {
|
585
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
586
|
}
|
587
|
}
|
588
|
}
|