Project

General

Profile

« Previous | Next » 

Revision 59242

[Users | Trunk]: Add methods for: 1. Get invited users. 2. Delete a verification. 3. Cancel invitation for a user.

View differences:

modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/utils/VerificationUtils.java
1 1
package eu.dnetlib.openaire.usermanagement.utils;
2 2

  
3
import com.google.gson.JsonArray;
3 4
import com.google.gson.JsonObject;
4 5
import eu.dnetlib.openaire.user.pojos.ManagerVerification;
5 6
import eu.dnetlib.openaire.user.utils.ManagerVerificationActions;
......
9 10
import org.springframework.stereotype.Component;
10 11

  
11 12
import java.sql.Timestamp;
12
import java.util.Date;
13
import java.util.Random;
13
import java.util.*;
14 14

  
15 15

  
16 16
@Component
......
33 33
        return invitation;
34 34
    }
35 35

  
36
    public void deleteRelatedVerifications(ManagerVerification managerVerification) {
37
        List<ManagerVerification> related = actions.
38
                getUserVerificationsForAnEntity(managerVerification.getEmail(), managerVerification.getType(), managerVerification.getEntity());
39
        for(ManagerVerification verification : related) {
40
            deleteVerification(verification.getId());
41
        }
42
    }
43

  
44
    public void deleteUserVerifications(String email, String type, String entity) {
45
        List<ManagerVerification> managerVerifications = actions.
46
                getUserVerificationsForAnEntity(email, type, entity);
47
        for(ManagerVerification verification : managerVerifications) {
48
            deleteVerification(verification.getId());
49
        }
50
    }
51

  
36 52
    public void deleteVerification(String id) {
37 53
        actions.deleteVerificationEntry(id);
38 54
    }
39 55

  
56
    public JsonArray getInvitedUsers(String type, String id) {
57
        List<String> emails = actions.getVerificationsForAnEntity(type, id);
58
        JsonArray users = new JsonArray();
59
        emails.forEach(users::add);
60
        return users;
61
    }
62

  
40 63
    public ManagerVerification getVerification(String id) {
41 64
        return actions.getManagerVerification(id);
42 65
    }
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/api/Test3Service.java
5 5
import com.google.gson.Gson;
6 6
import com.google.gson.JsonArray;
7 7
import com.google.gson.JsonObject;
8
import com.google.gson.JsonParser;
8 9
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO;
9 10
import eu.dnetlib.openaire.user.ldap.MUserActionsLDAP;
11
import eu.dnetlib.openaire.user.login.utils.AuthoritiesMapper;
10 12
import eu.dnetlib.openaire.user.pojos.migration.LDAPUser;
11 13
import eu.dnetlib.openaire.user.store.DataSourceConnector;
12 14
import org.apache.commons.io.IOUtils;
......
124 126
        HttpHeaders headers = new HttpHeaders();
125 127
        headers.add("Authorization","Bearer " + accessToken);
126 128
        HttpEntity<String>  request = new HttpEntity<>(headers);
127

  
128 129
        //logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class));
129 130
        ResponseEntity<String> response = restTemplate.exchange(issuer +"userinfo", HttpMethod.GET, request, String.class);
131
        if(response.getStatusCode() == HttpStatus.OK) {
132
            JsonObject userInfo = new JsonParser().parse(response.getBody()).getAsJsonObject();
133
            JsonArray roles = new JsonArray();
134
            AuthoritiesMapper.map(userInfo.get("edu_person_entitlements").getAsJsonArray()).forEach(grantedAuthority -> {
135
                roles.add(grantedAuthority.getAuthority());
136
            });
137
            userInfo.add("roles", roles);
138
            return Response.status(response.getStatusCode().value()).entity(userInfo.toString()).type(MediaType.APPLICATION_JSON).build();
139
        } else {
140
            return Response.status(response.getStatusCode().value()).entity(response.getBody()).type(MediaType.APPLICATION_JSON).build();
141
        }
