Search in sources :

Example 56 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    String name = json.getString("name");
    RoleVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new RoleVO(Common.NEW_ID, xid, name);
    }
    try {
        // Read into the VO to get all properties
        ctx.getReader().readInto(vo, json);
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        addSuccessMessage(isnew, "emport.role.prefix", xid);
    } catch (ValidationException e) {
        setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
    } catch (JsonException e) {
        addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : JsonException(com.serotonin.json.JsonException) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 57 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class SystemSettingsImporter method importImpl.

@Override
protected void importImpl() {
    try {
        Map<String, Object> settings = new HashMap<String, Object>();
        // Finish reading it in.
        for (String key : json.keySet()) {
            JsonValue value = json.get(key);
            // Don't import null values or database schemas
            if ((value != null) && (!key.startsWith(SystemSettingsDao.DATABASE_SCHEMA_VERSION))) {
                Object o = value.toNative();
                if (o instanceof String) {
                    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(key);
                    if (def != null) {
                        // Legacy permission import
                        try {
                            Set<String> xids = PermissionService.explodeLegacyPermissionGroups((String) o);
                            Set<Set<Role>> roles = new HashSet<>();
                            for (String xid : xids) {
                                RoleVO role = roleService.get(xid);
                                if (role != null) {
                                    roles.add(Collections.singleton(role.getRole()));
                                } else {
                                    roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
                                }
                            }
                            permissionService.update(new MangoPermission(roles), def);
                            addSuccessMessage(false, "emport.permission.prefix", key);
                        } catch (ValidationException e) {
                            setValidationMessages(e.getValidationResult(), "emport.permission.prefix", key);
                            return;
                        }
                    } else {
                        // Could be an export code so try and convert it
                        Integer id = SystemSettingsDao.getInstance().convertToValueFromCode(key, (String) o);
                        if (id != null)
                            settings.put(key, id);
                        else
                            settings.put(key, o);
                    }
                } else {
                    settings.put(key, o);
                }
            }
        }
        // Now validate it. Use a new response object so we can distinguish errors in this vo
        // from
        // other errors.
        ProcessResult voResponse = new ProcessResult();
        SystemSettingsDao.getInstance().validate(settings, voResponse, user);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        else {
            SystemSettingsDao.getInstance().updateSettings(settings);
            addSuccessMessage(false, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        }
    } catch (Exception e) {
        addFailureMessage("emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()), e.getMessage());
    }
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) HashMap(java.util.HashMap) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 58 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method testQuery.

@Test
public void testQuery() {
    List<RoleVO> vos = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        vos.add(insertNewVO(readUser));
    }
    AtomicInteger count = new AtomicInteger();
    service.buildQuery().equal("xid", vos.get(0).getXid()).query(r -> {
        assertVoEqual(vos.get(0), r);
        assertEquals(count.incrementAndGet(), 1);
    });
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 59 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method adminCanSeeAllRoles.

@Test
public void adminCanSeeAllRoles() {
    RoleVO vo = insertNewVO(readUser);
    Set<String> roleXids = service.list().stream().map(AbstractVO::getXid).collect(Collectors.toSet());
    Assert.assertTrue("Should see anonymous role", roleXids.contains(PermissionHolder.ANONYMOUS_ROLE_XID));
    Assert.assertTrue("Should see user role", roleXids.contains(PermissionHolder.USER_ROLE_XID));
    Assert.assertTrue("Should see superadmin role", roleXids.contains(PermissionHolder.SUPERADMIN_ROLE_XID));
    Assert.assertTrue("Superadmin should see all roles", roleXids.contains(vo.getXid()));
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test)

Example 60 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method cannotInsertSuperadminRole.

@Test
@ExpectValidationException("xid")
public void cannotInsertSuperadminRole() {
    RoleVO vo = new RoleVO(Common.NEW_ID, PermissionHolder.SUPERADMIN_ROLE_XID, "Superadmin default");
    service.insert(vo);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Aggregations

RoleVO (com.serotonin.m2m2.vo.role.RoleVO)58 Test (org.junit.Test)34 Role (com.serotonin.m2m2.vo.role.Role)33 HashSet (java.util.HashSet)17 RoleService (com.infiniteautomation.mango.spring.service.RoleService)14 User (com.serotonin.m2m2.vo.User)11 ArrayList (java.util.ArrayList)11 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)8 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)7 JsonValue (com.serotonin.json.type.JsonValue)7 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)7 Set (java.util.Set)7 Roles (com.infiniteautomation.mango.db.tables.Roles)6 JsonException (com.serotonin.json.JsonException)6 DSLContext (org.jooq.DSLContext)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)5 JsonObject (com.serotonin.json.type.JsonObject)5 ImportContext (com.infiniteautomation.mango.emport.ImportContext)4 JsonReader (com.serotonin.json.JsonReader)4