use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class Upgrade7 method upgrade.
@Override
protected void upgrade() throws Exception {
try (OutputStream out = createUpdateLogOutputStream()) {
// Update advancedSchedules
ejt.query("SELECT id, name, userId, readPermissionId, editPermissionId FROM watchLists", rs -> {
int id = rs.getInt(1);
String name = rs.getString(2);
int userId = rs.getInt(3);
int readPermissionId = rs.getInt(4);
int editPermissionId = rs.getInt(5);
// Is this user non superadmin
AtomicBoolean isAdmin = new AtomicBoolean();
ejt.query("SELECT roleId FROM userRoleMappings WHERE userId=?", new Object[] { userId }, row -> {
if (row.getInt(1) == PermissionHolder.SUPERADMIN_ROLE.getId()) {
isAdmin.set(true);
}
});
if (!isAdmin.get()) {
// Create read role
String readXid = UUID.randomUUID().toString();
String readRoleName = new TranslatableMessage("watchLists.watchListReadRolePrefix", name).translate(Common.getTranslations());
int readRoleId = ejt.doInsert("INSERT INTO roles (xid, name) VALUES (?, ?)", new Object[] { readXid, readRoleName }, new int[] { Types.VARCHAR, Types.VARCHAR });
Role readRole = new Role(readRoleId, readXid);
// Assign to user
ejt.doInsert("INSERT INTO userRoleMappings (roleId, userId) VALUES (?,?)", new Object[] { readRoleId, userId }, new int[] { Types.INTEGER, Types.INTEGER });
// Create read permission
MangoPermission readPermission = getExistingPermission(readPermissionId);
if (readPermission == null) {
readPermission = new MangoPermission();
}
Set<Set<Role>> readRoles = new HashSet<>(readPermission.getRoles());
readRoles.add(Collections.singleton(readRole));
MangoPermission newReadPermission = getOrCreatePermission(new MangoPermission(readRoles));
// Create edit role
String editXid = UUID.randomUUID().toString();
String editRoleName = new TranslatableMessage("watchLists.watchListEditRolePrefix", name).translate(Common.getTranslations());
int editRoleId = ejt.doInsert("INSERT INTO roles (xid, name) VALUES (?, ?)", new Object[] { editXid, editRoleName }, new int[] { Types.VARCHAR, Types.VARCHAR });
Role editRole = new Role(editRoleId, editXid);
// Assign to user
ejt.doInsert("INSERT INTO userRoleMappings (roleId, userId) VALUES (?,?)", new Object[] { editRoleId, userId }, new int[] { Types.INTEGER, Types.INTEGER });
// Create edit permission
MangoPermission editPermission = getExistingPermission(editPermissionId);
if (editPermission == null) {
editPermission = new MangoPermission();
}
Set<Set<Role>> editRoles = new HashSet<>(editPermission.getRoles());
editRoles.add(Collections.singleton(editRole));
MangoPermission newEditPermission = getOrCreatePermission(new MangoPermission(editRoles));
// Update the permissionIds
ejt.update("UPDATE watchLists SET readPermissionId=?, editPermissionId=? WHERE id=?", new Object[] { newReadPermission.getId(), newEditPermission.getId(), id });
}
});
// Drop the columns and indexes
Map<String, String[]> scripts = new HashMap<>();
scripts.put(DatabaseType.MYSQL.name(), mySQL);
scripts.put(DatabaseType.H2.name(), sql);
scripts.put(DatabaseType.MSSQL.name(), sql);
scripts.put(DatabaseType.POSTGRES.name(), mySQL);
runScript(scripts, out);
}
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class MangoPermissionModelDeserializer method nodeToModel.
@SuppressWarnings("unchecked")
public MangoPermissionModel nodeToModel(JsonNode tree, ObjectMapper mapper) throws JsonProcessingException {
Set<Set<Role>> roles = new HashSet<>();
if (tree instanceof ArrayNode) {
Set<Object> outerSet = mapper.treeToValue(tree, Set.class);
for (Object o : outerSet) {
Set<Role> innerRoles = new HashSet<>();
roles.add(innerRoles);
if (o instanceof Iterable) {
for (String xid : (Iterable<String>) o) {
Role role = permissionService.getRole(xid);
if (role != null) {
innerRoles.add(role);
} else {
// Let validation pick this up
innerRoles.add(new Role(Common.NEW_ID, xid));
}
}
} else {
String xid = (String) o;
Role role = permissionService.getRole(xid);
if (role != null) {
innerRoles.add(role);
} else {
// Let validation pick this up
innerRoles.add(new Role(Common.NEW_ID, xid));
}
}
}
} else if (tree instanceof TextNode) {
Set<String> xids = PermissionService.explodeLegacyPermissionGroups(tree.asText());
for (String xid : xids) {
Role role = permissionService.getRole(xid);
if (role != null) {
roles.add(Collections.singleton(role));
} else {
// Let validation pick this up
roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
}
}
}
return new MangoPermissionModel(new MangoPermission(roles));
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class RoleRestController method create.
@ApiOperation(value = "Create a Role", notes = "Admin only")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<RoleModel> create(@RequestBody RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
RoleVO vo = service.insert(mapping.unmap(model, user, mapper));
URI location = builder.path("/roles/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.CREATED);
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class MangoPermissionModelSerializer method serialize.
@Override
public void serialize(MangoPermissionModel value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
if (value == null) {
jgen.writeNull();
} else {
Set<Set<Role>> roleSets = value.getPermission().getRoles();
jgen.writeStartArray();
for (Set<Role> roleSet : roleSets) {
if (roleSet.size() > 1) {
jgen.writeStartArray();
}
for (Role role : roleSet) {
jgen.writeString(role.getXid());
}
if (roleSet.size() > 1) {
jgen.writeEndArray();
}
}
jgen.writeEndArray();
}
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class MaintenanceEventType method getEventPermission.
@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
DataPointService dataPointService = Common.getBean(DataPointService.class);
MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
Set<Role> allRequired = new HashSet<>();
try {
MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
try {
for (int dsId : vo.getDataSources()) {
MangoPermission read = dataSourceService.getReadPermission(dsId);
read.getRoles().forEach(allRequired::addAll);
}
} catch (NotFoundException e) {
// Ignore this item
}
try {
for (int dpId : vo.getDataPoints()) {
MangoPermission read = dataPointService.getReadPermission(dpId);
read.getRoles().forEach(allRequired::addAll);
}
} catch (NotFoundException e) {
// Ignore this item
}
} catch (NotFoundException e) {
// Ignore all of it
}
if (allRequired.size() == 0) {
return MangoPermission.superadminOnly();
} else {
return MangoPermission.requireAllRoles(allRequired);
}
}
Aggregations