Search in sources :

Example 21 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class RoleDao method saveRelationalData.

@Override
public void saveRelationalData(RoleVO existing, RoleVO vo) {
    if (existing != null) {
        // Drop the mappings
        this.create.deleteFrom(RoleInheritance.ROLE_INHERITANCE).where(RoleInheritance.ROLE_INHERITANCE.roleId.eq(vo.getId())).execute();
    }
    if (vo.getInherited() != null && vo.getInherited().size() > 0) {
        List<Query> inserts = new ArrayList<>();
        for (Role role : vo.getInherited()) {
            inserts.add(DSL.insertInto(RoleInheritance.ROLE_INHERITANCE).columns(RoleInheritance.ROLE_INHERITANCE.roleId, RoleInheritance.ROLE_INHERITANCE.inheritedRoleId).values(vo.getId(), role.getId()));
        }
        create.batch(inserts).execute();
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) Query(org.jooq.Query) ArrayList(java.util.ArrayList)

Example 22 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class RoleDao method joinPermissions.

@Override
public <R extends Record> SelectJoinStep<R> joinPermissions(SelectJoinStep<R> select, PermissionHolder user) {
    PermissionService permissionService = permissionServiceSupplier.get();
    Set<Role> heldRoles = permissionService.getAllInheritedRoles(user);
    if (heldRoles.contains(PermissionHolder.SUPERADMIN_ROLE)) {
        return select;
    }
    List<String> xids = heldRoles.stream().map(Role::getXid).collect(Collectors.toList());
    return select.innerJoin(DSL.selectOne()).on(table.xid.in(xids));
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role)

Example 23 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class RoleDao method addRolesThatInherit.

/**
 * Recursively add all roles that inherit this role
 */
private void addRolesThatInherit(Role role, Set<Role> all) {
    Set<Role> inherited = getRolesThatInherit(role.getId());
    for (Role inheritedRole : inherited) {
        all.add(inheritedRole);
        addRolesThatInherit(inheritedRole, all);
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role)

Example 24 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class Upgrade29 method upgradeEventHandlers.

private void upgradeEventHandlers(OutputStream out) {
    // Add permission id columns
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), eventHandlersPermissionMySQL);
    scripts.put(DatabaseType.H2.name(), eventHandlersPermissionH2);
    scripts.put(DatabaseType.MSSQL.name(), eventHandlersPermissionMSSQL);
    scripts.put(DatabaseType.POSTGRES.name(), eventHandlersPermissionMySQL);
    runScript(scripts, out);
    // set permission to superadmin
    Integer readId = getOrCreatePermission(MangoPermission.superadminOnly()).getId();
    Integer editId = getOrCreatePermission(MangoPermission.superadminOnly()).getId();
    ejt.update("UPDATE eventHandlers SET readPermissionId=?, editPermissionId=?", readId, editId);
    // Restrict to NOT NULL
    scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), eventHandlersPermissionNotNullMySQL);
    scripts.put(DatabaseType.H2.name(), eventHandlersPermissionNotNull);
    scripts.put(DatabaseType.MSSQL.name(), eventHandlersPermissionNotNull);
    scripts.put(DatabaseType.POSTGRES.name(), eventHandlersPermissionNotNull);
    runScript(scripts, out);
    // Upgrade Email Event Handlers to fix the script permission serialization, we don't need to clean the
    // recipient list here as we aren't going to use it.
    this.ejt.query("SELECT eh.id, eh.data FROM eventHandlers eh WHERE eh.eventHandlerType=?", new Object[] { EmailEventHandlerDefinition.TYPE_NAME }, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            int id = rs.getInt(1);
            EmailEventHandlerVO vo = (EmailEventHandlerVO) SerializationHelper.readObjectInContext(rs.getBinaryStream(2));
            Set<String> legacyScriptRoles = vo.getLegacyScriptRoles();
            if (legacyScriptRoles != null) {
                Set<Role> roles = new HashSet<>();
                for (String r : legacyScriptRoles) {
                    roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
                }
                vo.setScriptRoles(new ScriptPermissions(roles, vo.getLegacyPermissionHolderName()));
            } else {
                // Must be a ScriptPermission that might need to be upgraded
                ScriptPermissions permission = vo.getScriptRoles();
                if (permission.getLegacyScriptRoles() != null) {
                    Set<Role> roles = new HashSet<>();
                    for (String r : permission.getLegacyScriptRoles()) {
                        roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
                    }
                    ScriptPermissions upgraded = new ScriptPermissions(roles, permission.getPermissionHolderName());
                    vo.setScriptRoles(upgraded);
                }
            }
            ejt.update("UPDATE eventHandlers SET data=? where id=?", SerializationHelper.writeObjectToArray(vo), id);
        }
    });
    // Upgrade Set Point Event Handlers
    this.ejt.query("SELECT eh.id, eh.data FROM eventHandlers eh WHERE eh.eventHandlerType=?", new Object[] { SetPointEventHandlerDefinition.TYPE_NAME }, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            int id = rs.getInt(1);
            SetPointEventHandlerVO vo = (SetPointEventHandlerVO) SerializationHelper.readObjectInContext(rs.getBinaryStream(2));
            Set<String> legacyScriptRoles = vo.getLegacyScriptRoles();
            if (legacyScriptRoles != null) {
                Set<Role> roles = new HashSet<>();
                for (String r : legacyScriptRoles) {
                    roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
                }
                vo.setScriptRoles(new ScriptPermissions(roles, vo.getLegacyPermissionHolderName()));
            } else {
                // Must be a ScriptPermission that might need to be upgraded
                ScriptPermissions permission = vo.getScriptRoles();
                if (permission.getLegacyScriptRoles() != null) {
                    Set<Role> roles = new HashSet<>();
                    for (String r : permission.getLegacyScriptRoles()) {
                        roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
                    }
                    ScriptPermissions upgraded = new ScriptPermissions(roles, permission.getPermissionHolderName());
                    vo.setScriptRoles(upgraded);
                }
            }
            ejt.update("UPDATE eventHandlers SET data=? where id=?", SerializationHelper.writeObjectToArray(vo), id);
        }
    });
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) Role(com.serotonin.m2m2.vo.role.Role) SetPointEventHandlerVO(com.serotonin.m2m2.vo.event.SetPointEventHandlerVO) ResultSet(java.sql.ResultSet) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Example 25 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class Upgrade29 method convertUsers.

