1
|
package eu.dnetlib.openaire.usermanagement.api;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonObject;
|
5
|
import eu.dnetlib.openaire.user.pojos.ManagerVerification;
|
6
|
import eu.dnetlib.openaire.usermanagement.dto.Role;
|
7
|
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
|
8
|
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
|
9
|
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.http.HttpStatus;
|
13
|
import org.springframework.security.access.prepost.PreAuthorize;
|
14
|
import org.springframework.stereotype.Component;
|
15
|
import org.springframework.web.bind.annotation.RequestBody;
|
16
|
|
17
|
import javax.ws.rs.*;
|
18
|
import javax.ws.rs.core.MediaType;
|
19
|
import javax.ws.rs.core.Response;
|
20
|
|
21
|
@Component(value = "RegistryService")
|
22
|
@Path("/registry")
|
23
|
public class RegistryService {
|
24
|
|
25
|
private static final Logger logger = Logger.getLogger(RegistryService.class);
|
26
|
|
27
|
@Autowired
|
28
|
private RegistryCalls calls;
|
29
|
|
30
|
@Autowired
|
31
|
private JsonUtils jsonUtils;
|
32
|
|
33
|
@Autowired
|
34
|
private VerificationUtils verificationUtils;
|
35
|
|
36
|
/**
|
37
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
38
|
*
|
39
|
* */
|
40
|
@Path("/subscribe/{type}/{id}")
|
41
|
@POST
|
42
|
@Produces(MediaType.APPLICATION_JSON)
|
43
|
@PreAuthorize("isAuthenticated()")
|
44
|
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
|
45
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
46
|
Integer couId = calls.getCouId(type, id);
|
47
|
if (couId != null) {
|
48
|
Integer role = calls.getRoleId(coPersonId, couId);
|
49
|
calls.assignMemberRole(coPersonId, couId, role);
|
50
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
51
|
} else {
|
52
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
53
|
}
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Subscribe from type(Community, etc.) with id(ee, egi, etc.).
|
58
|
* If user has manager role for this entity, it will be removed too.
|
59
|
*
|
60
|
* */
|
61
|
@Path("/unsubscribe/{type}/{id}")
|
62
|
@POST
|
63
|
@Produces(MediaType.APPLICATION_JSON)
|
64
|
@PreAuthorize("isAuthenticated()")
|
65
|
public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) {
|
66
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
67
|
Integer couId = calls.getCouId(type, id);
|
68
|
if (couId != null) {
|
69
|
Integer role = calls.getRoleId(coPersonId, couId);
|
70
|
if (role != null) {
|
71
|
calls.removeAdminRole(coPersonId, couId);
|
72
|
calls.removeMemberRole(coPersonId, couId, role);
|
73
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
74
|
} else
|
75
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User does not have this role").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
|
* Create a new role with the given name and description.
|
83
|
*
|
84
|
**/
|
85
|
@Path("/createRole")
|
86
|
@POST
|
87
|
@Produces(MediaType.APPLICATION_JSON)
|
88
|
@Consumes(MediaType.APPLICATION_JSON)
|
89
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN)")
|
90
|
public Response createRole(@RequestBody Role role) {
|
91
|
calls.createRole(role);
|
92
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
|
97
|
* Auto generated link and code will be sent as response.
|
98
|
*
|
99
|
* */
|
100
|
@Path("/invite/{type}/{id}/manager/{email}")
|
101
|
@POST
|
102
|
@Produces(MediaType.APPLICATION_JSON)
|
103
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
|
104
|
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
105
|
public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
|
106
|
Integer couId = calls.getCouId(type, id);
|
107
|
if (couId != null) {
|
108
|
Integer coPersonId = calls.getCoPersonIdByEmail(email);
|
109
|
if(calls.getUserAdminGroup(coPersonId, couId) == null) {
|
110
|
JsonObject invitation = verificationUtils.createInvitation(email, type, id);
|
111
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
|
112
|
} else {
|
113
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
|
114
|
}
|
115
|
} else {
|
116
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
|
122
|
*
|
123
|
* */
|
124
|
@Path("/invite/{type}/{id}/manager/{email}")
|
125
|
@DELETE
|
126
|
@Produces(MediaType.APPLICATION_JSON)
|
127
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
|
128
|
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
129
|
public Response cancelUserInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
|
130
|
Integer couId = calls.getCouId(type, id);
|
131
|
if (couId != null) {
|
132
|
verificationUtils.deleteUserVerifications(email, type, id);
|
133
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
|
134
|
} else {
|
135
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
136
|
}
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
|
141
|
*
|
142
|
* */
|
143
|
@Path("/invite/{type}/{id}/managers/")
|
144
|
@GET
|
145
|
@Produces(MediaType.APPLICATION_JSON)
|
146
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
|
147
|
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
148
|
public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
|
149
|
JsonArray invited = verificationUtils.getInvitedUsers(type, id);
|
150
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Get the verification with a specific id only if it refers to the logged in user
|
155
|
*
|
156
|
* */
|
157
|
@Path("verification/{id}")
|
158
|
@GET
|
159
|
@Produces(MediaType.APPLICATION_JSON)
|
160
|
@PreAuthorize("isAuthenticated()")
|
161
|
public Response getVerification(@PathParam("id") String id) {
|
162
|
ManagerVerification managerVerification = verificationUtils.getVerification(id);
|
163
|
if (managerVerification != null) {
|
164
|
if (calls.getCoPersonIdByEmail(managerVerification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
|
165
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(managerVerification)).toString()).type(MediaType.APPLICATION_JSON).build();
|
166
|
} else {
|
167
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
|
168
|
}
|
169
|
} else {
|
170
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Delete the verification with a specific id.
|
176
|
*
|
177
|
* */
|
178
|
@Path("verification/{id}")
|
179
|
@DELETE
|
180
|
@Produces(MediaType.APPLICATION_JSON)
|
181
|
@PreAuthorize("isAuthenticated() && @VerificationUtils.ownedVerification(#id)")
|
182
|
public Response deleteVerification(@PathParam("id") String id) {
|
183
|
if (verificationUtils.getVerification(id) != null) {
|
184
|
verificationUtils.deleteVerification(id);
|
185
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
|
186
|
} else {
|
187
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
|
188
|
}
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
|
193
|
* Manager role is assigned to this user, along with the member role.
|
194
|
*
|
195
|
* */
|
196
|
@Path("verification/{id}")
|
197
|
@POST
|
198
|
@Produces(MediaType.APPLICATION_JSON)
|
199
|
@PreAuthorize("isAuthenticated()")
|
200
|
public Response verify(@PathParam("id") String id, @RequestBody String code) {
|
201
|
ManagerVerification managerVerification = verificationUtils.getVerification(id);
|
202
|
if (managerVerification != null) {
|
203
|
Integer coPersonId = calls.getCoPersonIdByEmail(managerVerification.getEmail());
|
204
|
if (coPersonId != null) {
|
205
|
if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
|
206
|
if (managerVerification.getVerificationCode().equals(code)) {
|
207
|
verificationUtils.deleteRelatedVerifications(managerVerification);
|
208
|
Integer couId = calls.getCouId(managerVerification.getType(), managerVerification.getEntity());
|
209
|
if (couId != null) {
|
210
|
Integer role = calls.getRoleId(coPersonId, couId);
|
211
|
calls.assignMemberRole(coPersonId, couId, role);
|
212
|
if (calls.getUserAdminGroup(coPersonId, couId) == null) {
|
213
|
calls.assignAdminRole(coPersonId, couId);
|
214
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
215
|
} else {
|
216
|
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User is already admin of this cou").toString()).type(MediaType.APPLICATION_JSON).build();
|
217
|
}
|
218
|
} else {
|
219
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
220
|
}
|
221
|
} else {
|
222
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
|
223
|
}
|
224
|
} else {
|
225
|
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
|
226
|
}
|
227
|
} else {
|
228
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
229
|
}
|
230
|
} else {
|
231
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
232
|
}
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
237
|
*
|
238
|
* */
|
239
|
@Path("/{type}/{id}/manager/{email}")
|
240
|
@DELETE
|
241
|
@Produces(MediaType.APPLICATION_JSON)
|
242
|
@Consumes(MediaType.APPLICATION_JSON)
|
243
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN," +
|
244
|
"@AuthoritiesService.PORTAL_ADMIN, @AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
245
|
public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
|
246
|
id, @PathParam("email") String email) {
|
247
|
Integer coPersonId = calls.getCoPersonIdByEmail(email);
|
248
|
if (coPersonId != null) {
|
249
|
Integer couId = calls.getCouId(type, id);
|
250
|
if (couId != null) {
|
251
|
calls.removeAdminRole(coPersonId, couId);
|
252
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
253
|
} else {
|
254
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
255
|
}
|
256
|
} else {
|
257
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
258
|
}
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Get the names of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
|
263
|
*
|
264
|
* */
|
265
|
@Path("/{type}/{id}/subscribers")
|
266
|
@GET
|
267
|
@Produces(MediaType.APPLICATION_JSON)
|
268
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
|
269
|
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
270
|
public Response getSubscribers(@PathParam("type") String type, @PathParam("id") String id) {
|
271
|
Integer couId = calls.getCouId(type, id);
|
272
|
JsonArray subscribers = calls.getUserNamesByCouId(couId, false);
|
273
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Get the emails of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
|
278
|
*
|
279
|
* */
|
280
|
@Path("/{type}/{id}/subscribers/email")
|
281
|
@GET
|
282
|
@Produces(MediaType.APPLICATION_JSON)
|
283
|
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
|
284
|
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
|
285
|
public Response getSubscribersEmail(@PathParam("type") String type, @PathParam("id") String id) {
|
286
|
Integer couId = calls.getCouId(type, id);
|
287
|
JsonArray subscribers = calls.getUserEmailByCouId(couId, false);
|
288
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* Get the number of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
|
293
|
*
|
294
|
* */
|
295
|
@Path("/{type}/{id}/subscribers/count")
|
296
|
@GET
|
297
|
@Produces(MediaType.APPLICATION_JSON)
|
298
|
public Response getSubscribersCount(@PathParam("type") String type, @PathParam("id") String id) {
|
299
|
Integer couId = calls.getCouId(type, id);
|
300
|
int count = calls.getUserNamesByCouId(couId, false).size();
|
301
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
|
302
|
}
|
303
|
|
304
|
/**
|
305
|
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
|
306
|
*
|
307
|
* */
|
308
|
@Path("/{type}/{id}/managers")
|
309
|
@GET
|
310
|
@Produces(MediaType.APPLICATION_JSON)
|
311
|
public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
|
312
|
Integer couId = calls.getCouId(type, id);
|
313
|
JsonArray managers = calls.getUserNamesByCouId(couId, true);
|
314
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
|
319
|
*
|
320
|
* */
|
321
|
@Path("/{type}/{id}/managers/email")
|
322
|
@GET
|
323
|
@Produces(MediaType.APPLICATION_JSON)
|
324
|
public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
|
325
|
Integer couId = calls.getCouId(type, id);
|
326
|
JsonArray managers = calls.getUserEmailByCouId(couId, true);
|
327
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
|
328
|
}
|
329
|
}
|