diff --git a/README.md b/README.md index d27fb87..0ce9cee 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ Framework also requires spring-context dependency for not spring-based projects: ``` +**Components** +![Easy-ABAC components](https://github.com/exadel-inc/activity-based-security-framework/blob/master/abac-diagram.png) + **Core Attributes** - ```Action``` interface - to define possible actions with entity - ```@Access``` annotation - to define custom annotation to restrict access to entity diff --git a/abac-diagram.png b/abac-diagram.png new file mode 100644 index 0000000..3e95964 Binary files /dev/null and b/abac-diagram.png differ diff --git a/easy-abac-demo/pom.xml b/easy-abac-demo/pom.xml index ef4d738..41b1e70 100644 --- a/easy-abac-demo/pom.xml +++ b/easy-abac-demo/pom.xml @@ -1,4 +1,20 @@ + + @@ -13,7 +29,7 @@ com.exadel.security easy-abac-demo - 1.0-RC2 + 1.0-RC3 easy-abac-demo easy-abac implementation example diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/DemoApplication.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/DemoApplication.java index 57028bf..060eca5 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/DemoApplication.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/DemoApplication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo; import com.exadel.easyabac.aspect.AbacConfiguration; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/configuration/SecurityConfig.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/configuration/SecurityConfig.java index 58710fe..d08bec5 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/configuration/SecurityConfig.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/configuration/SecurityConfig.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.configuration; import org.springframework.context.annotation.Configuration; @@ -19,7 +35,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() - .antMatchers("/*", "/welcome", "/login-as-user", "/login-as-administrator").permitAll() + .antMatchers("/*", "/welcome", "/login-as-admin", "/login-as-ba", "/login-as-dev").permitAll() .anyRequest() .authenticated(); } diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/ProjectController.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/ProjectController.java index 0197bab..34e05e1 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/ProjectController.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/ProjectController.java @@ -1,19 +1,32 @@ -package com.exadel.easyabac.demo.controller; +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static com.exadel.easyabac.demo.security.model.project.ProjectAction.DELETE; -import static com.exadel.easyabac.demo.security.model.project.ProjectAction.UPDATE; -import static com.exadel.easyabac.demo.security.model.project.ProjectAction.VIEW; +package com.exadel.easyabac.demo.controller; import com.exadel.easyabac.demo.security.model.project.ProjectAccess; import com.exadel.easyabac.demo.security.model.project.ProjectId; import com.exadel.easyabac.model.annotation.ProtectedResource; import com.exadel.easyabac.model.annotation.PublicResource; - import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static com.exadel.easyabac.demo.security.model.project.ProjectAction.*; + /** * Sample controller for Project entity. * @@ -29,19 +42,19 @@ public class ProjectController { @ProjectAccess(VIEW) @RequestMapping public ResponseEntity get(@ProjectId @PathVariable("projectId") Long projectId) { - return ResponseEntity.ok().build(); + return getResponse(projectId); } @ProjectAccess(UPDATE) @RequestMapping("/update") public ResponseEntity update(@ProjectId @PathVariable("projectId") Long projectId) { - return ResponseEntity.ok().build(); + return getResponse(projectId); } @ProjectAccess(DELETE) @RequestMapping("/delete") public ResponseEntity delete(@ProjectId @PathVariable("projectId") Long projectId) { - return ResponseEntity.ok().build(); + return getResponse(projectId); } @PublicResource @@ -49,4 +62,8 @@ public ResponseEntity delete(@ProjectId @PathVariable("projectId") Long projectI public ResponseEntity getPublicInfo() { return ResponseEntity.ok().build(); } + + private ResponseEntity getResponse(Long projectId) { + return ResponseEntity.ok(String.format("Project[id=%s]", projectId)); + } } diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/StoryController.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/StoryController.java index 721e03e..caff022 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/StoryController.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/StoryController.java @@ -1,6 +1,20 @@ -package com.exadel.easyabac.demo.controller; +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static com.exadel.easyabac.demo.security.model.project.ProjectAction.VIEW; +package com.exadel.easyabac.demo.controller; import com.exadel.easyabac.demo.security.model.project.ProjectAccess; import com.exadel.easyabac.demo.security.model.project.ProjectId; @@ -8,12 +22,13 @@ import com.exadel.easyabac.demo.security.model.story.StoryAction; import com.exadel.easyabac.demo.security.model.story.StoryId; import com.exadel.easyabac.model.annotation.ProtectedResource; - import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static com.exadel.easyabac.demo.security.model.project.ProjectAction.VIEW; + /** * Sample controller for Story entity. * @@ -31,7 +46,7 @@ public class StoryController { public ResponseEntity get( @ProjectId @PathVariable("projectId") Long projectId, @StoryId @PathVariable("storyId") Long storyId) { - return ResponseEntity.ok().build(); + return getResponse(projectId, storyId); } @StoryAccess(StoryAction.UPDATE) @@ -39,6 +54,10 @@ public ResponseEntity get( public ResponseEntity update( @ProjectId @PathVariable("projectId") Long projectId, @StoryId @PathVariable("storyId") Long storyId) { - return ResponseEntity.ok().build(); + return getResponse(projectId, storyId); + } + + private ResponseEntity getResponse(Long projectId, Long storyId) { + return ResponseEntity.ok(String.format("Project[id=%s], Story[id=%s]", projectId, storyId)); } } diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/TaskController.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/TaskController.java index f3844a7..9292950 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/TaskController.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/TaskController.java @@ -1,6 +1,20 @@ -package com.exadel.easyabac.demo.controller; +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static com.exadel.easyabac.demo.security.model.project.ProjectAction.VIEW; +package com.exadel.easyabac.demo.controller; import com.exadel.easyabac.demo.security.model.project.ProjectAccess; import com.exadel.easyabac.demo.security.model.project.ProjectId; @@ -11,12 +25,13 @@ import com.exadel.easyabac.demo.security.model.task.TaskAction; import com.exadel.easyabac.demo.security.model.task.TaskId; import com.exadel.easyabac.model.annotation.ProtectedResource; - import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static com.exadel.easyabac.demo.security.model.project.ProjectAction.VIEW; + /** * Sample controller for Task entity. * @@ -36,7 +51,7 @@ public ResponseEntity get( @ProjectId @PathVariable("projectId") Long projectId, @StoryId @PathVariable("storyId") Long storyId, @TaskId @PathVariable("taskId") Long taskId) { - return ResponseEntity.ok().build(); + return getResponse(projectId, storyId, taskId); } @TaskAccess(TaskAction.UPDATE) @@ -45,6 +60,10 @@ public ResponseEntity update( @ProjectId @PathVariable("projectId") Long projectId, @StoryId @PathVariable("storyId") Long storyId, @TaskId @PathVariable("taskId") Long taskId) { - return ResponseEntity.ok().build(); + return getResponse(projectId, storyId, taskId); + } + + private ResponseEntity getResponse(Long projectId, Long storyId, Long taskId) { + return ResponseEntity.ok(String.format("Project[id=%s], Story[id=%s], Task[id=%s]", projectId, storyId, taskId)); } } diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/WelcomeController.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/WelcomeController.java index 9393db3..8dd7eed 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/WelcomeController.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/controller/WelcomeController.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.controller; import com.exadel.easyabac.demo.security.authorization.DemoAuthorization; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/exception/AccessException.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/exception/AccessException.java index 01a7158..9a8f686 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/exception/AccessException.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/exception/AccessException.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.exception; import com.exadel.easyabac.demo.security.model.AccessResponse; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/handler/RestExceptionHandler.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/handler/RestExceptionHandler.java index 6ffeb5f..eb6767e 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/handler/RestExceptionHandler.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/handler/RestExceptionHandler.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.handler; import com.exadel.easyabac.demo.exception.AccessException; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProvider.java new file mode 100644 index 0000000..f7e4393 --- /dev/null +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProvider.java @@ -0,0 +1,53 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exadel.easyabac.demo.security.action; + +import com.exadel.easyabac.model.core.Action; + +import java.lang.reflect.ParameterizedType; +import java.util.Set; + +/** + * Example of entity action provider. + * + * @param the type parameter + * @author Gleb Bondarchuk + * @author Igor Sych + * @since 1.0-RC1 + */ +public interface ActionProvider { + + /** + * Example of generic method to fetch actions by particular type. + * + * @param entityId the entity identifier + * @return the available actions for entity + */ + Set getAvailableActions(Long entityId); + + /** + * Check whether provider accepts action type. + * + * @param actionType the action type + * @return true if accepts, false otherwise + */ + @SuppressWarnings("unchecked") + default boolean accepts(Class actionType) { + Class type = (Class) ((ParameterizedType) getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; + return actionType.equals(type); + } +} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProviderFactory.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProviderFactory.java new file mode 100644 index 0000000..196e3a2 --- /dev/null +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ActionProviderFactory.java @@ -0,0 +1,50 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exadel.easyabac.demo.security.action; + +import com.exadel.easyabac.model.core.Action; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Action provider factory example. + * + * @author Gleb Bondarchuk + * @since 1.0-RC2 + */ +@Component +public class ActionProviderFactory { + + @Autowired + private Set> providers; + + public ActionProvider getProvider(Class actionType) { + Set> foundProviders = providers.stream().filter(provider -> provider.accepts(actionType)).collect(Collectors.toSet()); + + if (CollectionUtils.isEmpty(foundProviders)) { + throw new IllegalArgumentException(String.format("Unable to find provider for type %s", actionType)); + } + if (CollectionUtils.size(foundProviders) > 1) { + throw new IllegalArgumentException(String.format("Providers defined ambiguously for type %s", actionType)); + } + return foundProviders.iterator().next(); + } +} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ProjectActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ProjectActionProvider.java new file mode 100644 index 0000000..6a80f57 --- /dev/null +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/ProjectActionProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exadel.easyabac.demo.security.action; + +import com.exadel.easyabac.demo.security.authorization.DemoAuthorization; +import com.exadel.easyabac.demo.security.model.project.ProjectAction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.Set; + +/** + * Project actions provider. + * + * @author Gleb Bondarchuk + * @since 1.0-RC2 + */ +@Component +public class ProjectActionProvider implements ActionProvider { + + @Autowired + private DemoAuthorization authorization; + + @Override + public Set getAvailableActions(Long projectId) { + Set actions = authorization.getUserActions(ProjectAction.class); + + // this is example of how actions can be restricted by some project attributes, for example, status, etc. + // so, for example, projects with 'active' status are visible for developers, while others - aren't. + + // for example, Project project = projectDao.get(projectId), then check some project attributes or other relations + // and decide which of actions are available. + + if (projectId == 1L) { + return actions; + } else { + return Collections.emptySet(); + } + } +} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/StoryActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/StoryActionProvider.java new file mode 100644 index 0000000..4b3ed1a --- /dev/null +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/StoryActionProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exadel.easyabac.demo.security.action; + +import com.exadel.easyabac.demo.security.authorization.DemoAuthorization; +import com.exadel.easyabac.demo.security.model.story.StoryAction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.Set; + +/** + * Story actions provider. + * + * @author Gleb Bondarchuk + * @since 1.0-RC2 + */ +@Component +public class StoryActionProvider implements ActionProvider { + + @Autowired + private DemoAuthorization authorization; + + @Override + public Set getAvailableActions(Long storyId) { + Set actions = authorization.getUserActions(StoryAction.class); + + // this is example of how actions can be restricted by some story attributes, for example, status, etc. + // so, for example, stories with 'active' status are visible for developers, while others - aren't. + + // for example, Story story = storyDao.get(storyId), then check some story attributes or other relations + // and decide which of actions are available. + + if (storyId == 1L) { + return actions; + } else { + return Collections.emptySet(); + } + } +} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/TaskActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/TaskActionProvider.java new file mode 100644 index 0000000..7aebdd8 --- /dev/null +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/action/TaskActionProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exadel.easyabac.demo.security.action; + +import com.exadel.easyabac.demo.security.authorization.DemoAuthorization; +import com.exadel.easyabac.demo.security.model.task.TaskAction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.Set; + +/** + * Task action provider. + * + * @author Gleb Bondarchuk + * @since 1.0-RC2 + */ +@Component +public class TaskActionProvider implements ActionProvider { + + @Autowired + private DemoAuthorization authorization; + + @Override + public Set getAvailableActions(Long taskId) { + Set actions = authorization.getUserActions(TaskAction.class); + + // this is example of how actions can be restricted by some task attributes, for example, status, etc. + // so, for example, tasks with 'active' status are visible for developers, while others - aren't. + + // for example, Task task = taskDao.get(taskId), then check some task attributes or other relations + // and decide which of actions are available. + + if (taskId == 1L) { + return actions; + } else { + return Collections.emptySet(); + } + } +} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/ActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/ActionProvider.java deleted file mode 100644 index 829d63c..0000000 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/ActionProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.exadel.easyabac.demo.security.authorization; - -import com.exadel.easyabac.model.core.Action; - -import java.util.Set; - -/** - * Example of entity action provider. - * - * @author Gleb Bondarchuk - * @author Igor Sych - * @since 1.0-RC1 - */ -public interface ActionProvider { - - - /** - * Example of generic method to fetch actions by particular type. - * - * @param entityId the entity identifier - * @param entityClass the entity class - * @return the available actions for entity - */ - Set getAvailableActions(Long entityId, Class entityClass); -} diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoActionProvider.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoActionProvider.java deleted file mode 100644 index 8317896..0000000 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoActionProvider.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.exadel.easyabac.demo.security.authorization; - -import com.exadel.easyabac.demo.security.model.project.ProjectAction; -import com.exadel.easyabac.demo.security.model.story.StoryAction; -import com.exadel.easyabac.demo.security.model.task.TaskAction; -import com.exadel.easyabac.model.core.Action; - -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Component; - -import java.util.Collections; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * The demo actions provider. - * - * @author Gleb Bondarchuk - * @author Igor Sych - * @since 1.0-RC1 - */ -@Component -public class DemoActionProvider implements ActionProvider { - - @Override - public Set getAvailableActions(Long entityId, Class entityClass) { - Set actions = getUserActions(); - - // this is just example of how actions can be restricted by some business dynamic attributes, - // so one entity has the action while other entity of the same type - doesn't. - if (entityClass.isAssignableFrom(ProjectAction.class)) { - return filterProjectActions(actions, entityId); - } - if (entityClass.isAssignableFrom(StoryAction.class)) { - return filterStoryActions(actions, entityId); - } - if (entityClass.isAssignableFrom(TaskAction.class)) { - return filterTaskActions(actions, entityId); - } - throw new IllegalArgumentException("Action " + entityClass.getName() + " is not handled"); - } - - /** - * Filter actions available for user for particular project with {@code projectId} - * - * @param actions the actions available for user - * @param projectId the project identifier - * @return the available project actions - */ - private Set filterProjectActions(Set actions, Long projectId) { - Set projectActions = filterActions(actions, ProjectAction.class); - - // this is example of how actions can be restricted by some project attributes, for example, status, etc. - // so, for example, projects with 'active' status are visible for developers, while others - aren't. - if (projectId == 1L) { - return projectActions; - } else { - return Collections.emptySet(); - } - } - - /** - * Filter actions available for user for particular story with {@code storyId} - * - * @param actions the actions available for user - * @param storyId the story identifier - * @return the available story actions - */ - private Set filterStoryActions(Set actions, Long storyId) { - Set storyActions = filterActions(actions, StoryAction.class); - - // this is example of how actions can be restricted by some story attributes, for example, status, etc. - // so, for example, stories with 'active' status are visible for developers, while others - aren't. - if (storyId == 1L) { - return storyActions; - } else { - return Collections.emptySet(); - } - } - - /** - * Filter actions available for user for particular task with {@code taskId} - * - * @param actions the actions available for user - * @param taskId the task identifier - * @return the available task actions - */ - private Set filterTaskActions(Set actions, Long taskId) { - Set taskActions = filterActions(actions, TaskAction.class); - - // this is example of how actions can be restricted by some task attributes, for example, status, etc. - // so, for example, tasks with 'active' status are visible for developers, while others - aren't. - if (taskId == 1L) { - return taskActions; - } else { - return Collections.emptySet(); - } - } - - private Set getUserActions() { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - return authentication.getAuthorities().stream() - .map(UserGrantedAuthority.class::cast) - .map(UserGrantedAuthority::getAction) - .collect(Collectors.toSet()); - } - - private Set filterActions(Set actions, Class type) { - return actions.stream().filter(type::isInstance).collect(Collectors.toSet()); - } -} - - - diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoAuthorization.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoAuthorization.java index 43f2eb6..6fd14ba 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoAuthorization.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/DemoAuthorization.java @@ -1,12 +1,30 @@ -package com.exadel.easyabac.demo.security.authorization; +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exadel.easyabac.demo.security.authorization; +import com.exadel.easyabac.model.core.Action; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import java.security.Principal; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; /** * Demo authorization utility methods. @@ -39,4 +57,20 @@ public String getLoggedUserRole() { .map(Principal::getName) .orElse(null); } + + /** + * Gets all user actions of type {@code actionType} available permanently. + * + * @param actionType the type of action + * @return the user actions + */ + public Set getUserActions(Class actionType) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + return authentication.getAuthorities().stream() + .map(UserGrantedAuthority.class::cast) + .map(UserGrantedAuthority::getAction) + .filter(actionType::isInstance) + .map(actionType::cast) + .collect(Collectors.toSet()); + } } diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/UserGrantedAuthority.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/UserGrantedAuthority.java index 21abf0d..c3fa13c 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/UserGrantedAuthority.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/UserGrantedAuthority.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.authorization; import com.exadel.easyabac.model.core.Action; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AbstractUserAuthentication.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AbstractUserAuthentication.java index d6031a8..7ad67b6 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AbstractUserAuthentication.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AbstractUserAuthentication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.authorization.authentication; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AdminAuthentication.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AdminAuthentication.java index 7ab39fc..45cf969 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AdminAuthentication.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/AdminAuthentication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.authorization.authentication; import com.google.common.collect.ImmutableSet; @@ -23,6 +39,7 @@ public class AdminAuthentication extends AbstractUserAuthentication { ProjectAction.DELETE, StoryAction.VIEW, + TaskAction.VIEW ); diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/BusinessAnalystAuthentication.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/BusinessAnalystAuthentication.java index c84a845..a0b7d1f 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/BusinessAnalystAuthentication.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/BusinessAnalystAuthentication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.authorization.authentication; import com.google.common.collect.ImmutableSet; @@ -22,6 +38,7 @@ public class BusinessAnalystAuthentication extends AbstractUserAuthentication { StoryAction.VIEW, StoryAction.UPDATE, + TaskAction.VIEW, TaskAction.UPDATE ); diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/DeveloperAuthentication.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/DeveloperAuthentication.java index 6a1d8ce..a8c953c 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/DeveloperAuthentication.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/authorization/authentication/DeveloperAuthentication.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.authorization.authentication; import com.google.common.collect.ImmutableSet; @@ -21,6 +37,7 @@ public class DeveloperAuthentication extends AbstractUserAuthentication { ProjectAction.VIEW, StoryAction.VIEW, + TaskAction.VIEW, TaskAction.UPDATE ); diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/AccessResponse.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/AccessResponse.java index a3331d5..509903b 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/AccessResponse.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/AccessResponse.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model; import com.exadel.easyabac.model.core.Action; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAccess.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAccess.java index 1dd070c..146a462 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAccess.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAccess.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.project; import com.exadel.easyabac.demo.security.validator.DemoValidator; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAction.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAction.java index b6f3267..8755866 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAction.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectAction.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.project; import com.exadel.easyabac.model.core.Action; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectId.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectId.java index 8ccc68a..144f98e 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectId.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/project/ProjectId.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.project; import java.lang.annotation.ElementType; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAccess.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAccess.java index cd72428..f314dd8 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAccess.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAccess.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.story; import com.exadel.easyabac.demo.security.validator.DemoValidator; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAction.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAction.java index 9771000..065fdd5 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAction.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryAction.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.story; import com.exadel.easyabac.model.core.Action; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryId.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryId.java index aecb19c..4170bbe 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryId.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/story/StoryId.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.story; import java.lang.annotation.ElementType; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAccess.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAccess.java index 5e479b4..907a31f 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAccess.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAccess.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.task; import com.exadel.easyabac.demo.security.validator.DemoValidator; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAction.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAction.java index f842915..25bf5d4 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAction.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskAction.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.task; import com.exadel.easyabac.model.core.Action; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskId.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskId.java index 8e21e77..ecc5bd7 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskId.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/model/task/TaskId.java @@ -1,3 +1,19 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.model.task; import java.lang.annotation.ElementType; diff --git a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/validator/DemoValidator.java b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/validator/DemoValidator.java index db87ec9..dbfb4a8 100644 --- a/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/validator/DemoValidator.java +++ b/easy-abac-demo/src/main/java/com/exadel/easyabac/demo/security/validator/DemoValidator.java @@ -1,7 +1,24 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.exadel.easyabac.demo.security.validator; import com.exadel.easyabac.demo.exception.AccessException; -import com.exadel.easyabac.demo.security.authorization.ActionProvider; +import com.exadel.easyabac.demo.security.action.ActionProvider; +import com.exadel.easyabac.demo.security.action.ActionProviderFactory; import com.exadel.easyabac.demo.security.authorization.DemoAuthorization; import com.exadel.easyabac.demo.security.model.AccessResponse; import com.exadel.easyabac.model.core.Action; @@ -27,7 +44,7 @@ public class DemoValidator implements EntityAccessValidator { private static final String ERROR_TEMPLATE = "Access to entity[id=%s] denied."; @Autowired - private ActionProvider actionProvider; + private ActionProviderFactory actionProviderFactory; @Autowired private DemoAuthorization authorization; @@ -35,7 +52,8 @@ public class DemoValidator implements EntityAccessValidator { @Override public void validate(ExecutionContext context) { Long entityId = context.getEntityId(); - Set availableActions = actionProvider.getAvailableActions(entityId, context.getActionType()); + ActionProvider provider = actionProviderFactory.getProvider(context.getActionType()); + Set availableActions = provider.getAvailableActions(entityId); Set requiredActions = context.getRequiredActions(); Set missingActions = SetUtils.difference(requiredActions, availableActions); @@ -47,7 +65,7 @@ public void validate(ExecutionContext context) { authorization.getLoggedUserRole(), entityId, missingActions, - context.getJoinPoint().getSignature().toString() + context.getMethod() ); throw new AccessException(String.format(ERROR_TEMPLATE, entityId), response); } diff --git a/easy-abac-demo/src/main/resources/application.properties b/easy-abac-demo/src/main/resources/application.properties index c49b26e..a9e4120 100644 --- a/easy-abac-demo/src/main/resources/application.properties +++ b/easy-abac-demo/src/main/resources/application.properties @@ -1,2 +1,18 @@ +# +# Copyright 2019-2020 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + logging.level.org.springframework.aop=debug logging.level.com.exadel=trace \ No newline at end of file diff --git a/easy-abac-demo/src/main/resources/templates/403.html b/easy-abac-demo/src/main/resources/templates/403.html index 34417c5..3239b63 100644 --- a/easy-abac-demo/src/main/resources/templates/403.html +++ b/easy-abac-demo/src/main/resources/templates/403.html @@ -1,3 +1,19 @@ + +