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());
}
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));
}
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());
}
Aggregations