Remove all field gets. Add force assign and remove role from users.

master
kostis30fyllou 3 years ago
parent de9fa97e72
commit 9a7d55694c

@ -2,10 +2,9 @@ package eu.dnetlib.dnetrolemanagement.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import eu.dnetlib.dnetrolemanagement.dto.Response;
import eu.dnetlib.dnetrolemanagement.dto.User;
import eu.dnetlib.dnetrolemanagement.entities.Response;
import eu.dnetlib.dnetrolemanagement.entities.User;
import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException;
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUpdater;
@ -40,8 +39,8 @@ public class AdminController {
* Get the user info of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET)
public ResponseEntity<User[]> getInfos(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
public ResponseEntity<User[]> getAll(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, true);
JsonArray emails = registryService.getUserEmailByCouId(couId, true);
@ -55,18 +54,20 @@ public class AdminController {
* Assign admin role to logged in user or user with @email
*/
@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 email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(type, id);
if (couId != null) {
Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (temp != null || force) {
Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.memberRole(type, id), "");
AtomicBoolean assigned = new AtomicBoolean(false);
coPersonIds.forEach(coPersonId -> {
if(assignRoleToAccount(coPersonId, couId, type, id)) {
if (assignRoleToAccount(coPersonId, couId, type, id, force)) {
assigned.set(true);
}
});
if(assigned.get()) {
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");
@ -77,14 +78,20 @@ public class AdminController {
throw new ResourceNotFoundException("User has not been found");
}
private boolean assignRoleToAccount(Integer coPersonId, Integer couId, String type, String id) {
private boolean assignRoleToAccount(Integer coPersonId, Integer couId, String type, String id, boolean force) {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
if (role != null) {
if (role != null || force) {
if(role == null) {
registryService.assignMemberRole(coPersonId,couId, role);
}
if (registryService.getUserAdminGroup(coPersonId, couId) == null) {
registryService.assignAdminRole(coPersonId, couId);
authoritiesUpdater.update(identifier, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
if(role == null) {
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
}
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
return authorities;
});
@ -102,7 +109,7 @@ public class AdminController {
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
coPersonIds.forEach(coPersonId -> {
registryService.removeAdminRole(coPersonId, couId);
@ -121,12 +128,12 @@ public class AdminController {
}
/**
/* *//**
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/name", method = RequestMethod.GET)
public ResponseEntity<User[]> getNames(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserNamesByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
@ -135,12 +142,12 @@ public class AdminController {
}
/**
*//**
* Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/id", method = RequestMethod.GET)
public ResponseEntity<User[]> getIds(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
@ -148,18 +155,18 @@ public class AdminController {
throw new ResourceNotFoundException("Role has not been found");
}
/**
*//**
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/email", method = RequestMethod.GET)
public ResponseEntity<User[]> getEmails(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserEmailByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
}*/
}

@ -0,0 +1,123 @@
package eu.dnetlib.dnetrolemanagement.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import eu.dnetlib.dnetrolemanagement.entities.Response;
import eu.dnetlib.dnetrolemanagement.entities.Role;
import eu.dnetlib.dnetrolemanagement.entities.User;
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUpdater;
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.core.authority.SimpleGrantedAuthority;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@RestController
@RequestMapping("/curator")
public class CuratorController {
private final RegistryService registryService;
private final AuthoritiesUpdater authoritiesUpdater;
private final Gson gson;
@Autowired
public CuratorController(RegistryService registryService, AuthoritiesUpdater authoritiesUpdater) {
this.registryService = registryService;
this.authoritiesUpdater = authoritiesUpdater;
this.gson = new Gson();
}
/**
* Create a new role (only for admins)
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity<Response> createRole(@RequestBody Role role) {
if (registryService.getCouId(role.getName()) == null) {
if (registryService.createRole(role.getName(), role.getDescription()) != null) {
return ResponseEntity.ok(new Response("Role has been created successfully"));
} else {
throw new UnprocessableException("Role creation failed. Try again later");
}
} else {
throw new ConflictException("This role already exists");
}
}
/**
* Get the user info of the curators of a type(Community, etc.)
*/
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public ResponseEntity<User[]> getAll(@PathVariable("type") String type) {
Integer couId = registryService.getCouId(AuthoritiesUtils.curatorRole(type));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, false);
JsonArray emails = registryService.getUserEmailByCouId(couId, false);
JsonArray names = registryService.getUserNamesByCouId(couId, false);
return ResponseEntity.ok(JsonUtils.mergeUserInfo(users, emails, names, gson));
}
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Assign curator role to logged in user or user with @email
*/
@RequestMapping(value = "/{type}", method = RequestMethod.POST)
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @RequestParam(required = false) String email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer temp = registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase());
if (temp != null || force) {
Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.curatorRole(type), "");
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(identifier, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.curator(type)));
return authorities;
});
});
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
}
throw new ResourceNotFoundException("Role has not been found");
}
throw new ResourceNotFoundException("User has not been found");
}
/**
* Remove curator role from logged in user or user with @email
*/
@RequestMapping(value = "/{type}", method = RequestMethod.DELETE)
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @RequestParam(required = false) String email) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase());
if (couId != null) {
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.removeMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(identifier, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.remove(new SimpleGrantedAuthority(AuthoritiesUtils.curator(type)));
return authorities;
});
});
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
}
throw new ResourceNotFoundException("Role has not been found");
}
throw new ResourceNotFoundException("User has not been found");
}
}

