Search in sources :

Example 26 with PermissionService

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

the class EmailEventHandlerModel method readInto.

@Override
public void readInto(EmailEventHandlerVO vo) {
    super.readInto(vo);
    if (activeRecipients != null) {
        List<MailingListRecipient> beans = new ArrayList<>();
        for (EmailRecipientModel model : activeRecipients) beans.add(model.fromModel());
        vo.setActiveRecipients(beans);
    }
    vo.setSendEscalation(sendEscalation);
    vo.setRepeatEscalations(repeatEscalations);
    vo.setEscalationDelayType(Common.TIME_PERIOD_CODES.getId(escalationDelayType));
    vo.setEscalationDelay(escalationDelay);
    if (escalationRecipients != null) {
        List<MailingListRecipient> beans = new ArrayList<>();
        for (EmailRecipientModel model : escalationRecipients) beans.add(model.fromModel());
        vo.setEscalationRecipients(beans);
    }
    vo.setSendInactive(sendInactive);
    vo.setInactiveOverride(inactiveOverride);
    if (inactiveRecipients != null) {
        List<MailingListRecipient> beans = new ArrayList<>();
        for (EmailRecipientModel model : inactiveRecipients) beans.add(model.fromModel());
        vo.setInactiveRecipients(beans);
    }
    vo.setIncludeSystemInfo(includeSystemInfo);
    if (includePointValueCount != null)
        vo.setIncludePointValueCount(includePointValueCount);
    vo.setIncludeLogfile(includeLogfile);
    vo.setCustomTemplate(customTemplate);
    vo.setScript(script);
    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);
    }
    vo.setSubject(EmailEventHandlerVO.SUBJECT_INCLUDE_CODES.getId(subject));
}
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) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) EmailRecipientModel(com.infiniteautomation.mango.rest.latest.model.mailingList.EmailRecipientModel) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 27 with PermissionService

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

the class MangoJavaScriptModel method toVO.

public MangoJavaScript toVO() {
    MangoJavaScript vo = new MangoJavaScript();
    vo.setWrapInFunction(wrapInFunction);
    vo.setContext(convertContext());
    vo.setLogLevel(logLevel);
    PermissionService service = Common.getBean(PermissionService.class);
    vo.setPermissions(new ScriptPermissions(service.explodeLegacyPermissionGroupsToRoles(permissions)));
    if (resultDataType != null)
        vo.setResultDataType(DataType.fromName(resultDataType));
    vo.setScript(script);
    vo.setAdditionalContext(additionalContext);
    return vo;
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) MangoJavaScript(com.infiniteautomation.mango.util.script.MangoJavaScript) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 28 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 29 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 30 with PermissionService

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

the class PermissionPersistenceTest method testComplexPermission.

@Test
public void testComplexPermission() {
    PermissionService service = Common.getBean(PermissionService.class);
    // Create some roles
    Set<Role> minterm1 = this.createRoles(1).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    Set<Role> minterm2 = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    Set<Role> minterm3 = this.createRoles(3).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    // insert the permission
    Set<Set<Role>> minterms = new HashSet<>();
    minterms.add(minterm1);
    minterms.add(minterm2);
    minterms.add(minterm3);
    MangoPermission permission = service.findOrCreate(new MangoPermission(minterms));
    MangoPermission read = service.get(permission.getId());
    assertEquals(3, read.getRoles().size());
    Iterator<Set<Role>> it = read.getRoles().iterator();
    while (it.hasNext()) {
        Set<Role> minterm = it.next();
        switch(minterm.size()) {
            case 1:
                assertEquals(1, minterm.size());
                Role minterm1Role1 = minterm.iterator().next();
                assertTrue(minterm1.contains(minterm1Role1));
                break;
            case 2:
                assertEquals(2, minterm.size());
                Iterator<Role> it2 = minterm.iterator();
                Role minterm2Role1 = it2.next();
                Role minterm2Role2 = it2.next();
                assertTrue(minterm2.contains(minterm2Role1));
                assertTrue(minterm2.contains(minterm2Role2));
                break;
            case 3:
                assertEquals(3, minterm.size());
                Iterator<Role> it3 = minterm.iterator();
                Role minterm3Role1 = it3.next();
                Role minterm3Role2 = it3.next();
                Role minterm3Role3 = it3.next();
                assertTrue(minterm3.contains(minterm3Role1));
                assertTrue(minterm3.contains(minterm3Role2));
                assertTrue(minterm3.contains(minterm3Role3));
                break;
        }
    }
}
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) HashSet(java.util.HashSet) 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