Search in sources :

Example 6 with PermissionService

use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.

the class PermissionPersistenceTest method testOrPermission.

@Test
public void testOrPermission() {
    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.requireAnyRole(roles));
    MangoPermission read = service.get(permission.getId());
    assertEquals(2, read.getRoles().size());
    Iterator<Set<Role>> it = read.getRoles().iterator();
    assertEquals(1, it.next().size());
    assertEquals(1, it.next().size());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Role(com.serotonin.m2m2.vo.role.Role) Iterator(java.util.Iterator) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with PermissionService

use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.

the class PermissionPersistenceTest method testOrphanedPermission.

/**
 *  TODO we cannot remove permissions with empty minterms as of the FK RESTRICT on VOs
 *  Test when removing a role we also delete any orphaned permission (has no roles in minterms)
 *  Delete role then check each used minterms to see if empty
 */
@Test
public void testOrphanedPermission() {
    PermissionService service = Common.getBean(PermissionService.class);
    // insert some roles
    Set<Role> roles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    // insert the permission
    MangoPermission permission = service.findOrCreate(MangoPermission.requireAnyRole(roles));
    MangoPermission read = service.get(permission.getId());
    assertEquals(2, read.getRoles().size());
    // Delete all roles in minterm
    Iterator<Set<Role>> it = read.getRoles().iterator();
    Set<Role> term1 = it.next();
    Iterator<Role> term1It = term1.iterator();
    Set<Role> term2 = it.next();
    Iterator<Role> term2It = term2.iterator();
    Common.getBean(RoleDao.class).delete(term1It.next().getId());
    Common.getBean(RoleDao.class).delete(term2It.next().getId());
    // Confirm that the permission has no roles
    read = service.get(permission.getId());
    assertEquals(0, read.getRoles().size());
    // Check for orphaned minterm mappings
    List<Integer> mintermIds = getMintermIds(permission.getId());
    assertEquals(0, mintermIds.size());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Role(com.serotonin.m2m2.vo.role.Role) Iterator(java.util.Iterator) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Set(java.util.Set) HashSet(java.util.HashSet) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Test(org.junit.Test)

Example 8 with PermissionService

use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.

the class PermissionPersistenceTest method testModifyPermission.

/**
 * Modify a permission and ensure that it is retrieved correctly from the database after
 */
@Test
public void testModifyPermission() {
    PermissionService service = Common.getBean(PermissionService.class);
    // insert some roles
    Set<Role> roles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    // insert the permission
    MangoPermission permission = service.findOrCreate(MangoPermission.requireAnyRole(roles));
    MangoPermission read = service.get(permission.getId());
    assertEquals(2, read.getRoles().size());
    Iterator<Set<Role>> it = read.getRoles().iterator();
    Role toKeep = it.next().iterator().next();
    MangoPermission keep = service.findOrCreate(MangoPermission.requireAnyRole(toKeep));
    read = service.get(keep.getId());
    assertEquals(1, read.getRoles().size());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Role(com.serotonin.m2m2.vo.role.Role) Iterator(java.util.Iterator) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with PermissionService

use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-modules-public by infiniteautomation.

the class SetPointEventHandlerModel method readInto.

@Override
public void readInto(SetPointEventHandlerVO vo) {
    super.readInto(vo);
    Integer targetId = DataPointDao.getInstance().getIdByXid(targetPointXid);
    if (targetId != null)
        vo.setTargetPointId(targetId);
    vo.setActiveAction(SetPointEventHandlerVO.SET_ACTION_CODES.getId(activeAction));
    if (activeValueToSet != null)
        vo.setActiveValueToSet(activeValueToSet.toString());
    if (activePointXid != null) {
        Integer activePointId = DataPointDao.getInstance().getIdByXid(activePointXid);
        if (activePointId != null)
            vo.setActivePointId(activePointId);
    }
    vo.setActiveScript(activeScript);
    vo.setInactiveAction(SetPointEventHandlerVO.SET_ACTION_CODES.getId(inactiveAction));
    if (inactiveValueToSet != null)
        vo.setInactiveValueToSet(inactiveValueToSet.toString());
    if (inactivePointXid != null) {
        Integer inactivePointId = DataPointDao.getInstance().getIdByXid(inactivePointXid);
        if (inactivePointId != null)
            vo.setInactivePointId(inactivePointId);
    }
    vo.setInactiveScript(inactiveScript);
    PermissionService service = Common.getBean(PermissionService.class);
    vo.setScriptRoles(new ScriptPermissions(service.explodeLegacyPermissionGroupsToRoles(scriptPermissions)));
    if (scriptContext != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (ScriptContextVariableModel var : scriptContext) {
            Integer id = DataPointDao.getInstance().getIdByXid(var.getXid());
            if (id != null) {
                additionalContext.add(new IntStringPair(id, var.getVariableName()));
            } else {
                additionalContext.add(new IntStringPair(Common.NEW_ID, var.getVariableName()));
            }
        }
        vo.setAdditionalContext(additionalContext);
    }
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) IntStringPair(com.serotonin.db.pair.IntStringPair) ScriptContextVariableModel(com.infiniteautomation.mango.rest.latest.model.javascript.MangoJavaScriptModel.ScriptContextVariableModel) ArrayList(java.util.ArrayList) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 10 with PermissionService

use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.

the class RoleDao method joinPermissions.

@Override
public <R extends Record> SelectJoinStep<R> joinPermissions(SelectJoinStep<R> select, PermissionHolder user) {
    PermissionService permissionService = permissionServiceSupplier.get();
    Set<Role> heldRoles = permissionService.getAllInheritedRoles(user);
    if (heldRoles.contains(PermissionHolder.SUPERADMIN_ROLE)) {
        return select;
    }
    List<String> xids = heldRoles.stream().map(Role::getXid).collect(Collectors.toList());
    return select.innerJoin(DSL.selectOne()).on(table.xid.in(xids));
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role)

Aggregations

PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)47 Test (org.junit.Test)32 Role (com.serotonin.m2m2.vo.role.Role)25 Set (java.util.Set)19 HashSet (java.util.HashSet)17 Common (com.serotonin.m2m2.Common)14 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)14 List (java.util.List)13 MangoTestBase (com.serotonin.m2m2.MangoTestBase)12 Collectors (java.util.stream.Collectors)12 PermissionsMinterms (com.infiniteautomation.mango.db.tables.PermissionsMinterms)10 DataSourceService (com.infiniteautomation.mango.spring.service.DataSourceService)10 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)10 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)10 Iterator (java.util.Iterator)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 MutableObject (org.apache.commons.lang3.mutable.MutableObject)10 DSLContext (org.jooq.DSLContext)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 Assert.assertTrue (org.junit.Assert.assertTrue)10