Search in sources :

Example 61 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

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 62 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class JsonDataImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    JsonDataVO vo = null;
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        isNew = true;
        vo = new JsonDataVO();
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // Ensure we have a default permission since null is valid in Mango 3.x
            if (vo.getReadPermission() == null) {
                vo.setReadPermission(new MangoPermission());
            }
            if (vo.getEditPermission() == null) {
                vo.setEditPermission(new MangoPermission());
            }
            if (isNew) {
                service.insert(vo);
            } else {
                service.update(vo.getId(), vo);
            }
            addSuccessMessage(isNew, "emport.jsondata.prefix", xid);
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.jsondata.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 63 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerServiceTest method addRoleToCreatePermission.

void addRoleToCreatePermission(Role vo) {
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(EventHandlerCreatePermission.PERMISSION);
    Set<Set<Role>> roleSet = def.getPermission().getRoles();
    Set<Set<Role>> newRoles = new HashSet<>();
    newRoles.add(Collections.singleton(vo));
    for (Set<Role> roles : roleSet) {
        newRoles.add(new HashSet<>(roles));
    }
    Common.getBean(SystemPermissionService.class).update(new MangoPermission(newRoles), def);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) HashSet(java.util.HashSet) Set(java.util.Set) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 64 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class AbstractVOServiceWithPermissionsTest method removeRoleFromCreatePermission.

void removeRoleFromCreatePermission(Role vo) {
    String permissionType = getCreatePermissionType();
    if (permissionType != null) {
        PermissionDefinition def = ModuleRegistry.getPermissionDefinition(getCreatePermissionType());
        MangoPermission permission = def.getPermission();
        Common.getBean(SystemPermissionService.class).update(new MangoPermission(permission.withoutRole(vo).getRoles()), def);
    }
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 65 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class AbstractBasicVOServiceWithPermissionsTestBase method addRoleToCreatePermission.

void addRoleToCreatePermission(Role vo) {
    String permissionType = getCreatePermissionType();
    if (permissionType != null) {
        PermissionDefinition def = ModuleRegistry.getPermissionDefinition(getCreatePermissionType());
        Set<Set<Role>> roleSet = def.getPermission().getRoles();
        Set<Set<Role>> newRoles = new HashSet<>();
        newRoles.add(Collections.singleton(vo));
        for (Set<Role> roles : roleSet) {
            newRoles.add(new HashSet<>(roles));
        }
        Common.getBean(SystemPermissionService.class).update(new MangoPermission(newRoles), def);
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)93 Role (com.serotonin.m2m2.vo.role.Role)22 HashSet (java.util.HashSet)21 Set (java.util.Set)18 HashMap (java.util.HashMap)13 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)12 PermissionDefinition (com.serotonin.m2m2.module.PermissionDefinition)11 JsonException (com.serotonin.json.JsonException)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)9 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)8 JsonValue (com.serotonin.json.type.JsonValue)7 ArrayList (java.util.ArrayList)7 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 User (com.serotonin.m2m2.vo.User)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4