130 142

  
131
        return Response.status(response.getStatusCode().value()).entity(response.getBody()).type(MediaType.APPLICATION_JSON).build();
132

  
133 143
    }
134 144

  
135 145
    @GET
......
159 169
            userInfoJson.addProperty("email", userInfo.getEmail());
160 170
            JsonArray roles = new JsonArray();
161 171
            authentication.getAuthorities().forEach(grantedAuthority -> {
162
                roles.add(grantedAuthority.toString());
172
                roles.add(grantedAuthority.getAuthority());
163 173
            });
164 174
            userInfoJson.add("roles", roles);
165 175
        }catch (Exception e){
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java
33 33
    @Autowired
34 34
    private VerificationUtils verificationUtils;
35 35

  
36

  
37

  
38 36
    /**
39 37
     * Subscribe to type(Community, etc.) with id(ee, egi, etc.)
40 38
     *
......
115 113
    }
116 114

  
117 115
    /**
116
     * Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
117
     *
118
     * */
119
    @Path("/invite/{type}/{id}/manager/{email}")
120
    @DELETE
121
    @Produces(MediaType.APPLICATION_JSON)
122
    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
123
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
124
    public Response cancelUserInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
125
        Integer couId = calls.getCouId(type, id);
126
        if (couId != null) {
127
            verificationUtils.deleteUserVerifications(email, type, id);
128
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
129
        } else {
130
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
131
        }
132
    }
133

  
134
    /**
135
     * Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
136
     *
137
     * */
138
    @Path("/invite/{type}/{id}/manager/")
139
    @GET
140
    @Produces(MediaType.APPLICATION_JSON)
141
    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
142
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
143
    public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
144
        JsonArray invited = verificationUtils.getInvitedUsers(type, id);
145
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
146
    }
147

  
148
    /**
118 149
     * Get the verification with a specific id only if it refers to the logged in user
119 150
     *
120 151
     * */
......
136 167
    }
137 168

  
138 169
    /**
170
     * Delete the verification with a specific id.
171
     *
172
     * */
173
    @Path("verification/{id}")
174
    @DELETE
175
    @Produces(MediaType.APPLICATION_JSON)
176
    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN," +
177
            "@AuthoritiesService.PORTAL_ADMIN, @AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
178
    public Response deleteVerification(@PathParam("id") String id) {
179
        if (verificationUtils.getVerification(id) != null) {
180
            verificationUtils.deleteVerification(id);
181
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
182
        } else {
183
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
184
        }
185
    }
186

  
187
    /**
139 188
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
140 189
     * Manager role is assigned to this user, along with the member role.
141 190
     *
......
151 200
            if (coPersonId != null) {
152 201
                if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
153 202
                    if (managerVerification.getVerificationCode().equals(code)) {
154
                        verificationUtils.deleteVerification(managerVerification.getId());
203
                        verificationUtils.deleteRelatedVerifications(managerVerification);
155 204
                        Integer couId = calls.getCouId(managerVerification.getType(), managerVerification.getEntity());
156 205
                        if (couId != null) {
157 206
                            Integer role = calls.getRoleId(coPersonId, couId);
......
212 261
    @Path("/{type}/{id}/subscribers")
213 262
    @GET
214 263
    @Produces(MediaType.APPLICATION_JSON)
215
/*    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
216
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")*/
264
    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
265
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
217 266
    public Response getSubscribers(@PathParam("type") String type, @PathParam("id") String id) {
218 267
        Integer couId = calls.getCouId(type, id);
219 268
        JsonArray subscribers = calls.getUserNamesByCouId(couId, false);
......
227 276
    @Path("/{type}/{id}/subscribers/email")
228 277
    @GET
229 278
    @Produces(MediaType.APPLICATION_JSON)
230
/*    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
231
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")*/
279
    @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
280
            "@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
232 281
    public Response getSubscribersEmail(@PathParam("type") String type, @PathParam("id") String id) {
233 282
        Integer couId = calls.getCouId(type, id);
234 283
        JsonArray subscribers = calls.getUserEmailByCouId(couId, false);

Also available in: Unified diff