Search in sources :

Example 36 with MangoPermission

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

the class Upgrade29 method convertMailingLists.

private void convertMailingLists(OutputStream out) {
    // Add permission id columns
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), mailingListsPermissionMySQL);
    scripts.put(DatabaseType.H2.name(), mailingListsPermissionH2);
    scripts.put(DatabaseType.MSSQL.name(), mailingListsPermissionMSSQL);
    scripts.put(DatabaseType.POSTGRES.name(), mailingListsPermissionMySQL);
    runScript(scripts, out);
    // Move current permissions to roles
    ejt.query("SELECT id, readPermission, editPermission FROM mailingLists", rs -> {
        int voId = rs.getInt(1);
        // Add role/mapping
        MangoPermission readPermissions = PermissionMigration.parseLegacyPermission(rs.getString(2));
        Integer readId = getOrCreatePermission(readPermissions).getId();
        MangoPermission editPermissions = PermissionMigration.parseLegacyPermission(rs.getString(3));
        Integer editId = getOrCreatePermission(editPermissions).getId();
        ejt.update("UPDATE mailingLists SET readPermissionId=?, editPermissionId=? WHERE id=?", readId, editId, voId);
    });
    // Restrict to NOT NULL
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), mailingListsPermissionNotNullMySQL);
    scripts.put(DatabaseType.H2.name(), mailingListsPermissionNotNull);
    scripts.put(DatabaseType.MSSQL.name(), mailingListsPermissionNotNull);
    scripts.put(DatabaseType.POSTGRES.name(), mailingListsPermissionNotNull);
    runScript(scripts, out);
    // Drop old columns
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), mailingListsDropPermissionsSQL);
    scripts.put(DatabaseType.H2.name(), mailingListsDropPermissionsSQL);
    scripts.put(DatabaseType.MSSQL.name(), mailingListsDropPermissionsSQL);
    scripts.put(DatabaseType.POSTGRES.name(), mailingListsDropPermissionsSQL);
    runScript(scripts, out);
}
Also used : HashMap(java.util.HashMap) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 37 with MangoPermission

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

the class Upgrade29 method convertJsonData.

private void convertJsonData(OutputStream out) {
    // Add permission id columns
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), jsonDataPermissionMySQL);
    scripts.put(DatabaseType.H2.name(), jsonDataPermissionH2);
    scripts.put(DatabaseType.MSSQL.name(), jsonDataPermissionMSSQL);
    scripts.put(DatabaseType.POSTGRES.name(), jsonDataPermissionMySQL);
    runScript(scripts, out);
    // Move current permissions to roles
    ejt.query("SELECT id, readPermission, editPermission, publicData FROM jsonData", rs -> {
        int voId = rs.getInt(1);
        // Add role/mapping
        MangoPermission readPermissions = PermissionMigration.parseLegacyPermission(rs.getString(2));
        if (BaseDao.charToBool(rs.getString(4))) {
            // Is public so add anonymous role
            Set<Set<Role>> newRoles = new HashSet<>(readPermissions.getRoles());
            newRoles.add(Collections.singleton(PermissionHolder.USER_ROLE));
            newRoles.add(Collections.singleton(PermissionHolder.ANONYMOUS_ROLE));
            readPermissions = new MangoPermission(newRoles);
        }
        Integer readId = getOrCreatePermission(readPermissions).getId();
        MangoPermission editPermissions = PermissionMigration.parseLegacyPermission(rs.getString(3));
        Integer editId = getOrCreatePermission(editPermissions).getId();
        ejt.update("UPDATE jsonData SET readPermissionId=?, editPermissionId=? WHERE id=?", readId, editId, voId);
    });
    // DROP publicData column
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), jsonDataDropPublicDataSQL);
    scripts.put(DatabaseType.H2.name(), jsonDataDropPublicDataSQL);
    scripts.put(DatabaseType.MSSQL.name(), jsonDataDropPublicDataSQL);
    scripts.put(DatabaseType.POSTGRES.name(), jsonDataDropPublicDataSQL);
    runScript(scripts, out);
    // Restrict to NOT NULL
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), jsonDataPermissionNotNullMySQL);
    scripts.put(DatabaseType.H2.name(), jsonDataPermissionNotNull);
    scripts.put(DatabaseType.MSSQL.name(), jsonDataPermissionNotNull);
    scripts.put(DatabaseType.POSTGRES.name(), jsonDataPermissionNotNull);
    runScript(scripts, out);
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), jsonDataDropPermissionsSQL);
    scripts.put(DatabaseType.H2.name(), jsonDataDropPermissionsSQL);
    scripts.put(DatabaseType.MSSQL.name(), jsonDataDropPermissionsSQL);
    scripts.put(DatabaseType.POSTGRES.name(), jsonDataDropPermissionsSQL);
    runScript(scripts, out);
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 38 with MangoPermission

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

