Project

General

Profile

1
package eu.dnetlib.repo.manager.controllers;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import eu.dnetlib.repo.manager.domain.dto.Role;
6
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
7
import eu.dnetlib.repo.manager.utils.JsonUtils;
8
import io.swagger.annotations.Api;
9
import io.swagger.annotations.ApiOperation;
10
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.security.core.context.SecurityContextHolder;
16
import org.springframework.web.bind.annotation.*;
17

    
18
import javax.ws.rs.core.MediaType;
19
import javax.ws.rs.core.Response;
20
import java.util.ArrayList;
21
import java.util.List;
22

    
23
@RestController
24
@RequestMapping(value = "/aai-user-management")
25
@Api(description = "AAI User Management", value = "aai-user-management")
26
public class AaiUserRoleController {
27

    
28
    private final AaiRegistryService aaiRegistryService;
29

    
30
    // TODO: Antonis K. This should be uncommented
31
//    @Autowired
32
//    private AuthoritiesUpdater authoritiesUpdater;
33
//
34
//    @Autowired
35
//    private AuthorizationService authorizationService;
36

    
37
    @Autowired
38
    AaiUserRoleController(AaiRegistryService aaiRegistryService) {
39
        this.aaiRegistryService = aaiRegistryService;
40
    }
41

    
42
    private String sendEmail() {
43
        OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
44
        return authenticationToken.getUserInfo().getEmail();
45
    }
46

    
47
    /**
48
     * Create a new role with the given name and description.
49
     **/
50
    @RequestMapping(method = RequestMethod.GET, path = "/role/id/get")
51
//    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
52
    public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @RequestParam("id") String id) {
53
        int roleId = aaiRegistryService.getCouId(type, id);
54
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build();
55
    }
56

    
57
    /**
58
     * Create a new role with the given name and description.
59
     **/
60
    @RequestMapping(method = RequestMethod.POST, path = "/createRole")
61
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
62
    public Response createRole(@RequestBody Role role) {
63
        aaiRegistryService.createRole(role);
64
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
65
    }
66

    
67
    /**
68
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
69
     */
70
    @ApiOperation(value = "subscribe")
71
    @RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}")
72
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(#repository.id)) or hasRole(@Converter.convertRepoIdToRoleId(returnObject.id)))")
73
    // TODO: Perhaps less roles here
74
    public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) {
75
        Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
76
        if (coPersonId == null) {
77
            coPersonId = aaiRegistryService.getCoPersonIdByEmail();
78
        }
79
        Integer couId = aaiRegistryService.getCouId(type, id);
80
        if (couId != null) {
81
            Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
82
            aaiRegistryService.assignMemberRole(coPersonId, couId, role);
83
            // TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
84
//            authoritiesUpdater.update(sendEmail(), old -> {
85
//                HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
86
//                authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
87
//                return authorities;
88
//            });
89
            return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
90
        } else {
91
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
92
        }
93
    }
94

    
95

    
96
    /**
97
     * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
98
     */
99
    @ApiOperation(value = "Remove role from member")
100
    @RequestMapping(method = RequestMethod.DELETE, path = "/{type}/{id}/member/{email}")
101
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
102
    public Response removeMemberRole(@PathVariable("type") String type, @PathVariable("id") String
103
            id, @PathVariable("email") String email) {
104
        Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
105
        if (coPersonId != null) {
106
            Integer couId = aaiRegistryService.getCouId(type, id);
107
            Integer role = null;
108
            if (couId != null) {
109
                role = aaiRegistryService.getRoleId(coPersonId, couId);
110
            }
111
            if (couId != null && role != null) {
112
                aaiRegistryService.removeMemberRole(coPersonId, couId, role);
113
                // TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
114
//                authoritiesUpdater.update(email, old -> {
115
//                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
116
//                    authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
117
//                    return authorities;
118
//                });
119
                return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
120
            } else {
121
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
122
            }
123
        } else {
124
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
125
        }
126
    }
127

    
128

    
129
    /**
130
     * Subscribe to role-repo by his email
131
     */
132
    @RequestMapping(method = RequestMethod.POST, path = "/subscribe/repo-role/{id}")
133
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
134
    public Response subscribeRoleByEmail(@PathVariable("id") String id, @RequestParam("email") String email) {
135
        Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
136
        if (coPersonId != null) {
137
            Integer couId = aaiRegistryService.getCouId("datasource", id);
138
            if (couId != null) {
139
                Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
140
                aaiRegistryService.assignMemberRole(coPersonId, couId, role);
141
                // TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
142
//                    authoritiesUpdater.update(sendEmail(), old -> {
143
//                        HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
144
//                        authorities.add(new SimpleGrantedAuthority(authorizationService.member("datasource", id)));
145
//                        return authorities;
146
//                    });
147
                return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
148
            } else {
149
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
150
            }
151
        } else {
152
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User with this email has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
153
        }
154

    
155

    
156
    }
157

    
158

    
159
    /**
160
     * Get all the users that have the role that is associated with repoId
161
     */
162
    @RequestMapping(method = RequestMethod.GET, path = "/repo/{id}/all-users")
163
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
164
    public ResponseEntity<List<String>> getAllUsersOfARepo(@PathVariable("id") String id) {
165

    
166
        List<String> userList = new ArrayList<>();
167

    
168
        // find couId by role name
169
        Integer couId = aaiRegistryService.getCouId("datasource", id);
170
        if (couId != null) {
171
            JsonArray users = aaiRegistryService.getUsersByCouId(couId);
172
            for (JsonElement jsonElement : users) {
173
                userList.add(jsonElement.toString());
174
            }
175
            return ResponseEntity.ok(userList);
176
        }
177

    
178
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
179
    }
180
    /////////////////////////////////////////////////////////////////////////////////////////////
181
    /////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    @RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}")
184
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
185
    public ResponseEntity<String> getUsersByCouId(@PathVariable("id") Integer id) {
186
//        calls.getUserByCoId()
187
        return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString());
188
    }
189

    
190

    
191
    @RequestMapping(method = RequestMethod.GET, path = "/user/roles")
192
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
193
    public ResponseEntity<List<String>> getRolesByEmail(@RequestParam("email") String email) {
194
        int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
195
        List<String> list = new ArrayList<>();
196
        for (JsonElement element : aaiRegistryService.getRoles(coPersonId)) {
197
            list.add(element.toString());
198
        }
199
        return ResponseEntity.ok(list);
200
    }
201

    
202
}
(1-1/12)