Revision 61944
Added by Konstantina Galouni about 2 years ago
PageController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoaadmintoolslibrary.controllers; |
2 | 2 |
|
3 |
import com.mongodb.DuplicateKeyException; |
|
3 | 4 |
import eu.dnetlib.uoaadmintoolslibrary.entities.Page; |
4 | 5 |
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalPage; |
6 |
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; |
|
7 |
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException; |
|
8 |
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; |
|
5 | 9 |
import eu.dnetlib.uoaadmintoolslibrary.services.PageService; |
6 | 10 |
|
7 | 11 |
import org.apache.log4j.Logger; |
... | ... | |
9 | 13 |
import org.springframework.web.bind.annotation.*; |
10 | 14 |
import org.springframework.security.access.prepost.PreAuthorize; |
11 | 15 |
|
16 |
import javax.validation.constraints.Null; |
|
12 | 17 |
import java.util.*; |
13 | 18 |
|
14 | 19 |
@RestController |
... | ... | |
19 | 24 |
@Autowired |
20 | 25 |
private PageService pageService; |
21 | 26 |
|
27 |
@Autowired |
|
28 |
private RolesUtils rolesUtils; |
|
29 |
|
|
22 | 30 |
// used by portals WITHOUT ANY PARAMS |
23 | 31 |
@RequestMapping(value = "/page/full", method = RequestMethod.GET) |
24 | 32 |
public List<PortalPage> getPagesFull(@RequestParam(value="pid", required=false) String pid, |
25 | 33 |
@RequestParam(value="page_route", required=false) String page_route) { |
34 |
// isEnabled is not filled!!! Do not use this method to get pages for specific pid - use getPagesForPortalByType instead. |
|
26 | 35 |
return pageService.getPagesFull(pid, page_route); |
27 | 36 |
} |
28 | 37 |
|
... | ... | |
41 | 50 |
// } |
42 | 51 |
|
43 | 52 |
// used |
44 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
53 |
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
|
45 | 54 |
@RequestMapping(value = "/page/update", method = RequestMethod.POST) |
46 | 55 |
public PortalPage updatePage(@RequestBody PortalPage portalPage) { |
56 |
List<String> roles = rolesUtils.getRoles(); |
|
57 |
if(portalPage == null) { |
|
58 |
throw new NullPointerException("Update page: portalPage is null"); |
|
59 |
} |
|
60 |
if(portalPage.getId() == null) { |
|
61 |
// EXCEPTION - MismatchingContent |
|
62 |
throw new MismatchingContentException("Update page: Page has no id."); |
|
63 |
} |
|
64 |
if(!rolesUtils.isPortalAdmin(roles) || ( |
|
65 |
portalPage.getPortalPid() != null && !rolesUtils.hasUpdateAuthority(roles, portalPage.getPortalType(), portalPage.getPortalPid()))) { |
|
66 |
// EXCEPTION - Access denied |
|
67 |
throw new ForbiddenException("Update page: You are not authorized to update a page for "+portalPage.getPortalType()+ |
|
68 |
(portalPage.getPortalPid()!=null ? " : "+portalPage.getPortalPid() : "")); |
|
69 |
} |
|
70 |
|
|
71 |
Page existingPage = pageService.getPageByPortalTypeAndRoute(portalPage.getPortalType(), portalPage.getRoute(), portalPage.getPortalPid()); |
|
72 |
if(existingPage != null && !existingPage.getId().equals(portalPage.getId())) { |
|
73 |
throw new MismatchingContentException("Update page: There is already a page ("+existingPage.getId()+") with route: "+portalPage.getRoute() + " in "+ |
|
74 |
portalPage.getPortalType() + (portalPage.getPortalPid() != null ? " : "+portalPage.getPortalPid() : "")); |
|
75 |
} |
|
47 | 76 |
return pageService.updatePage(portalPage); |
48 | 77 |
} |
49 | 78 |
|
50 | 79 |
// used |
51 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
80 |
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
|
52 | 81 |
@RequestMapping(value = "/page/save", method = RequestMethod.POST) |
53 | 82 |
public PortalPage insertPage(@RequestBody PortalPage portalPage) { |
83 |
List<String> roles = rolesUtils.getRoles(); |
|
84 |
if(portalPage == null) { |
|
85 |
throw new NullPointerException("Save page: portalPage is null"); |
|
86 |
} |
|
87 |
if(portalPage.getId() != null) { |
|
88 |
// EXCEPTION - MismatchingContent |
|
89 |
throw new MismatchingContentException("Save page: Page has already an id: "+portalPage.getId()); |
|
90 |
} |
|
91 |
if(!rolesUtils.isPortalAdmin(roles) || ( |
|
92 |
portalPage.getPortalPid() != null && !rolesUtils.hasUpdateAuthority(roles, portalPage.getPortalType(), portalPage.getPortalPid()))) { |
|
93 |
// EXCEPTION - Access denied |
|
94 |
throw new ForbiddenException("Save page: You are not authorized to create a page for "+portalPage.getPortalType()+ |
|
95 |
(portalPage.getPortalPid()!=null ? " : "+portalPage.getPortalPid() : "")); |
|
96 |
} |
|
97 |
|
|
98 |
Page existingPage = pageService.getPageByPortalTypeAndRoute(portalPage.getPortalType(), portalPage.getRoute(), portalPage.getPortalPid()); |
|
99 |
if(existingPage != null) { |
|
100 |
throw new MismatchingContentException("Save page: There is already a page ("+existingPage.getId()+") with route: "+portalPage.getRoute() + " in "+ |
|
101 |
portalPage.getPortalType() + (portalPage.getPortalPid() != null ? " : "+portalPage.getPortalPid() : "")); |
|
102 |
} |
|
103 |
|
|
54 | 104 |
return pageService.insertPage(portalPage); |
55 | 105 |
} |
56 | 106 |
|
57 | 107 |
// used |
58 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
108 |
// @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
|
59 | 109 |
@RequestMapping(value = "/page/delete", method = RequestMethod.POST) |
60 | 110 |
public Boolean deletePages(@RequestBody List<String> pages) throws Exception { |
61 | 111 |
return pageService.deletePages(pages); |
Also available in: Unified diff
[Trunk | Admin tools library]: Added portal specific pages.
1. Page.java & PortalPage.java: Added new field "String portalPid" - pages will be either portalPid and portalType specific either only portalType specific.
2. MongoDBDAOs/MongoDBPageDAO.java & PageDAO.java: Updated find methods to search for pages related to portalType and pages related to both portalPid and portalType.
a. Replaced findByPortalType with: @Query("{'portalType': ?0, 'portalPid': {$in: [?1, null] }}") List<Page> findByPortalTypeAndPortalPidOrNull(String portalType, String portalPid);
b. Replaced findByPortalTypeAndRoute with: @Query("{'portalType': ?0, 'route': ?1, 'portalPid': {$in: [?2, null] }}") Page findByPortalTypeAndRouteAndPortalPidOrNull(String portalType, String route, String portalPid);
3. PageController.java & PageService.java & PortalService.java & PageHelpContentService.java: Updated methods to handle pages with portalPid and added more checks for null values and permissions.