the class UserDao method deletePostRelationalData.

@Override
public void deletePostRelationalData(User vo) {
    super.deletePostRelationalData(vo);
    MangoPermission readPermission = vo.getReadPermission();
    MangoPermission editPermission = vo.getEditPermission();
    permissionService.deletePermissions(readPermission, editPermission);
}
Also used : MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 39 with MangoPermission

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

the class PermissionMigration method getOrCreatePermissionNoCache.

default MangoPermission getOrCreatePermissionNoCache(MangoPermission permission) {
    if (permission.getId() != null) {
        return permission;
    }
    return getTransactionTemplate().execute(tx -> {
        Set<Integer> mintermIds = new HashSet<>();
        Set<Set<Role>> minterms = new HashSet<>();
        for (Set<Role> minterm : permission.getRoles()) {
            Set<Role> savedRoles = new HashSet<>();
            for (Role role : minterm) {
                savedRoles.add(getOrCreateRole(role));
            }
            mintermIds.add(getOrCreateMinterm(savedRoles));
        }
        Integer id = getOrCreatePermission(mintermIds);
        MangoPermission saved = new MangoPermission(minterms);
        return saved.withId(id);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 40 with MangoPermission

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

the class ScriptService method evalScript.

private EvalResult evalScript(ScriptAndEngine scriptAndEngine, EvalContext evalContext) {
    MangoScript script = scriptAndEngine.script;
    ScriptEngine engine = scriptAndEngine.engine;
    ScriptEngineDefinition engineDefinition = scriptAndEngine.engineDefinition;
    ScriptContext context = engine.getContext();
    Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    engineBindings.putAll(evalContext.bindings);
    context.setWriter(evalContext.writer);
    context.setErrorWriter(evalContext.errorWriter);
    context.setReader(evalContext.reader);
    String scriptFilename = script.getScriptFilename();
    if (scriptFilename != null) {
        engineBindings.put(ScriptEngine.FILENAME, scriptFilename);
    } else {
        String scriptName = script.getScriptName();
        String fileName = scriptName;
        List<String> extensions = engine.getFactory().getExtensions();
        boolean hasExtension = extensions.stream().anyMatch(e -> scriptName.endsWith("." + e));
        if (!hasExtension && !extensions.isEmpty()) {
            fileName += "." + extensions.get(0);
        }
        engineBindings.put(ScriptEngine.FILENAME, fileName);
    }
    for (ScriptBindingsDefinition bindingsDef : bindingsDefinitions) {
        MangoPermission permission = bindingsDef.requiredPermission();
        if (permissionService.hasPermission(script, permission)) {
            bindingsDef.addBindings(script, engineBindings, scriptAndEngine.synchronizationObject, engineDefinition);
        }
    }
    return this.runAs.runAs(script, () -> {
        try {
            Object value;
            if (script instanceof CompiledMangoScript) {
                value = ((CompiledMangoScript) script).compiled.eval();
            } else {
                try (Reader reader = script.readScript()) {
                    value = engine.eval(reader);
                }
            }
            return new EvalResult(value, engineBindings);
        } catch (ScriptException e) {
            throw new ScriptEvalException(e, engineDefinition.extractSourceLocation(e));
        } catch (IOException e) {
            throw new ScriptIOException(e);
        }
    });
}
Also used : ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) ScriptBindingsDefinition(com.serotonin.m2m2.module.ScriptBindingsDefinition) ScriptException(javax.script.ScriptException) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptEvalException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)

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