use of com.infiniteautomation.mango.permission.MangoPermission 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.infiniteautomation.mango.permission.MangoPermission 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.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.
the class WatchListEmportDefinition method doImport.
@Override
public void doImport(JsonValue jsonValue, ImportContext importContext, PermissionHolder importer) throws JsonException {
JsonObject watchListJson = jsonValue.toJsonObject();
String xid = watchListJson.getString("xid");
WatchListVO vo = null;
if (StringUtils.isBlank(xid)) {
xid = service.generateUniqueXid();
} else {
try {
vo = service.get(xid);
} catch (NotFoundException e) {
}
}
if (vo == null) {
vo = new WatchListVO();
vo.setXid(xid);
}
try {
importContext.getReader().readInto(vo, watchListJson);
// Ensure we have a default permission since null is valid in Mango 3.x
if (vo.getReadPermission() == null) {
vo.setReadPermission(new MangoPermission());
}
if (vo.getEditPermission() == null) {
vo.setEditPermission(new MangoPermission());
}
boolean isnew = vo.getId() == Common.NEW_ID;
if (isnew) {
service.insert(vo);
} else {
service.update(vo.getId(), vo);
}
importContext.addSuccessMessage(isnew, "emport.watchList.prefix", xid);
} catch (ValidationException e) {
importContext.copyValidationMessages(e.getValidationResult(), "emport.watchList.prefix", xid);
} catch (TranslatableJsonException e) {
importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, e.getMsg());
} catch (JsonException e) {
importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, importContext.getJsonExceptionMessage(e));
}
}
use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.
the class WatchListDao method savePreRelationalData.
@Override
public void savePreRelationalData(WatchListVO existing, WatchListVO vo) {
MangoPermission readPermission = permissionService.findOrCreate(vo.getReadPermission());
vo.setReadPermission(readPermission);
MangoPermission editPermission = permissionService.findOrCreate(vo.getEditPermission());
vo.setEditPermission(editPermission);
}
use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.
the class WatchListSummaryModelMapping method unmap.
@Override
public WatchListVO unmap(Object from, PermissionHolder user, RestModelMapper mapper) throws ValidationException {
WatchListSummaryModel model = (WatchListSummaryModel) from;
WatchListVO vo = model.toVO();
vo.setReadPermission(model.getReadPermission() != null ? model.getReadPermission().getPermission() : new MangoPermission());
vo.setEditPermission(model.getEditPermission() != null ? model.getEditPermission().getPermission() : new MangoPermission());
return vo;
}
Aggregations