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