Search in sources :

Example 41 with PermissionService

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

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)

Example 42 with PermissionService

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

the class MangoPermissionConverter method jsonRead.

@Override
public Object jsonRead(JsonReader reader, JsonValue jsonValue, Type type) throws JsonException {
    Set<Set<Role>> roles = new HashSet<>();
    PermissionService permissionService = Common.getBean(PermissionService.class);
    if (jsonValue instanceof JsonArray) {
        for (JsonValue val : (JsonArray) jsonValue) {
            if (val instanceof JsonArray) {
                Set<Role> inner = new HashSet<>();
                roles.add(inner);
                for (JsonValue v : (JsonArray) val) {
                    Role r = permissionService.getRole(v.toString());
                    if (r != null) {
                        inner.add(r);
                    } else {
                        inner.add(new Role(Common.NEW_ID, v.toString()));
                    }
                }
            } else {
                // Just a single string
                Role r = permissionService.getRole(val.toString());
                if (r != null) {
                    roles.add(Collections.singleton(r));
                } else {
                    roles.add(Collections.singleton(new Role(Common.NEW_ID, val.toString())));
                }
            }
        }
    } else {
        for (String role : PermissionService.explodeLegacyPermissionGroups(jsonValue.toString())) {
            Role r = permissionService.getRole(role);
            if (r != null) {
                roles.add(Collections.singleton(r));
            } else {
                roles.add(Collections.singleton(new Role(Common.NEW_ID, role)));
            }
        }
    }
    return new MangoPermission(roles);
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) JsonArray(com.serotonin.json.type.JsonArray) Role(com.serotonin.m2m2.vo.role.Role) Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(com.serotonin.json.type.JsonValue) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 43 with PermissionService

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

the class ScriptPermissionConverter method jsonRead.

// TODO Mango 4.2 improve performance with lazy field as PermissionService is not available at construct time
@Override
public Object jsonRead(JsonReader reader, JsonValue jsonValue, Type type) throws JsonException {
    Set<Role> roles = new HashSet<>();
    PermissionService service = Common.getBean(PermissionService.class);
    if (jsonValue instanceof JsonArray) {
        for (JsonValue val : (JsonArray) jsonValue) {
            // Just a single string
            Role r = service.getRole(val.toString());
            if (r != null) {
                roles.add(r);
            } else {
                // Let the validation pick this up as a missing role, the response to the user is cleaner
                roles.add(new Role(Common.NEW_ID, val.toString()));
            }
        }
    } else if (jsonValue instanceof JsonObject) {
        // Could be the super-legacy version with 3 separate sets of roles
        JsonObject o = (JsonObject) jsonValue;
        Set<String> permissions = new HashSet<>();
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_SOURCE)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_SET)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_READ)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(CUSTOM)));
        for (String role : permissions) {
            Role r = service.getRole(role);
            if (r != null) {
                roles.add(r);
            } else {
                // Let the validation pick this up as a missing role, the response to the user is cleaner
                roles.add(new Role(Common.NEW_ID, role));
            }
        }
    }
    return new ScriptPermissions(roles);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) JsonArray(com.serotonin.json.type.JsonArray) Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) HashSet(java.util.HashSet)

Example 44 with PermissionService

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

the class DataSourceDaoDeadlockDetection method detectDeadlockFromDataSourceDeleteDataPointInsertAndDelete.

/**
 * See the deadlock when you insert data source, then point,
 *  then delete point (outside of transaction) then delete source
 */
@Test
public void detectDeadlockFromDataSourceDeleteDataPointInsertAndDelete() {
    // Create data source
    // Add data point
    // Delete data source
    // Create data source
    // 25;
    int numThreads = 5;
    // 100;
    int numDataSources = 10;
    PermissionService permissionService = Common.getBean(PermissionService.class);
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    AtomicInteger successes = new AtomicInteger();
    AtomicInteger failures = new AtomicInteger();
    AtomicInteger running = new AtomicInteger(numThreads);
    MutableObject<Exception> failure = new MutableObject<>(null);
    for (int i = 0; i < numThreads; i++) {
        new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numDataSources; i++) {
                        // Create data source
                        MockDataSourceVO ds = new MockDataSourceVO();
                        ds.setName(Common.generateXid("Mock "));
                        dataSourceService.insert(ds);
                        // Create and save point
                        DataPointVO dp = createMockDataPoint(ds, new MockPointLocatorVO());
                        // Delete point
                        dataPointService.delete(dp.getId());
                        // Delete data source
                        dataSourceService.delete(ds.getId());
                        successes.getAndIncrement();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    failure.setValue(e);
                    failures.getAndIncrement();
                } finally {
                    running.decrementAndGet();
                }
            }
        }.start();
    }
    while (running.get() > 0) {
        try {
            Thread.sleep(100);
        } catch (Exception e) {
        }
    }
    if (failures.get() > 0) {
        fail("Ran " + successes.get() + " queries: " + failure.getValue().getMessage());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 45 with PermissionService

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

the class DataSourceDaoDeadlockDetection method detectDeadlockFromDataSourceDeleteDataPointInsert.

/**
 * See the deadlock when you insert data source, then point,
 *  then then delete source (which deletes the point inside a transaction)
 */
@Test
public void detectDeadlockFromDataSourceDeleteDataPointInsert() {
    // 25;
    int numThreads = 5;
    // 100;
    int numDataSources = 10;
    PermissionService permissionService = Common.getBean(PermissionService.class);
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    AtomicInteger failures = new AtomicInteger();
    AtomicInteger successes = new AtomicInteger();
    AtomicInteger running = new AtomicInteger(numThreads);
    MutableObject<Exception> failure = new MutableObject<>(null);
    for (int i = 0; i < numThreads; i++) {
        new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numDataSources; i++) {
                        // Create data source
                        MockDataSourceVO ds = new MockDataSourceVO();
                        ds.setName(Common.generateXid("Mock "));
                        dataSourceService.insert(ds);
                        // Create and save point
                        createMockDataPoint(ds, new MockPointLocatorVO());
                        // Delete data source
                        dataSourceService.delete(ds.getId());
                        successes.getAndIncrement();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    failure.setValue(e);
                    failures.getAndIncrement();
                } finally {
                    running.decrementAndGet();
                }
            }
        }.start();
    }
    while (running.get() > 0) {
        try {
            Thread.sleep(100);
        } catch (Exception e) {
        }
    }
    if (failures.get() > 0) {
        fail("Ran " + successes.get() + " queries: " + failure.getValue().getMessage());
    }
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

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