Fix a bug on assign role if both session and email are present
This commit is contained in:
parent
959ba82d44
commit
de9fa97e72
|
@ -13,14 +13,13 @@ import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUtils;
|
|||
import eu.dnetlib.dnetrolemanagement.utils.JsonUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
|
@ -56,13 +55,30 @@ public class AdminController {
|
|||
* Assign admin role to logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
|
||||
@PreAuthorize("hasAuthority('REGISTERED_USER') || @registryService.getCoPersonIdsByEmail(#email).size() > 0")
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
||||
Integer coPersonId = (email != null) ? registryService.getCoPersonIdsByEmail(email).get(0) : registryService.getCoPersonIdByIdentifier();
|
||||
if (coPersonId != null) {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(type, id);
|
||||
if (couId != null) {
|
||||
AtomicBoolean assigned = new AtomicBoolean(false);
|
||||
coPersonIds.forEach(coPersonId -> {
|
||||
if(assignRoleToAccount(coPersonId, couId, type, id)) {
|
||||
assigned.set(true);
|
||||
}
|
||||
});
|
||||
if(assigned.get()) {
|
||||
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
|
||||
} else {
|
||||
throw new UnprocessableException("User must be a member of this group and not already admin");
|
||||
}
|
||||
}
|
||||
throw new ResourceNotFoundException("Role has not been found");
|
||||
}
|
||||
throw new ResourceNotFoundException("User has not been found");
|
||||
}
|
||||
|
||||
private boolean assignRoleToAccount(Integer coPersonId, Integer couId, String type, String id) {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
Integer role = registryService.getRoleId(coPersonId, couId);
|
||||
if (role != null) {
|
||||
if (registryService.getUserAdminGroup(coPersonId, couId) == null) {
|
||||
|
@ -72,24 +88,19 @@ public class AdminController {
|
|||
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
|
||||
return authorities;
|
||||
});
|
||||
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
|
||||
return true;
|
||||
}
|
||||
throw new ConflictException("User is already an admin of this group");
|
||||
return false;
|
||||
}
|
||||
throw new UnprocessableException("User must be member of this group in order to become an admin.");
|
||||
}
|
||||
throw new ResourceNotFoundException("Role has not been found");
|
||||
}
|
||||
throw new ResourceNotFoundException("User has not been found");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove admin role from logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
|
||||
@PreAuthorize("hasAuthority('REGISTERED_USER') || @registryService.getCoPersonIdsByEmail(#email).size() > 0")
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
||||
List<Integer> coPersonIds = (email != null) ? registryService.getCoPersonIdsByEmail(email) : Collections.singletonList(registryService.getCoPersonIdByIdentifier());
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(type, id);
|
||||
if (couId != null) {
|
||||
|
|
|
@ -57,13 +57,13 @@ public class MemberController {
|
|||
* Assign member role to logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
|
||||
@PreAuthorize("hasAuthority('REGISTERED_USER') || @registryService.getCoPersonIdsByEmail(#email).size() > 0")
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
||||
Integer coPersonId = (email != null)?registryService.getCoPersonIdsByEmail(email).get(0):registryService.getCoPersonIdByIdentifier();
|
||||
if (coPersonId != null) {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(type, id);
|
||||
if (couId != null) {
|
||||
coPersonIds.forEach(coPersonId -> {
|
||||
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
||||
Integer role = registryService.getRoleId(coPersonId, couId);
|
||||
registryService.assignMemberRole(coPersonId, couId, role);
|
||||
authoritiesUpdater.update(identifier, old -> {
|
||||
|
@ -71,6 +71,7 @@ public class MemberController {
|
|||
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
||||
return authorities;
|
||||
});
|
||||
});
|
||||
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
|
||||
}
|
||||
throw new ResourceNotFoundException("Role has not been found");
|
||||
|
@ -82,9 +83,8 @@ public class MemberController {
|
|||
* Remove member role from logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
|
||||
@PreAuthorize("hasAuthority('REGISTERED_USER') || @registryService.getCoPersonIdsByEmail(#email).size() > 0")
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
||||
List<Integer> coPersonIds = (email != null) ? registryService.getCoPersonIdsByEmail(email) : Collections.singletonList(registryService.getCoPersonIdByIdentifier());
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(type, id);
|
||||
if (couId != null) {
|
||||
|
|
|
@ -12,10 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class RegistryService {
|
||||
|
@ -39,6 +36,7 @@ public class RegistryService {
|
|||
* 1.1 Get CoPersonId by Email
|
||||
*/
|
||||
public List<Integer> getCoPersonIdsByEmail(String email) {
|
||||
if(email != null) {
|
||||
List<Integer> coPersonIds = new ArrayList<>();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("coid", coid);
|
||||
|
@ -51,6 +49,10 @@ public class RegistryService {
|
|||
}
|
||||
}
|
||||
return coPersonIds;
|
||||
} else {
|
||||
Integer coPersonId = getCoPersonIdByIdentifier();
|
||||
return (coPersonId != null)? Collections.singletonList(coPersonId):new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue