Search in sources :

Example 46 with MangoPermission

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();
}
Also used : DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary) User(com.serotonin.m2m2.vo.User) HashSet(java.util.HashSet) Set(java.util.Set) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonValue(com.serotonin.json.type.JsonValue) ArrayList(java.util.ArrayList) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) JsonArray(com.serotonin.json.type.JsonArray) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) UserDao(com.serotonin.m2m2.db.dao.UserDao) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 47 with MangoPermission

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);
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 48 with MangoPermission

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));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonObject(com.serotonin.json.type.JsonObject) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 49 with MangoPermission

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);
}
Also used : MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 50 with MangoPermission

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;
}
Also used : MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)93 Role (com.serotonin.m2m2.vo.role.Role)22 HashSet (java.util.HashSet)21 Set (java.util.Set)18 HashMap (java.util.HashMap)13 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)12 PermissionDefinition (com.serotonin.m2m2.module.PermissionDefinition)11 JsonException (com.serotonin.json.JsonException)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)9 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)8 JsonValue (com.serotonin.json.type.JsonValue)7 ArrayList (java.util.ArrayList)7 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 User (com.serotonin.m2m2.vo.User)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4