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.service.security.RoleMappingService;
8
import eu.dnetlib.repo.manager.service.security.AuthoritiesMapper;
9
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
10
import eu.dnetlib.repo.manager.utils.JsonUtils;
11
import io.swagger.annotations.Api;
12
import io.swagger.annotations.ApiOperation;
13
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
14
import org.mitre.openid.connect.model.UserInfo;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.http.HttpStatus;
17
import org.springframework.http.ResponseEntity;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.security.core.context.SecurityContextHolder;
20
import org.springframework.web.bind.annotation.*;
21

    
22
import javax.ws.rs.core.MediaType;
23
import javax.ws.rs.core.Response;
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.List;
27

    
28
@RestController
29
@RequestMapping(value = "/role-management")
30
@Api(description = "Role Management", value = "role-management")
31
public class UserRoleController {
32

    
33
    private final AaiRegistryService aaiRegistryService;
34
    private final AuthoritiesUpdater authoritiesUpdater;
35
    private final RoleMappingService roleMappingService;
36

    
37
    @Autowired
38
    UserRoleController(AaiRegistryService aaiRegistryService,
39
                       AuthoritiesUpdater authoritiesUpdater,
40
                       RoleMappingService roleMappingService) {
41
        this.aaiRegistryService = aaiRegistryService;
42
        this.authoritiesUpdater = authoritiesUpdater;
43
        this.roleMappingService = roleMappingService;
44
    }
45

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

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

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

    
82
            // Add role to current authorities
83
            authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(id));
84

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

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

    
100

    
101
    @RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles")
102
    @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') and authentication.userInfo.email==#email")
103
    public ResponseEntity<Collection<String>> getRolesByEmail(@PathVariable("email") String email) {
104
        int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
105
        List<Integer> list = new ArrayList<>();
106
        for (JsonElement element : aaiRegistryService.getRolesWithStatus(coPersonId, AaiRegistryService.RoleStatus.ACTIVE)) {
107
            list.add(element.getAsJsonObject().get("CouId").getAsInt());
108
        }
109
        return ResponseEntity.ok(aaiRegistryService.getCouNames(list).values());
110
    }
111

    
112

    
113
    @RequestMapping(method = RequestMethod.GET, path = "/user/roles/my")
114
    @PreAuthorize("hasAuthority('REGISTERED_USER')")
115
    public ResponseEntity<Collection<String>> getRoleNames() {
116
        List<String> roles;
117
        JsonArray entitlements = null;
118
        UserInfo userInfo = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo();
119
        if (userInfo.getSource().getAsJsonArray("edu_person_entitlements") != null) {
120
            entitlements = userInfo.getSource().getAsJsonArray("edu_person_entitlements");
121
        } else if (userInfo.getSource().getAsJsonArray("eduperson_entitlement") != null) {
122
            entitlements = userInfo.getSource().getAsJsonArray("eduperson_entitlement");
123
        } else {
124
            return ResponseEntity.ok(null);
125
        }
126
        roles = AuthoritiesMapper.entitlementRoles(entitlements);
127
        return ResponseEntity.ok(roles);
128
    }
129

    
130
}
(11-11/12)