Search in sources :

Example 1 with Builder

use of io.crnk.security.SecurityConfig.Builder in project crnk-framework by crnk-project.

the class SecurityConfigTest method test.

@Test
public void test() {
    // tag::docs[]
    Builder builder = SecurityConfig.builder();
    builder.permitAll(ResourcePermission.GET);
    builder.permitAll(Task.class, ResourcePermission.DELETE);
    builder.permitAll("projects", ResourcePermission.PATCH);
    builder.permitRole("someRole", ResourcePermission.GET);
    builder.permitRole("someRole", Task.class, ResourcePermission.DELETE);
    builder.permitRole("someRole", "projects", ResourcePermission.PATCH);
    SecurityConfig config = builder.build();
    // end::docs[]
    List<SecurityRule> rules = config.getRules();
    Assert.assertEquals(6, rules.size());
    Assert.assertEquals(ResourcePermission.GET, rules.get(0).getPermission());
    Assert.assertEquals(ResourcePermission.DELETE, rules.get(1).getPermission());
    Assert.assertEquals(ResourcePermission.PATCH, rules.get(2).getPermission());
    Assert.assertEquals(ResourcePermission.GET, rules.get(3).getPermission());
    Assert.assertEquals(ResourcePermission.DELETE, rules.get(4).getPermission());
    Assert.assertEquals(ResourcePermission.PATCH, rules.get(5).getPermission());
    Assert.assertNull(rules.get(0).getRole());
    Assert.assertNull(rules.get(1).getRole());
    Assert.assertNull(rules.get(2).getRole());
    Assert.assertEquals("someRole", rules.get(3).getRole());
    Assert.assertEquals("someRole", rules.get(4).getRole());
    Assert.assertEquals("someRole", rules.get(5).getRole());
    Assert.assertNull(rules.get(0).getResourceClass());
    Assert.assertEquals(Task.class, rules.get(1).getResourceClass());
    Assert.assertEquals("projects", rules.get(2).getResourceType());
    Assert.assertNull(rules.get(3).getResourceClass());
    Assert.assertEquals(Task.class, rules.get(4).getResourceClass());
    Assert.assertEquals("projects", rules.get(5).getResourceType());
}
Also used : Builder(io.crnk.security.SecurityConfig.Builder) Test(org.junit.Test)

Example 2 with Builder

use of io.crnk.security.SecurityConfig.Builder in project crnk-framework by crnk-project.

the class SecurityModuleTest method testReconfigure.

@Test
public void testReconfigure() {
    Assert.assertTrue(securityModule.isAllowed(Project.class, ResourcePermission.GET));
    Assert.assertFalse(securityModule.isAllowed(Project.class, ResourcePermission.DELETE));
    Builder builder = SecurityConfig.builder();
    builder.permitRole(allowedRule, "projects", ResourcePermission.DELETE);
    securityModule.reconfigure(builder.build());
    Assert.assertFalse(securityModule.isAllowed(Project.class, ResourcePermission.GET));
    Assert.assertTrue(securityModule.isAllowed(Project.class, ResourcePermission.DELETE));
}
Also used : Project(io.crnk.security.model.Project) Builder(io.crnk.security.SecurityConfig.Builder) Test(org.junit.Test)

Example 3 with Builder

use of io.crnk.security.SecurityConfig.Builder in project crnk-framework by crnk-project.

the class SecurityModuleTest method setup.

@Before
public void setup() {
    // TODO simplify ones simple module is fixed
    SimpleModule appModule = new SimpleModule("app") {

        @Override
        public void setupModule(ModuleContext context) {
            super.setupModule(context);
            context.addSecurityProvider(new SecurityProvider() {

                @Override
                public boolean isUserInRole(String role) {
                    return role.equals(allowedRule);
                }
            });
        }
    };
    appModule.addRepository(new TaskRepository());
    appModule.addRepository(new ProjectRepository());
    Builder builder = SecurityConfig.builder();
    builder.permitAll(ResourcePermission.GET);
    builder.permitRole("taskRole", Task.class, ResourcePermission.ALL);
    builder.permitRole("projectRole", "projects", ResourcePermission.POST);
    SecurityConfig config = builder.build();
    securityModule = SecurityModule.newServerModule(config);
    Assert.assertSame(config, securityModule.getConfig());
    ModuleRegistry moduleRegistry = new ModuleRegistry();
    moduleRegistry.setServiceDiscovery(new EmptyServiceDiscovery());
    moduleRegistry.setResourceRegistry(new ResourceRegistryImpl(new DefaultResourceRegistryPart(), moduleRegistry));
    moduleRegistry.addModule(securityModule);
    moduleRegistry.addModule(appModule);
    moduleRegistry.addModule(new JacksonModule(new ObjectMapper(), false, ImmutableList.of(new OffsetLimitPagingBehavior())));
    moduleRegistry.addModule(new CoreModule());
    moduleRegistry.init(new ObjectMapper());
}
Also used : OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ProjectRepository(io.crnk.security.model.ProjectRepository) TaskRepository(io.crnk.security.model.TaskRepository) Builder(io.crnk.security.SecurityConfig.Builder) ModuleRegistry(io.crnk.core.module.ModuleRegistry) ResourceRegistryImpl(io.crnk.core.engine.internal.registry.ResourceRegistryImpl) JacksonModule(io.crnk.core.engine.internal.jackson.JacksonModule) SecurityProvider(io.crnk.core.engine.security.SecurityProvider) DefaultResourceRegistryPart(io.crnk.core.engine.registry.DefaultResourceRegistryPart) CoreModule(io.crnk.core.engine.internal.CoreModule) SimpleModule(io.crnk.core.module.SimpleModule) EmptyServiceDiscovery(io.crnk.core.module.discovery.EmptyServiceDiscovery) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Aggregations

Builder (io.crnk.security.SecurityConfig.Builder)3 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CoreModule (io.crnk.core.engine.internal.CoreModule)1 JacksonModule (io.crnk.core.engine.internal.jackson.JacksonModule)1 ResourceRegistryImpl (io.crnk.core.engine.internal.registry.ResourceRegistryImpl)1 DefaultResourceRegistryPart (io.crnk.core.engine.registry.DefaultResourceRegistryPart)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ModuleRegistry (io.crnk.core.module.ModuleRegistry)1 SimpleModule (io.crnk.core.module.SimpleModule)1 EmptyServiceDiscovery (io.crnk.core.module.discovery.EmptyServiceDiscovery)1 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)1 Project (io.crnk.security.model.Project)1 ProjectRepository (io.crnk.security.model.ProjectRepository)1 TaskRepository (io.crnk.security.model.TaskRepository)1 Before (org.junit.Before)1