Search in sources :

Example 41 with RoleVO

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;
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) HashSet(java.util.HashSet)

Example 42 with RoleVO

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();
}
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 43 with RoleVO

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);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO)

Example 44 with RoleVO

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);
    });
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 45 with RoleVO

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);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Aggregations

RoleVO (com.serotonin.m2m2.vo.role.RoleVO)58 Test (org.junit.Test)34 Role (com.serotonin.m2m2.vo.role.Role)33 HashSet (java.util.HashSet)17 RoleService (com.infiniteautomation.mango.spring.service.RoleService)14 User (com.serotonin.m2m2.vo.User)11 ArrayList (java.util.ArrayList)11 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)8 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)7 JsonValue (com.serotonin.json.type.JsonValue)7 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)7 Set (java.util.Set)7 Roles (com.infiniteautomation.mango.db.tables.Roles)6 JsonException (com.serotonin.json.JsonException)6 DSLContext (org.jooq.DSLContext)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)5 JsonObject (com.serotonin.json.type.JsonObject)5 ImportContext (com.infiniteautomation.mango.emport.ImportContext)4 JsonReader (com.serotonin.json.JsonReader)4