Project

General

Profile

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

    
3
import eu.dnetlib.repo.manager.domain.dto.Role;
4
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
5
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
6
import eu.dnetlib.repo.manager.service.security.AuthorizationService;
7
import eu.dnetlib.repo.manager.service.security.RoleMappingService;
8
import eu.dnetlib.repo.manager.utils.JsonUtils;
9
import io.swagger.annotations.Api;
10
import io.swagger.annotations.ApiOperation;
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.web.bind.annotation.*;
16

    
17
import javax.ws.rs.core.MediaType;
18
import javax.ws.rs.core.Response;
19
import java.util.Collection;
20

    
21
@RestController
22
@RequestMapping(value = "/role-management")
23
@Api(description = "Role Management", value = "role-management")
24
public class UserRoleController {
25

    
26
    private final AaiRegistryService aaiRegistryService;
27
    private final AuthoritiesUpdater authoritiesUpdater;
28
    private final RoleMappingService roleMappingService;
29
    private final AuthorizationService authorizationService;
30

    
31
    @Autowired
32
    UserRoleController(AaiRegistryService aaiRegistryService,
33
                       AuthoritiesUpdater authoritiesUpdater,
34
                       RoleMappingService roleMappingService,
35
                       AuthorizationService authorizationService) {
36
        this.aaiRegistryService = aaiRegistryService;
37
        this.authoritiesUpdater = authoritiesUpdater;
38
        this.roleMappingService = roleMappingService;
39
        this.authorizationService = authorizationService;
40
    }
41

    
42
    /**
43
     * Get the role with the given id.
44
     **/
45
    @RequestMapping(method = RequestMethod.GET, path = "/role/{id}")
46
//    @PreAuthorize("hasAnyAuthority('REGISTERED_USER', 'SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
47
    public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @PathVariable("id") String id) {
48
        int roleId = aaiRegistryService.getCouId(type, id);
49
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build();
50
    }
51

    
52
    /**
53
     * Create a new role with the given name and description.
54
     **/
55
    @RequestMapping(method = RequestMethod.POST, path = "/role")
56
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR')")
57
    public Response createRole(@RequestBody Role role) {
58
        aaiRegistryService.createRole(role);
59
        return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
60
    }
61

    
62
    /**
63
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
64
     */
65
    @ApiOperation(value = "subscribe")
66
    @RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}")
67
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
68
    public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) {
69
        Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
70
        if (coPersonId == null) {
71
            coPersonId = aaiRegistryService.getCoPersonIdByEmail();
72
        }
73
        Integer couId = aaiRegistryService.getCouId(type, id);
74
        if (couId != null) {
75
            Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
76
            aaiRegistryService.assignMemberRole(coPersonId, couId, role);
77

    
78
            // Add role to current authorities
79
            authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(id));
80

    
81
            return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
82
        } else {
83
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
84
        }
85
    }
86
    /////////////////////////////////////////////////////////////////////////////////////////////
87
    /////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    @RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}")
90
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
91
    public ResponseEntity<String> getUsersByCouId(@PathVariable("id") Integer id) {
92
//        calls.getUserByCoId()
93
        return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString());
94
    }
95

    
96

    
97
    @RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles")
98
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') and authentication.userInfo.email==#email")
99
    public ResponseEntity<Collection<String>> getRolesByEmail(@PathVariable("email") String email) {
100
        return ResponseEntity.ok(authorizationService.getUserRoles(email));
101
    }
102

    
103

    
104
    @RequestMapping(method = RequestMethod.GET, path = "/user/roles/my")
105
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
106
    public ResponseEntity<Collection<String>> getRoleNames() {
107
        return ResponseEntity.ok(authorizationService.getUserRoles());
108
    }
109

    
110
}
(11-11/12)