use of com.serotonin.m2m2.vo.role.RoleVO in project ma-modules-public by infiniteautomation.
the class RoleModelMapping method unmap.
@Override
public RoleVO unmap(Object from, PermissionHolder user, RestModelMapper mapper) throws ValidationException {
RoleModel model = (RoleModel) from;
RoleVO vo = model.toVO();
Set<Role> roles = new HashSet<>();
if (model.getInherited() != null) {
for (String xid : model.getInherited()) {
Role role = service.getRole(xid);
if (role != null) {
roles.add(role);
} else {
roles.add(new Role(Common.NEW_ID, xid));
}
}
}
vo.setInherited(roles);
return vo;
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-modules-public by infiniteautomation.
the class WatchListVO method jsonRead.
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
super.jsonRead(reader, jsonObject);
String type = jsonObject.getString("type");
try {
this.type = WatchListType.valueOf(type.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
this.type = null;
}
JsonValue read = jsonObject.get("readPermission");
if (read != null) {
this.readPermission = reader.read(MangoPermission.class, read);
}
JsonValue edit = jsonObject.get("editPermission");
if (edit != null) {
this.editPermission = reader.read(MangoPermission.class, edit);
}
if (jsonObject.containsKey("user")) {
String username = jsonObject.getString("user");
if (StringUtils.isBlank(username))
throw new TranslatableJsonException("emport.error.missingValue", "user");
User user = UserDao.getInstance().getByXid(username);
if (user == null) {
throw new TranslatableJsonException("emport.error.missingUser", username);
} else if (!Common.getBean(PermissionService.class).hasAdminRole(user)) {
RoleDao dao = Common.getBean(RoleDao.class);
String name = jsonObject.getString("name", new TranslatableMessage("header.watchlist").translate(user.getTranslations()));
// Create a role for this user to be able to edit this item
String editName = new TranslatableMessage("watchList.watchListEditRolePrefix", name).translate(user.getTranslations());
RoleVO editRole = new RoleVO(Common.NEW_ID, UUID.randomUUID().toString(), editName);
dao.insert(editRole);
Set<Set<Role>> editRoles = new HashSet<>(this.editPermission.getRoles());
editRoles.add(Collections.singleton(editRole.getRole()));
this.editPermission = new MangoPermission(editRoles);
// Create a role for this user to be able to read this item
String readName = new TranslatableMessage("watchList.watchListReadRolePrefix", name).translate(user.getTranslations());
RoleVO readRole = new RoleVO(Common.NEW_ID, UUID.randomUUID().toString(), readName);
dao.insert(readRole);
Set<Set<Role>> readRoles = new HashSet<>(this.readPermission.getRoles());
readRoles.add(Collections.singleton(readRole.getRole()));
this.readPermission = new MangoPermission(readRoles);
// Update the user to have this role
UserDao userDao = Common.getBean(UserDao.class);
Set<Role> newUserRoles = new HashSet<>(user.getRoles());
newUserRoles.add(editRole.getRole());
newUserRoles.add(readRole.getRole());
user.setRoles(newUserRoles);
userDao.update(user.getId(), user);
}
}
JsonArray jsonDataPoints = jsonObject.getJsonArray("dataPoints");
if (jsonDataPoints != null) {
List<IDataPoint> points = new ArrayList<>();
DataPointDao dataPointDao = DataPointDao.getInstance();
for (JsonValue jv : jsonDataPoints) {
String xid = jv.toString();
DataPointSummary dpVO = dataPointDao.getSummary(xid);
if (dpVO == null)
throw new TranslatableJsonException("emport.error.missingPoint", xid);
points.add(dpVO);
}
pointList.set(points);
}
JsonObject o = jsonObject.getJsonObject("data");
if (o != null)
this.data = o.toMap();
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by infiniteautomation.
the class PermissionServiceTest method randomRole.
Role randomRole() {
RoleVO vo = new RoleVO(Common.NEW_ID, UUID.randomUUID().toString(), "Random permission");
roleService.insert(vo);
return new Role(vo);
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by infiniteautomation.
the class RoleServiceTest method testQuery.
@Test
public void testQuery() {
List<RoleVO> vos = new ArrayList<>();
for (int i = 0; i < 5; i++) {
vos.add(insertNewVO(readUser));
}
AtomicInteger count = new AtomicInteger();
service.buildQuery().equal("xid", vos.get(0).getXid()).query(r -> {
assertVoEqual(vos.get(0), r);
assertEquals(count.incrementAndGet(), 1);
});
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by infiniteautomation.
the class RoleServiceTest method cannotModifySuperadminRole.
@Test
@ExpectValidationException("xid")
public void cannotModifySuperadminRole() {
RoleVO vo = service.get(PermissionHolder.SUPERADMIN_ROLE_XID);
RoleVO updated = new RoleVO(Common.NEW_ID, vo.getXid(), "Superadmin default changed");
service.update(vo.getXid(), updated);
}
Aggregations