@ -2,8 +2,10 @@ package eu.dnetlib.dnetrolemanagement.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import eu.dnetlib.dnetrolemanagement.dto.Response;
import eu.dnetlib.dnetrolemanagement.dto.User;
import eu.dnetlib.dnetrolemanagement.entities.Response;
import eu.dnetlib.dnetrolemanagement.entities.Role;
import eu.dnetlib.dnetrolemanagement.entities.User;
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
@ -11,13 +13,12 @@ import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUpdater;
import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUtils;
import eu.dnetlib.dnetrolemanagement.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.stream.Collectors;
@ -38,12 +39,13 @@ public class MemberController {
}
/**
* Get the user info of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET)
public ResponseEntity<User[]> getInfos(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
public ResponseEntity<User[]> getAll(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, false);
JsonArray emails = registryService.getUserEmailByCouId(couId, false);
@ -57,11 +59,13 @@ public class MemberController {
* Assign member role to logged in user or user with @email
*/
@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 email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(type, id);
if (couId != null) {
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);
Integer role = registryService.getRoleId(coPersonId, couId);
@ -83,26 +87,33 @@ public class MemberController {
* Remove member 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) {
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(type, id);
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 (coPersonIds.stream().filter(coPersonId -> registryService.getUserAdminGroup(coPersonId, couId) != null).collect(Collectors.toList()).size() == 0) {
if (force || coPersonIds.stream().noneMatch(coPersonId -> registryService.getUserAdminGroup(coPersonId, couId) != null)) {
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
if(force) {
registryService.removeAdminRole(coPersonId, couId);
}
registryService.removeMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(identifier, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
if(force) {
authorities.remove(new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
}
authorities.remove(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
return authorities;
});
});
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
}
throw new UnprocessableException("User is admin of this group, so you must remove admin role first instead of member role");
throw new UnprocessableException("Remove admin role first");
}
throw new ResourceNotFoundException("Role has not been found");
}
@ -110,12 +121,12 @@ public class MemberController {
}
/**
/* *//**
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/name", method = RequestMethod.GET)
public ResponseEntity<User[]> getNames(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserNamesByCouId(couId, false);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
@ -123,12 +134,12 @@ public class MemberController {
throw new ResourceNotFoundException("Role has not been found");
}
/**
*//**
* Get the Identifiers of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/id", method = RequestMethod.GET)
public ResponseEntity<User[]> getIds(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, false);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
@ -136,16 +147,16 @@ public class MemberController {
throw new ResourceNotFoundException("Role has not been found");
}
/**
*//**
* Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
*//*
@RequestMapping(value = "/{type}/{id}/email", method = RequestMethod.GET)
public ResponseEntity<User[]> getEmails(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserEmailByCouId(couId, false);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
}*/
}

@ -1,4 +1,4 @@
package eu.dnetlib.dnetrolemanagement.dto;
package eu.dnetlib.dnetrolemanagement.entities;
public class Response {

@ -0,0 +1,29 @@
package eu.dnetlib.dnetrolemanagement.entities;
public class Role {
String name;
String description;
public Role() {}
public Role(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

@ -1,4 +1,4 @@
package eu.dnetlib.dnetrolemanagement.dto;
package eu.dnetlib.dnetrolemanagement.entities;
public class User {

@ -128,17 +128,6 @@ public class RegistryService {
return null;
}
/**
* 4.2 Get a couId by type.id with/without mapping type
*
* @param type
* @param id
* @return
*/
public Integer getCouId(String type, String id) {
return getCouId(type + "." + id);
}
/**
* 5. Get User non admin roles
*/

@ -7,33 +7,40 @@ import org.springframework.security.core.context.SecurityContextHolder;
public class AuthoritiesUtils {
/**
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
*
* */
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT etc.
*/
public static String curator(String type) {
return "CURATOR_" + type.toUpperCase();
}
public static String curatorRole(String type) {
return "Curator - " + Character.toString(type.charAt(0)).toUpperCase() + type.substring(1);
}
/**
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
*
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT etc.
* <p>
* Id = EE, EGI, etc
* */
*/
public static String manager(String type, String id) {
return type.toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
}
/**
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
*
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT etc.
* <p>
* Id = EE, EGI, etc
* */
*/
public static String member(String type, String id) {
return type.toUpperCase() + "_" + id.toUpperCase();
}
public static String memberRole(String type, String id) {
return type + "." + id;
}
public static String getAaiId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getSub() : null;
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken) authentication).getSub() : null;
}
}

@ -5,7 +5,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import eu.dnetlib.dnetrolemanagement.config.properties.RegistryProperties;
import eu.dnetlib.dnetrolemanagement.dto.User;
import eu.dnetlib.dnetrolemanagement.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Loading…
Cancel
Save