Skip to content

Commit b196f66

Browse files
author
eugenp
committed
securing privilege APIs
1 parent 5f1bfb8 commit b196f66

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
33
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
4-
<listEntry value="/rest-sec/src/test/java/org/rest/sec/client/template/test/RoleClientRESTIntegrationTest.java"/>
4+
<listEntry value="/rest-sec/src/test/java/org/rest/sec/client/template/test/PrivilegeClientRESTIntegrationTest.java"/>
55
</listAttribute>
66
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
77
<listEntry value="1"/>
@@ -12,10 +12,10 @@
1212
</mapAttribute>
1313
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
1414
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
15-
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="givenResourceExists_whenResourceIsRetrieved_thenResourceIsCorrectlyRetrieved"/>
15+
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
1616
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
1717
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
18-
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.client.template.test.RoleClientRESTIntegrationTest"/>
18+
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.rest.sec.client.template.test.PrivilegeClientRESTIntegrationTest"/>
1919
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="rest-sec"/>
2020
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
2121
</launchConfiguration>

‎sec/src/main/java/org/rest/sec/persistence/setup/SecuritySetup.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.context.event.ContextRefreshedEvent;
2020
import org.springframework.stereotype.Component;
2121

22+
import com.google.common.base.Preconditions;
2223
import com.google.common.collect.Sets;
2324

2425
@Component
@@ -71,6 +72,7 @@ public final void onApplicationEvent(final ContextRefreshedEvent event) {
7172
private void createPrivileges() {
7273
createPrivilegeIfNotExisting(SecurityConstants.CAN_USER_WRITE);
7374
createPrivilegeIfNotExisting(SecurityConstants.CAN_ROLE_WRITE);
75+
createPrivilegeIfNotExisting(SecurityConstants.CAN_PRIVILEGE_WRITE);
7476
}
7577

7678
final void createPrivilegeIfNotExisting(final String name) {
@@ -84,10 +86,14 @@ final void createPrivilegeIfNotExisting(final String name) {
8486
// Role
8587

8688
private void createRoles() {
87-
final Privilege privilegeUserWrite = privilegeService.findByName(SecurityConstants.CAN_USER_WRITE);
88-
final Privilege privilegeRoleWrite = privilegeService.findByName(SecurityConstants.CAN_ROLE_WRITE);
89-
90-
createRoleIfNotExisting(SecurityConstants.ROLE_ADMIN, Sets.<Privilege> newHashSet(privilegeUserWrite, privilegeRoleWrite));
89+
final Privilege canUserWrite = privilegeService.findByName(SecurityConstants.CAN_USER_WRITE);
90+
final Privilege canRoleWrite = privilegeService.findByName(SecurityConstants.CAN_ROLE_WRITE);
91+
final Privilege canPrivilegeWrite = privilegeService.findByName(SecurityConstants.CAN_PRIVILEGE_WRITE);
92+
Preconditions.checkNotNull(canUserWrite);
93+
Preconditions.checkNotNull(canRoleWrite);
94+
Preconditions.checkNotNull(canPrivilegeWrite);
95+
96+
createRoleIfNotExisting(SecurityConstants.ROLE_ADMIN, Sets.<Privilege> newHashSet(canUserWrite, canRoleWrite, canPrivilegeWrite));
9197
}
9298

9399
final void createRoleIfNotExisting(final String name, final Set<Privilege> privileges) {

‎sec/src/main/java/org/rest/sec/util/SecurityConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class SecurityConstants {
88
*/
99
public static final String CAN_USER_WRITE = "ROLE_USER_WRITE";
1010
public static final String CAN_ROLE_WRITE = "ROLE_ROLE_WRITE";
11+
public static final String CAN_PRIVILEGE_WRITE = "ROLE_PRIVILEGE_WRITE";
1112

1213
public static final String ROLE_ADMIN = "AdminOfSecurityService";
1314

‎sec/src/main/java/org/rest/sec/web/controller/PrivilegeController.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import org.rest.common.web.controller.ISortingController;
1414
import org.rest.sec.model.Privilege;
1515
import org.rest.sec.persistence.service.IPrivilegeService;
16+
import org.rest.sec.util.SecurityConstants;
1617
import org.rest.sec.web.common.URIMappingConstants;
1718
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.dao.InvalidDataAccessApiUsageException;
1920
import org.springframework.http.HttpStatus;
21+
import org.springframework.security.access.annotation.Secured;
2022
import org.springframework.stereotype.Controller;
2123
import org.springframework.web.bind.annotation.PathVariable;
2224
import org.springframework.web.bind.annotation.RequestBody;
@@ -108,6 +110,7 @@ public Privilege findByName(@RequestParam("name") final String name) {
108110

109111
@RequestMapping(method = RequestMethod.POST)
110112
@ResponseStatus(HttpStatus.CREATED)
113+
@Secured(SecurityConstants.CAN_PRIVILEGE_WRITE)
111114
public void create(@RequestBody final Privilege resource, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
112115
createInternal(resource, uriBuilder, response);
113116
}
@@ -116,6 +119,7 @@ public void create(@RequestBody final Privilege resource, final UriComponentsBui
116119

117120
@RequestMapping(method = RequestMethod.PUT)
118121
@ResponseStatus(HttpStatus.OK)
122+
@Secured(SecurityConstants.CAN_PRIVILEGE_WRITE)
119123
public void update(@RequestBody final Privilege resource) {
120124
updateInternal(resource);
121125
}
@@ -124,6 +128,7 @@ public void update(@RequestBody final Privilege resource) {
124128

125129
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
126130
@ResponseStatus(HttpStatus.NO_CONTENT)
131+
@Secured(SecurityConstants.CAN_PRIVILEGE_WRITE)
127132
public void delete(@PathVariable("id") final Long id) {
128133
deleteByIdInternal(id);
129134
}

0 commit comments

Comments
 (0)