uoa-admin-tools/src/main/java/eu/dnetlib/uoaadmintools/controllers/CuratorController.java

100 lines
3.1 KiB
Java
Raw Normal View History

package eu.dnetlib.uoaadmintools.controllers;
import eu.dnetlib.uoaadmintools.dao.CuratorDAO;
import eu.dnetlib.uoaadmintools.entities.curator.Curator;
import eu.dnetlib.uoaadmintools.entities.curator.CuratorResponse;
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@CrossOrigin(origins = "*")
public class CuratorController {
private final Logger log = Logger.getLogger(this.getClass());
@Autowired
private CuratorDAO curatorDAO;
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
@Autowired
private RolesUtils rolesUtils;
/**
* Return a list with curator. If list of emails does not existed return all curators, else return
* curators based on given list.
*
* @param emails
* @return
*/
@RequestMapping(value = "/curator", method = RequestMethod.GET)
public List<CuratorResponse> getCurators(@RequestParam(required = false) Optional<String> emails) {
List<CuratorResponse> curators = new ArrayList<>();
if(emails.isPresent()) {
for(String email: emails.get().split(",")) {
Curator curator = curatorDAO.findByEmail(email);
if(curator != null) {
curators.add(new CuratorResponse(curator));
}
}
} else {
for(Curator curator: curatorDAO.findAll()) {
curators.add(new CuratorResponse(curator));
}
}
return curators;
}
/**
* Return a Curator with the given id.
*
* @param id
* @return
*/
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/curator/{id}", method = RequestMethod.GET)
public Curator getCuratorById(@PathVariable String id) {
return curatorDAO.findById(id);
}
/**
* Create or update a curator, base on Curator object given on Request Body.
*
* @param curator
* @return
*/
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/curator", method = RequestMethod.POST)
public Curator insertCurator(@RequestBody Curator curator) {
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
String aaiId = rolesUtils.getAaiId();
curator.setId(aaiId);
return curatorDAO.save(curator);
}
/**
* Delete all curators if list of emails does not exist or curators based on given list.
*
* @param emails
*/
[Trunk | Admin Tools]: 1. pom.xml: Added dependency for spring security. 2. UoaAdminToolsApplication.java: Import AuthorizationConfiguration.class | Remove SecurityConfig.class from @EnableConfigurationProperties. 3. UoaAdminToolsConfiguration.java: Comment "addInterceptors()" method calling AuthorizationHandler with SecurityConfig. 4. SecurityConfig.java & AuthorizationHandler.java & AuthorizationUtils.java & CommunityInfo.java & UserInfo.java: Commented all contents of these files (files will be deleted in coming commit). 5. PortalSubscribersController.java: Comment imports from commeted files. 6. Notifications.java: Added field "aaiId" get getters and setters. 7. NotificationsController.java: a. Method "getNotifications()" is replaced by "getNotificationsForUser()" (/community/{pid}/notifications) - returns notification settings only for user who made the request (uoa-authorization-li$ b. Path changed for method "getNotifications()": /community/{pid}/notifications/all c. Remove "@RequestBody String email" parameter from method "deleteNotification()" - get email from user who made the request (uoa-authorization-library). d. In method "saveNotification()" get aaiId and email from user who made the request (uoa-authorization-library). e. Added checks and throw Exceptions in all methods. f. Added @PreAuthorize Portal Admins: "getNotifications()" (/community/{pid}/notifications/all) Portal Admins - Curators - Managers: "getNotificationsForUser()" (/community/{pid}/notifications), "deleteNotification()" (/community/{pid}/notifications), "saveNotification()" (/communit$ 8. ExploreController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateExplore()" (/explore/update), "insertExplore()" (/explore/save), "deleteExplore()" (/explore/delete). 9. ConnectController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateConnect()" (/connect/update), "insertConnect()" (/connect/save), "deleteConnect()" (/connect/delete). c. Commented methods "getLayoutForConnect()" and "updateLayoutForConnect()" (/connect/{pid}/layout). 10. CommunityController.java: a. Added checks and throw Exceptions in all methods. b. Added @PreAuthorize Portal Admins: "updateCommunity()" (/community/update), "insertCommunity()" (/community/save), "deleteCommunity()" (/community/delete). Portal Admin - Curators - Managers: "updateLayoutForCommunity()" (/community/{pid}/layout). 11. CuratorController.java: a. In "insertCurator() (/curator) set _id field with aaiId from user who made the request (uoa-authorization-library). b. Added @PreAuthorize Authenticated users: "getCuratorById()" (/curator/{id}), "insertCurator()" (/curator). Portal Admins: "deleteCurators()" (/curator).
2021-02-25 12:57:22 +01:00
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/curator", method = RequestMethod.DELETE)
public void deleteCurators(@RequestBody(required = false) Optional<List<String>> emails) {
if(emails.isPresent()) {
for(String email: emails.get()) {
Curator curator = curatorDAO.findByEmail(email);
if(curator != null) {
curatorDAO.delete(curator.getId());
}
}
} else {
curatorDAO.deleteAll();
}
}
}