use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class RoleInheritanceTest method test3.
/**
* Test a slightly more complex tree hierarchy with multiple inheritance
*
* <pre>{@code
* gasTechnician -> gasRead <- gasEdit <- gasAdmin
* technician -> solidTechnician -> solidRead <- solidEdit <- solidAdmin <- admin
* fluidTechnician -> fluidRead <- fluidEdit <- fluidAdmin
* }</pre>
*/
@Test
public void test3() {
PermissionService service = Common.getBean(PermissionService.class);
// Fluid roles
RoleVOHierarchy fluidRead = createRoleVOHierarchy("fluidRead", "fluidRead");
MangoPermission fluidReadPermission = MangoPermission.requireAnyRole(fluidRead.getRole());
RoleVOHierarchy fluidEdit = createRoleVOHierarchy("fluidEdit", "fluidEdit", fluidRead);
MangoPermission fluidEditPermission = MangoPermission.requireAnyRole(fluidEdit.getRole());
RoleVOHierarchy fluidTechnician = createRoleVOHierarchy("fluidTechnician", "fluidTechnician", fluidRead);
RoleVOHierarchy fluidAdmin = createRoleVOHierarchy("fluidAdmin", "fluidAdmin", fluidEdit);
// Solid roles
RoleVOHierarchy solidRead = createRoleVOHierarchy("solidRead", "solidRead");
MangoPermission solidReadPermission = MangoPermission.requireAnyRole(solidRead.getRole());
RoleVOHierarchy solidEdit = createRoleVOHierarchy("solidEdit", "solidEdit", solidRead);
MangoPermission solidEditPermission = MangoPermission.requireAnyRole(solidEdit.getRole());
RoleVOHierarchy solidTechnician = createRoleVOHierarchy("solidTechnician", "solidTechnician", solidRead);
RoleVOHierarchy solidAdmin = createRoleVOHierarchy("solidAdmin", "solidAdmin", solidEdit);
// Gas roles
RoleVOHierarchy gasRead = createRoleVOHierarchy("gasRead", "gasRead");
MangoPermission gasReadPermission = MangoPermission.requireAnyRole(gasRead.getRole());
RoleVOHierarchy gasEdit = createRoleVOHierarchy("gasEdit", "gasEdit", gasRead);
MangoPermission gasEditPermission = MangoPermission.requireAnyRole(gasEdit.getRole());
RoleVOHierarchy gasTechnician = createRoleVOHierarchy("gasTechnician", "gasTechnician", gasRead);
RoleVOHierarchy gasAdmin = createRoleVOHierarchy("gasAdmin", "gasAdmin", gasEdit);
RoleVOHierarchy technician = createRoleVOHierarchy("technician", "technician", fluidTechnician, solidTechnician, gasTechnician);
RoleVOHierarchy admin = createRoleVOHierarchy("admin", "admin", fluidAdmin, solidAdmin, gasAdmin);
// Who can read fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidRead), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidAdmin), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), fluidReadPermission));
// Who cannot read fluid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasAdmin), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidAdmin), fluidReadPermission));
// Who can edit fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidAdmin), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), fluidEditPermission));
// Who cannot edit fluid
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), fluidEditPermission));
// Who can read solid
assertTrue(service.hasPermission(createPermissionHolder(solidRead), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidAdmin), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), solidReadPermission));
// Who cannot read solid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasAdmin), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidAdmin), solidReadPermission));
// Who can edit solid
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidAdmin), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), solidEditPermission));
// Who cannot edit solid
assertFalse(service.hasPermission(createPermissionHolder(solidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), solidEditPermission));
// Who can read gas
assertTrue(service.hasPermission(createPermissionHolder(gasRead), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasAdmin), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), gasReadPermission));
// Who cannot read gas
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidAdmin), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidAdmin), gasReadPermission));
// Who can edit gas
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasAdmin), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), gasEditPermission));
// Who cannot edit gas
assertFalse(service.hasPermission(createPermissionHolder(gasRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(technician), gasEditPermission));
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class PermissionPersistenceTest method testAndPermission.
@Test
public void testAndPermission() {
PermissionService service = Common.getBean(PermissionService.class);
// Create some roles
Set<Role> roles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
// insert the permission
MangoPermission permission = service.findOrCreate(MangoPermission.requireAllRoles(roles));
MangoPermission read = service.get(permission.getId());
assertEquals(1, read.getRoles().size());
Iterator<Set<Role>> it = read.getRoles().iterator();
assertEquals(2, it.next().size());
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class RoleInheritanceTest method test1.
/**
* Test a straight hierarchy with only single inheritance
*
* <pre>{@code
* admin -> supervisor -> edit -> read
* }</pre>
*/
@Test
public void test1() {
PermissionService service = Common.getBean(PermissionService.class);
// Lowest level
RoleVOHierarchy read = createRoleVOHierarchy("read", "read");
MangoPermission readPermission = MangoPermission.requireAnyRole(read.getRole());
RoleVOHierarchy edit = createRoleVOHierarchy("edit", "edit", read);
MangoPermission editPermission = MangoPermission.requireAnyRole(edit.getRole());
RoleVOHierarchy supervisor = createRoleVOHierarchy("supervisor", "supervisor", edit);
MangoPermission supervisorPermission = MangoPermission.requireAnyRole(supervisor.getRole());
RoleVOHierarchy admin = createRoleVOHierarchy("admin", "admin", supervisor);
MangoPermission adminPermission = MangoPermission.requireAnyRole(admin.getRole());
// What admin can do
assertTrue(service.hasPermission(createPermissionHolder(admin), readPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), editPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), supervisorPermission));
assertTrue(service.hasPermission(createPermissionHolder(admin), adminPermission));
// What supervisor can do
assertTrue(service.hasPermission(createPermissionHolder(supervisor), readPermission));
assertTrue(service.hasPermission(createPermissionHolder(supervisor), editPermission));
assertTrue(service.hasPermission(createPermissionHolder(supervisor), supervisorPermission));
assertFalse(service.hasPermission(createPermissionHolder(supervisor), adminPermission));
// What edit can do
assertTrue(service.hasPermission(createPermissionHolder(edit), readPermission));
assertTrue(service.hasPermission(createPermissionHolder(edit), editPermission));
assertFalse(service.hasPermission(createPermissionHolder(edit), supervisorPermission));
assertFalse(service.hasPermission(createPermissionHolder(edit), adminPermission));
// What read can do
assertTrue(service.hasPermission(createPermissionHolder(read), readPermission));
assertFalse(service.hasPermission(createPermissionHolder(read), editPermission));
assertFalse(service.hasPermission(createPermissionHolder(read), supervisorPermission));
assertFalse(service.hasPermission(createPermissionHolder(read), adminPermission));
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class RoleInheritanceTest method test2.
/**
* Test a tree hierarchy with multiple inheritance
*
* <pre>{@code
* -> gasTechnician -> gasEdit -> gasRead
* technician -> solidTechnician -> solidEdit -> solidRead
* -> fluidTechnician -> fluidEdit -> fluidRead
* }</pre>
*/
@Test
public void test2() {
PermissionService service = Common.getBean(PermissionService.class);
RoleVOHierarchy fluidRead = createRoleVOHierarchy("fluidRead", "fluidRead");
MangoPermission fluidReadPermission = MangoPermission.requireAnyRole(fluidRead.getRole());
RoleVOHierarchy fluidEdit = createRoleVOHierarchy("fluidEdit", "fluidEdit", fluidRead);
MangoPermission fluidEditPermission = MangoPermission.requireAnyRole(fluidEdit.getRole());
RoleVOHierarchy solidRead = createRoleVOHierarchy("solidRead", "solidRead");
MangoPermission solidReadPermission = MangoPermission.requireAnyRole(solidRead.getRole());
RoleVOHierarchy solidEdit = createRoleVOHierarchy("solidEdit", "solidEdit", solidRead);
MangoPermission solidEditPermission = MangoPermission.requireAnyRole(solidEdit.getRole());
RoleVOHierarchy gasRead = createRoleVOHierarchy("gasRead", "gasRead");
MangoPermission gasReadPermission = MangoPermission.requireAnyRole(gasRead.getRole());
RoleVOHierarchy gasEdit = createRoleVOHierarchy("gasEdit", "gasEdit", gasRead);
MangoPermission gasEditPermission = MangoPermission.requireAnyRole(gasEdit.getRole());
RoleVOHierarchy fluidTechnician = createRoleVOHierarchy("fluidTechnician", "fluidTechnician", fluidEdit);
RoleVOHierarchy solidTechnician = createRoleVOHierarchy("solidTechnician", "solidTechnician", solidEdit);
RoleVOHierarchy gasTechnician = createRoleVOHierarchy("gasTechnician", "gasTechnician", gasEdit);
RoleVOHierarchy technician = createRoleVOHierarchy("technician", "technician", fluidTechnician, solidTechnician, gasTechnician);
// Who can read fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidRead), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidReadPermission));
// Who can edit fluid
assertTrue(service.hasPermission(createPermissionHolder(fluidEdit), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(fluidTechnician), fluidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), fluidEditPermission));
// Who cannot read fluid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidReadPermission));
// Who cannot edit fluid
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), fluidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), fluidEditPermission));
// Who can read solid
assertTrue(service.hasPermission(createPermissionHolder(solidRead), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidReadPermission));
// Who can edit solid
assertTrue(service.hasPermission(createPermissionHolder(solidEdit), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(solidTechnician), solidEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), solidEditPermission));
// Who cannot read solid
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidReadPermission));
// Who cannot edit solid
assertFalse(service.hasPermission(createPermissionHolder(solidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(gasTechnician), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), solidEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), solidEditPermission));
// Who can read gas
assertTrue(service.hasPermission(createPermissionHolder(gasRead), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasReadPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasReadPermission));
// Who can edit gas
assertTrue(service.hasPermission(createPermissionHolder(gasEdit), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(gasTechnician), gasEditPermission));
assertTrue(service.hasPermission(createPermissionHolder(technician), gasEditPermission));
// Who cannot read gas
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasReadPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasReadPermission));
// Who cannot edit gas
assertFalse(service.hasPermission(createPermissionHolder(gasRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(fluidTechnician), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidRead), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidEdit), gasEditPermission));
assertFalse(service.hasPermission(createPermissionHolder(solidTechnician), gasEditPermission));
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class LazyFieldJsonTest method testLazyPermissionInObject.
@Test
public void testLazyPermissionInObject() {
RoleService roleService = Common.getBean(RoleService.class);
PermissionService permissionService = Common.getBean(PermissionService.class);
Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
LazyContainer container = new LazyContainer();
container.supplyPermission(() -> MangoPermission.builder().minterm(role1, role2).build());
try (StringWriter stringWriter = new StringWriter()) {
JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
JsonValue value = typeWriter.writeObject(container);
writer.setPrettyIndent(0);
writer.setPrettyOutput(true);
writer.writeObject(value);
String json = stringWriter.toString();
JsonTypeReader typeReader = new JsonTypeReader(json);
JsonValue read = typeReader.read();
JsonObject root = read.toJsonObject();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
LazyContainer readContainer = new LazyContainer();
context.getReader().readInto(readContainer, root);
assertEquals(container.getPermission(), readContainer.getPermission());
} catch (IOException | JsonException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations