Search in sources :

Example 1 with RoleDao

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();
}
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 2 with RoleDao

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());
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Aggregations

RoleDao (com.serotonin.m2m2.db.dao.RoleDao)2 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)2 Role (com.serotonin.m2m2.vo.role.Role)2 Set (java.util.Set)2 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)1 RunAs (com.infiniteautomation.mango.spring.components.RunAs)1 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)1 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)1 JsonArray (com.serotonin.json.type.JsonArray)1 JsonObject (com.serotonin.json.type.JsonObject)1 JsonValue (com.serotonin.json.type.JsonValue)1 Common (com.serotonin.m2m2.Common)1 MangoTestBase (com.serotonin.m2m2.MangoTestBase)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 QueryBuilder (com.serotonin.m2m2.db.dao.QueryBuilder)1 UserDao (com.serotonin.m2m2.db.dao.UserDao)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DataPointSummary (com.serotonin.m2m2.vo.DataPointSummary)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1