Project

General

Profile

1 60749 akoulalis
package eu.dnetlib.repo.manager.controllers;
2
3
import com.google.gson.JsonArray;
4 61318 spyroukon
import com.google.gson.JsonElement;
5 60749 akoulalis
import eu.dnetlib.repo.manager.domain.dto.Role;
6 61318 spyroukon
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
7 60749 akoulalis
import eu.dnetlib.repo.manager.utils.JsonUtils;
8 61318 spyroukon
import io.swagger.annotations.Api;
9
import io.swagger.annotations.ApiOperation;
10 60749 akoulalis
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
13 61318 spyroukon
import org.springframework.http.ResponseEntity;
14 60749 akoulalis
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.security.core.context.SecurityContextHolder;
16 61318 spyroukon
import org.springframework.web.bind.annotation.*;
17 60749 akoulalis
18
import javax.ws.rs.core.MediaType;
19
import javax.ws.rs.core.Response;
20 61318 spyroukon
import java.util.ArrayList;
21
import java.util.List;
22 60749 akoulalis
23
@RestController
24 61318 spyroukon
@RequestMapping(value = "/aai-user-management")
25
@Api(description = "AAI User Management", value = "aai-user-management")
26 60749 akoulalis
public class AaiUserRoleController {
27
28 61318 spyroukon
    private final AaiRegistryService aaiRegistryService;
29 60749 akoulalis
30
    // TODO: Antonis K. This should be uncommented
31
//    @Autowired
32
//    private AuthoritiesUpdater authoritiesUpdater;
33 61318 spyroukon
//
34
//    @Autowired
35
//    private AuthorizationService authorizationService;
36 60749 akoulalis
37
    @Autowired
38 61318 spyroukon
    AaiUserRoleController(AaiRegistryService aaiRegistryService) {
39
        this.aaiRegistryService = aaiRegistryService;
40
    }
41 60749 akoulalis
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 61318 spyroukon
    @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 60749 akoulalis
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
62
    public Response createRole(@RequestBody Role role) {
63 61318 spyroukon
        aaiRegistryService.createRole(role);
64
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
65 60749 akoulalis
    }
66
67
    /**
68
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
69
     */
70 61318 spyroukon
    @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 60749 akoulalis
        if (couId != null) {
81 61318 spyroukon
            Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
82
            aaiRegistryService.assignMemberRole(coPersonId, couId, role);
83 60749 akoulalis
            // 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 61318 spyroukon
            return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
90 60749 akoulalis
        } else {
91 61318 spyroukon
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
92 60749 akoulalis
        }
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 61318 spyroukon
    @ApiOperation(value = "Remove role from member")
100
    @RequestMapping(method = RequestMethod.DELETE, path = "/{type}/{id}/member/{email}")
101 60749 akoulalis
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
102 61318 spyroukon
    public Response removeMemberRole(@PathVariable("type") String type, @PathVariable("id") String
103
            id, @PathVariable("email") String email) {
104
        Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
105 60749 akoulalis
        if (coPersonId != null) {
106 61318 spyroukon
            Integer couId = aaiRegistryService.getCouId(type, id);
107 60749 akoulalis
            Integer role = null;
108
            if (couId != null) {
109 61318 spyroukon
                role = aaiRegistryService.getRoleId(coPersonId, couId);
110 60749 akoulalis
            }
111
            if (couId != null && role != null) {
112 61318 spyroukon
                aaiRegistryService.removeMemberRole(coPersonId, couId, role);
113 60749 akoulalis
                // 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 61318 spyroukon
                return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
120 60749 akoulalis
            } else {
121 61318 spyroukon
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
122 60749 akoulalis
            }
123
        } else {
124 61318 spyroukon
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
125 60749 akoulalis
        }
126
    }
127
128 61318 spyroukon
129 60749 akoulalis
    /**
130 61318 spyroukon
     * Subscribe to role-repo by his email
131 60749 akoulalis
     */
132 61318 spyroukon
    @RequestMapping(method = RequestMethod.POST, path = "/subscribe/repo-role/{id}")
133 60749 akoulalis
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
134 61318 spyroukon
    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 60749 akoulalis
            if (couId != null) {
139 61318 spyroukon
                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 60749 akoulalis
            } else {
149 61318 spyroukon
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
150 60749 akoulalis
            }
151 61318 spyroukon
        } 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 60749 akoulalis
        }
154
155 61318 spyroukon
156 60749 akoulalis
    }
157
158
159
    /**
160 61318 spyroukon
     * Get all the users that have the role that is associated with repoId
161 60749 akoulalis
     */
162 61318 spyroukon
    @RequestMapping(method = RequestMethod.GET, path = "/repo/{id}/all-users")
163 60749 akoulalis
    @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
164 61318 spyroukon
    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 60749 akoulalis
            }
175 61318 spyroukon
            return ResponseEntity.ok(userList);
176 60749 akoulalis
        }
177
178 61318 spyroukon
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
179
    }
180
    /////////////////////////////////////////////////////////////////////////////////////////////
181
    /////////////////////////////////////////////////////////////////////////////////////////////
182 60749 akoulalis
183 61318 spyroukon
    @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 60749 akoulalis
    }
189
190 61318 spyroukon
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
}