private void convertUsers(OutputStream out) {
    // Move current permissions to roles
    ejt.query("SELECT id, permissions FROM users", rs -> {
        int userId = rs.getInt(1);
        // Get user's current permissions
        Set<String> legacyRoleNames = PermissionService.explodeLegacyPermissionGroups(rs.getString(2));
        Set<Role> savedRoles = legacyRoleNames.stream().map(roleName -> new Role(Common.NEW_ID, roleName)).map(this::getOrCreateRole).collect(Collectors.toSet());
        // ensure they have the user role
        savedRoles.add(PermissionHolder.USER_ROLE);
        for (Role role : savedRoles) {
            // Add a mapping
            ejt.doInsert("INSERT INTO userRoleMappings (roleId, userId) VALUES (?,?)", role.getId(), userId);
        }
    });
    // Drop the permissions column
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseType.MYSQL.name(), userSQL);
    scripts.put(DatabaseType.H2.name(), userSQL);
    scripts.put(DatabaseType.MSSQL.name(), userSQL);
    scripts.put(DatabaseType.POSTGRES.name(), userSQL);
    runScript(scripts, out);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) HashMap(java.util.HashMap)

Aggregations

Role (com.serotonin.m2m2.vo.role.Role)102 Test (org.junit.Test)59 HashSet (java.util.HashSet)40 Set (java.util.Set)38 User (com.serotonin.m2m2.vo.User)33 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)23 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)22 Collectors (java.util.stream.Collectors)18 Common (com.serotonin.m2m2.Common)17 MangoTestBase (com.serotonin.m2m2.MangoTestBase)15 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 List (java.util.List)15 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)12 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)12 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)11 DSLContext (org.jooq.DSLContext)11