Search in sources :

Example 71 with Role

use of com.serotonin.m2m2.vo.role.Role 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 72 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.

the class MangoPermissionModelDeserializer method nodeToModel.

@SuppressWarnings("unchecked")
public MangoPermissionModel nodeToModel(JsonNode tree, ObjectMapper mapper) throws JsonProcessingException {
    Set<Set<Role>> roles = new HashSet<>();
    if (tree instanceof ArrayNode) {
        Set<Object> outerSet = mapper.treeToValue(tree, Set.class);
        for (Object o : outerSet) {
            Set<Role> innerRoles = new HashSet<>();
            roles.add(innerRoles);
            if (o instanceof Iterable) {
                for (String xid : (Iterable<String>) o) {
                    Role role = permissionService.getRole(xid);
                    if (role != null) {
                        innerRoles.add(role);
                    } else {
                        // Let validation pick this up
                        innerRoles.add(new Role(Common.NEW_ID, xid));
                    }
                }
            } else {
                String xid = (String) o;
                Role role = permissionService.getRole(xid);
                if (role != null) {
                    innerRoles.add(role);
                } else {
                    // Let validation pick this up
                    innerRoles.add(new Role(Common.NEW_ID, xid));
                }
            }
        }
    } else if (tree instanceof TextNode) {
        Set<String> xids = PermissionService.explodeLegacyPermissionGroups(tree.asText());
        for (String xid : xids) {
            Role role = permissionService.getRole(xid);
            if (role != null) {
                roles.add(Collections.singleton(role));
            } else {
                // Let validation pick this up
                roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
            }
        }
    }
    return new MangoPermissionModel(new MangoPermission(roles));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TextNode(com.fasterxml.jackson.databind.node.TextNode) Role(com.serotonin.m2m2.vo.role.Role) MangoPermissionModel(com.infiniteautomation.mango.rest.latest.model.permissions.MangoPermissionModel) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 73 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.

the class RoleRestController method create.

@ApiOperation(value = "Create a Role", notes = "Admin only")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<RoleModel> create(@RequestBody RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
    RoleVO vo = service.insert(mapping.unmap(model, user, mapper));
    URI location = builder.path("/roles/{xid}").buildAndExpand(vo.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 74 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.

the class MangoPermissionModelSerializer method serialize.

@Override
public void serialize(MangoPermissionModel value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    if (value == null) {
        jgen.writeNull();
    } else {
        Set<Set<Role>> roleSets = value.getPermission().getRoles();
        jgen.writeStartArray();
        for (Set<Role> roleSet : roleSets) {
            if (roleSet.size() > 1) {
                jgen.writeStartArray();
            }
            for (Role role : roleSet) {
                jgen.writeString(role.getXid());
            }
            if (roleSet.size() > 1) {
                jgen.writeEndArray();
            }
        }
        jgen.writeEndArray();
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) Set(java.util.Set)

Example 75 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.

the class MaintenanceEventType method getEventPermission.

@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
    Set<Role> allRequired = new HashSet<>();
    try {
        MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
        try {
            for (int dsId : vo.getDataSources()) {
                MangoPermission read = dataSourceService.getReadPermission(dsId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
        try {
            for (int dpId : vo.getDataPoints()) {
                MangoPermission read = dataPointService.getReadPermission(dpId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
    } catch (NotFoundException e) {
    // Ignore all of it
    }
    if (allRequired.size() == 0) {
        return MangoPermission.superadminOnly();
    } else {
        return MangoPermission.requireAllRoles(allRequired);
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) Role(com.serotonin.m2m2.vo.role.Role) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) HashSet(java.util.HashSet)

Aggregations

Role (com.serotonin.m2m2.vo.role.Role)102 Test (org.junit.Test)59 HashSet (java.util.HashSet)40 Set (java.util.Set)38 User (com.serotonin.m2m2.vo.User)33 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)23 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)22 Collectors (java.util.stream.Collectors)18 Common (com.serotonin.m2m2.Common)17 MangoTestBase (com.serotonin.m2m2.MangoTestBase)15 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 List (java.util.List)15 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)12 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)12 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)11 DSLContext (org.jooq.DSLContext)11