Merge from master
This commit is contained in:
commit
ec9d644527
|
@ -51,13 +51,14 @@ public class AdminController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Assign admin role to logged in user or user with @email
|
||||
* If role doesn't exists or user is not a member of this group already, use force=true to create and assign both roles.
|
||||
* Assign admin role to logged-in user or user with @email
|
||||
* If role doesn't exist or user is not a member of this group already, use force=true to create and assign both roles.
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
|
||||
@RequestParam(required = false) String identifier,
|
||||
@RequestParam(value = "force", defaultValue = "false") boolean force) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
|
||||
if (temp != null || force) {
|
||||
|
@ -98,18 +99,19 @@ public class AdminController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Remove admin role from logged in user or user with @email
|
||||
* Remove admin role from logged-in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id,
|
||||
@RequestParam(required = false) String identifier,
|
||||
@RequestParam(required = false) String email) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
|
||||
if (couId != null) {
|
||||
coPersonIds.forEach(coPersonId -> {
|
||||
registryService.removeAdminRole(coPersonId, couId);
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
|
||||
authoritiesUpdater.removeRole(registryService.getIdentifierByCoPersonId(coPersonId), new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
|
||||
});
|
||||
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
|
||||
}
|
||||
|
|
|
@ -90,21 +90,22 @@ public class MemberController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Assign member role to logged in user or user with @email
|
||||
* Assign member role to logged-in user or user with @email
|
||||
* If role doesn't exist, use force=true to create and assign the role
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id,
|
||||
@RequestParam(required = false) String identifier,
|
||||
@RequestParam(required = false) String email,
|
||||
@RequestParam(value = "force", defaultValue = "false") boolean force) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
|
||||
if (temp != null || force) {
|
||||
Integer couId = (temp != null) ? temp : registryService.createRole(AuthoritiesUtils.memberRole(type, id), "");
|
||||
coPersonIds.forEach(coPersonId -> {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
registryService.assignMemberRole(coPersonId, couId);
|
||||
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
||||
authoritiesUpdater.addRole(registryService.getIdentifierByCoPersonId(coPersonId), new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
||||
});
|
||||
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
|
||||
}
|
||||
|
@ -114,27 +115,29 @@ public class MemberController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Remove member role from logged in user or user with @email
|
||||
* Remove member role from logged-in user or user with @email
|
||||
* If user is an admin of this group, use force=true to revoke both roles
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id,
|
||||
@RequestParam(required = false) String identifier,
|
||||
@RequestParam(required = false) String email,
|
||||
@RequestParam(value = "force", defaultValue = "false") boolean force) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
|
||||
if (couId != null) {
|
||||
// If none of the accounts has admin role for this group remove member role
|
||||
if (force || coPersonIds.stream().noneMatch(coPersonId -> registryService.getUserAdminGroup(coPersonId, couId) != null)) {
|
||||
coPersonIds.forEach(coPersonId -> {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
String aaiIdentifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
Integer role = registryService.getRoleId(coPersonId, couId);
|
||||
if (force) {
|
||||
registryService.removeAdminRole(coPersonId, couId);
|
||||
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
|
||||
authoritiesUpdater.removeRole(aaiIdentifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
|
||||
}
|
||||
registryService.removeMemberRole(coPersonId, couId, role);
|
||||
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
||||
authoritiesUpdater.removeRole(aaiIdentifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
||||
});
|
||||
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package eu.dnetlib.dnetrolemanagement.controllers;
|
||||
|
||||
import eu.dnetlib.dnetrolemanagement.entities.Response;
|
||||
import eu.dnetlib.dnetrolemanagement.entities.User;
|
||||
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
|
||||
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
|
||||
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
|
||||
|
@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/super")
|
||||
|
@ -90,4 +92,15 @@ public class SuperAdminController {
|
|||
}
|
||||
throw new ResourceNotFoundException("User has not been found");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user", method = RequestMethod.GET)
|
||||
public ResponseEntity<List<User>> getUsersByEmail(@RequestParam String email) {
|
||||
return ResponseEntity.ok(registryService.getCoPersonIdsByEmail(email).stream().map(id -> {
|
||||
User user = new User();
|
||||
user.setEmail(registryService.getUserEmail(id));
|
||||
user.setId(registryService.getUserId(id));
|
||||
user.setName(registryService.getUserNames(id));
|
||||
return user;
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ public class RegistryService {
|
|||
/**
|
||||
* 1.1 Get CoPersonId by Email
|
||||
*/
|
||||
public List<Integer> getCoPersonIdsByEmail(String email) {
|
||||
public List<Integer> getCoPersonIdsByEmail(String email, String identifier) {
|
||||
if (email != null) {
|
||||
List<Integer> coPersonIds = new ArrayList<>();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
@ -46,11 +46,15 @@ public class RegistryService {
|
|||
}
|
||||
return coPersonIds;
|
||||
} else {
|
||||
Integer coPersonId = getCoPersonIdByIdentifier();
|
||||
Integer coPersonId = identifier != null ? getCoPersonIdByIdentifier(identifier): getCoPersonIdByIdentifier();
|
||||
return (coPersonId != null) ? Collections.singletonList(coPersonId) : new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Integer> getCoPersonIdsByEmail(String email) {
|
||||
return getCoPersonIdsByEmail(email, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.2 Get CoPersonId by AAI identifier
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue