use of com.serotonin.m2m2.db.dao.RoleDao 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.db.dao.RoleDao in project ma-core-public by infiniteautomation.
the class DataPointQueryPermissionTest method testDeleteAllRoles.
@Test
public void testDeleteAllRoles() {
// Insert some data points
Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
List<IDataPoint> points = this.createMockDataPoints(5, false, MangoPermission.requireAllRoles(readRoles), new MangoPermission());
List<IDataPoint> unreadable = this.createMockDataPoints(5, false, new MangoPermission(), new MangoPermission());
RoleDao roleDao = Common.getBean(RoleDao.class);
for (Role role : readRoles) {
roleDao.delete(role.getId());
}
DataPointService service = Common.getBean(DataPointService.class);
runAs.runAs(new PermissionHolder() {
@Override
public String getPermissionHolderName() {
return "Test";
}
@Override
public boolean isPermissionHolderDisabled() {
return false;
}
@Override
public Set<Role> getRoles() {
// This is odd as these roles should not be on a user either, but wanted to t
return readRoles;
}
}, () -> {
List<Integer> ids = points.stream().map(dp -> dp.getId()).collect(Collectors.toList());
ids.addAll(unreadable.stream().map(dp -> dp.getId()).collect(Collectors.toList()));
QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
List<DataPointVO> vos = query.query();
assertEquals(0, vos.size());
});
}
Aggregations