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();
}
}
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));
}
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);
}
}
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);
}
});
}
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);
}
Aggregations