Search in sources :

Example 31 with PermissionService

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

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)

Example 32 with PermissionService

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

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());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) ImportContext(com.infiniteautomation.mango.emport.ImportContext) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) Test(org.junit.Test)

Example 33 with PermissionService

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

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 34 with PermissionService

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

the class ScriptEventHandlerVO method setScriptRoleXids.

private void setScriptRoleXids(Set<String> xids) {
    PermissionService permissionService = Common.getBean(PermissionService.class);
    this.scriptRoles = xids.stream().map(xid -> {
        Role r = permissionService.getRole(xid);
        return r != null ? r : null;
    }).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) ObjectInputStream(java.io.ObjectInputStream) Set(java.util.Set) IOException(java.io.IOException) MangoScript(com.infiniteautomation.mango.spring.script.MangoScript) StringMangoScript(com.infiniteautomation.mango.spring.script.StringMangoScript) Collectors(java.util.stream.Collectors) JsonProperty(com.serotonin.json.spi.JsonProperty) Objects(java.util.Objects) ObjectOutputStream(java.io.ObjectOutputStream) Collections(java.util.Collections) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Objects(java.util.Objects)

Example 35 with PermissionService

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

the class ScriptEventHandlerVO method setScriptRoleXids.

private void setScriptRoleXids(Set<String> xids) {
    PermissionService permissionService = Common.getBean(PermissionService.class);
    this.scriptRoles = xids.stream().map(xid -> {
        Role r = permissionService.getRole(xid);
        return r != null ? r : null;
    }).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) ObjectInputStream(java.io.ObjectInputStream) Set(java.util.Set) IOException(java.io.IOException) MangoScript(com.infiniteautomation.mango.spring.script.MangoScript) StringMangoScript(com.infiniteautomation.mango.spring.script.StringMangoScript) Collectors(java.util.stream.Collectors) JsonProperty(com.serotonin.json.spi.JsonProperty) Objects(java.util.Objects) ObjectOutputStream(java.io.ObjectOutputStream) Collections(java.util.Collections) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Objects(java.util.Objects)

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