1
|
package eu.dnetlib.repo.manager.service.security;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonElement;
|
5
|
import eu.dnetlib.repo.manager.domain.dto.User;
|
6
|
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
|
7
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
8
|
import org.apache.log4j.LogManager;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
11
|
import org.mitre.openid.connect.model.UserInfo;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
import org.springframework.security.core.GrantedAuthority;
|
14
|
import org.springframework.security.core.context.SecurityContextHolder;
|
15
|
import org.springframework.stereotype.Service;
|
16
|
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.Arrays;
|
19
|
import java.util.Collection;
|
20
|
import java.util.List;
|
21
|
import java.util.stream.Collectors;
|
22
|
|
23
|
@Service("authorizationService")
|
24
|
public class AuthorizationServiceImpl implements AuthorizationService {
|
25
|
|
26
|
private static final Logger logger = LogManager.getLogger(AuthorizationServiceImpl.class);
|
27
|
|
28
|
public static final String SUPER_ADMINISTRATOR = "SUPER_ADMINISTRATOR";
|
29
|
public static final String CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR = "CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR";
|
30
|
public static final String REGISTERED_USER = "REGISTERED_USER";
|
31
|
|
32
|
private final RoleMappingService roleMappingService;
|
33
|
private final AaiRegistryService aaiRegistryService;
|
34
|
private final AuthoritiesUpdater authoritiesUpdater;
|
35
|
|
36
|
@Autowired
|
37
|
AuthorizationServiceImpl(RoleMappingService roleMappingService, AaiRegistryService aaiRegistryService,
|
38
|
AuthoritiesUpdater authoritiesUpdater) {
|
39
|
this.roleMappingService = roleMappingService;
|
40
|
this.aaiRegistryService = aaiRegistryService;
|
41
|
this.authoritiesUpdater = authoritiesUpdater;
|
42
|
}
|
43
|
|
44
|
private String mapType(String type) {
|
45
|
if (type.equals("datasource")) {
|
46
|
type = "datasource";
|
47
|
}
|
48
|
return type;
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Type = DATASOURCE
|
53
|
*/
|
54
|
@Override
|
55
|
public String member(String type, String id) {
|
56
|
return mapType(type).toUpperCase() + "_" + id.toUpperCase();
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
public boolean isMemberOf(String repoId) {
|
61
|
String repoRole = roleMappingService.convertRepoIdToEncodedAuthorityId(repoId);
|
62
|
return SecurityContextHolder.getContext().getAuthentication().getAuthorities()
|
63
|
.stream().anyMatch(authority -> authority.toString().equals(repoRole));
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public boolean isMemberOfInterface(String interfaceId) {
|
68
|
|
69
|
//TODO blame Konstantinos Spyrou. He forced my hand...
|
70
|
String repoId = interfaceId.split("::")[1] + "::" + interfaceId.split("::")[2];
|
71
|
|
72
|
return isMemberOf(repoId);
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public List<User> getAdminsOfRepo(String repoId) {
|
77
|
List<String> userList = new ArrayList<>();
|
78
|
|
79
|
// find couId by role name
|
80
|
String role = roleMappingService.getRoleIdByRepoId(repoId);
|
81
|
Integer couId = aaiRegistryService.getCouId(role);
|
82
|
if (couId != null) {
|
83
|
JsonArray users = aaiRegistryService.getUsersByCouId(couId);
|
84
|
for (JsonElement jsonElement : users) {
|
85
|
userList.add(jsonElement.toString());
|
86
|
}
|
87
|
}
|
88
|
return aaiRegistryService.getUsers(couId);
|
89
|
}
|
90
|
|
91
|
|
92
|
@Override
|
93
|
public boolean addAdmin(String id, String email) throws ResourceNotFoundException {
|
94
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
95
|
if (coPersonId != null) {
|
96
|
String role = roleMappingService.getRoleIdByRepoId(id);
|
97
|
Integer couId = aaiRegistryService.getCouId(role);
|
98
|
if (couId != null) {
|
99
|
Integer roleId = aaiRegistryService.getRoleId(coPersonId, couId);
|
100
|
aaiRegistryService.assignMemberRole(coPersonId, couId, roleId);
|
101
|
|
102
|
// Add role to user current authorities
|
103
|
authoritiesUpdater.addRole(email, roleMappingService.convertRepoIdToAuthority(id));
|
104
|
|
105
|
return true;
|
106
|
} else {
|
107
|
throw new ResourceNotFoundException("Cannot find CouId for role: " + role);
|
108
|
}
|
109
|
} else {
|
110
|
throw new ResourceNotFoundException("Cannot find coPersonId for user with email: " + email);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public boolean removeAdmin(String id, String email) throws ResourceNotFoundException {
|
116
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
117
|
if (coPersonId != null) {
|
118
|
String role = roleMappingService.getRoleIdByRepoId(id);
|
119
|
Integer couId = aaiRegistryService.getCouId(role);
|
120
|
Integer roleId = null;
|
121
|
if (couId != null) {
|
122
|
roleId = aaiRegistryService.getRoleId(coPersonId, couId);
|
123
|
}
|
124
|
if (couId != null && roleId != null) {
|
125
|
aaiRegistryService.removeMemberRole(coPersonId, couId, roleId);
|
126
|
|
127
|
// Remove role from user current authorities
|
128
|
authoritiesUpdater.removeRole(email, roleMappingService.convertRepoIdToAuthority(id));
|
129
|
|
130
|
return true;
|
131
|
} else {
|
132
|
throw new ResourceNotFoundException("Cannot find CouId for role: " + role);
|
133
|
}
|
134
|
} else {
|
135
|
throw new ResourceNotFoundException("Cannot find coPersonId for user with email: " + email);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public Collection<String> getUserRoles() {
|
141
|
Collection<String> roles;
|
142
|
UserInfo userInfo = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo();
|
143
|
roles = getUserRoles(userInfo.getEmail());
|
144
|
|
145
|
logger.debug(String.format("User Roles: %s", String.join(",", roles)));
|
146
|
return roles;
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
public Collection<String> getUserRoles(String email) {
|
151
|
int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
152
|
List<Integer> list = new ArrayList<>();
|
153
|
for (JsonElement element : aaiRegistryService.getRolesWithStatus(coPersonId, AaiRegistryService.RoleStatus.ACTIVE)) {
|
154
|
list.add(element.getAsJsonObject().get("CouId").getAsInt());
|
155
|
}
|
156
|
return aaiRegistryService.getCouNames(list).values();
|
157
|
}
|
158
|
|
159
|
}
|