Search in sources :

Example 11 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class UsersService method handleRoleEvent.

@EventListener
protected void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
    if (event.getType() == DaoEventType.DELETE || event.getType() == DaoEventType.UPDATE) {
        Role originalRole = event.getType() == DaoEventType.UPDATE ? event.getOriginalVo().getRole() : event.getVo().getRole();
        // in the user here
        if (userByUsername != null) {
            userByUsername.asMap().forEach((username, user) -> {
                if (user.getRoles().contains(originalRole)) {
                    Set<Role> updatedRoles = new HashSet<>(user.getRoles());
                    if (event.getType() == DaoEventType.DELETE) {
                        // Remove this role
                        updatedRoles.remove(originalRole);
                    } else if (event.getType() == DaoEventType.UPDATE) {
                        // Replace this role
                        updatedRoles.remove(originalRole);
                        updatedRoles.add(event.getVo().getRole());
                    }
                    user.setRoles(Collections.unmodifiableSet(updatedRoles));
                    // publish the event using the same user for originalVo, we aren't changing the XID
                    // so it shouldn't matter
                    DaoEvent<User> userUpdatedEvent = new DaoEvent<>(this.dao, UPDATE, user, user);
                    this.eventPublisher.publishEvent(userUpdatedEvent);
                }
            });
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DaoEvent(com.infiniteautomation.mango.spring.events.DaoEvent) User(com.serotonin.m2m2.vo.User) HashSet(java.util.HashSet) EventListener(org.springframework.context.event.EventListener)

Example 12 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class LazyFieldJsonTest method testLazyPermissionFromJsonObject.

@Test
public void testLazyPermissionFromJsonObject() {
    RoleService roleService = Common.getBean(RoleService.class);
    PermissionService permissionService = Common.getBean(PermissionService.class);
    Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
    Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
    LazyField<MangoPermission> permission = new LazyField<>(() -> MangoPermission.builder().minterm(role1, role2).build());
    try (StringWriter stringWriter = new StringWriter()) {
        JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
        JsonValue value = typeWriter.writeObject(permission);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        String json = stringWriter.toString();
        JsonTypeReader typeReader = new JsonTypeReader(json);
        JsonValue read = typeReader.read();
        JsonArray root = read.toJsonArray();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
        LazyField<MangoPermission> readPermission = new LazyField<>();
        TypeDefinition lazyType = new TypeDefinition(LazyField.class, MangoPermission.class);
        context.getReader().readInto(lazyType, readPermission, root);
        assertEquals(permission.get(), readPermission.get());
    } catch (IOException | JsonException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) LazyField(com.infiniteautomation.mango.util.LazyField) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) TypeDefinition(com.serotonin.json.util.TypeDefinition) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) JsonArray(com.serotonin.json.type.JsonArray) ImportContext(com.infiniteautomation.mango.emport.ImportContext) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) Test(org.junit.Test)

Example 13 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class UserEventMulticasterTest method setupRoles.

@Before
public void setupRoles() {
    roleService = Common.getBean(RoleService.class);
    systemSuperadmin = PermissionHolder.SYSTEM_SUPERADMIN;
    // Add some roles
    mockRole = new RoleVO(Common.NEW_ID, "MOCK", "Mock test role.");
    mockRole = roleService.insert(mockRole);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) Before(org.junit.Before)

Example 14 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class DataPointService method handleRoleEvent.

@EventListener
protected void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
    if (event.getType() == DaoEventType.DELETE) {
        Role deletedRole = event.getVo().getRole();
        for (DataPointRT rt : getRuntimeManager().getRunningDataPoints()) {
            DataPointVO point = rt.getVO();
            point.setReadPermission(point.getReadPermission().withoutRole(deletedRole));
            point.setEditPermission(point.getEditPermission().withoutRole(deletedRole));
            point.setSetPermission(point.getSetPermission().withoutRole(deletedRole));
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) EventListener(org.springframework.context.event.EventListener)

Example 15 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class SetPointEventHandlerDefinition method handleRoleEvent.

@Override
public void handleRoleEvent(SetPointEventHandlerVO vo, DaoEvent<? extends RoleVO> event) {
    // Remove and re-serialize our handler's script roles if it has a role that was deleted
    if (vo.getScriptRoles().getRoles().contains(event.getVo().getRole())) {
        switch(event.getType()) {
            case UPDATE:
                break;
            case DELETE:
                Set<Role> updated = new HashSet<>(vo.getScriptRoles().getRoles());
                updated.remove(event.getVo().getRole());
                Set<Role> allRoles = new HashSet<>();
                for (Role role : updated) {
                    allRoles.addAll(service.getAllInheritedRoles(role));
                }
                ScriptPermissions permission = new ScriptPermissions(allRoles, vo.getScriptRoles().getPermissionHolderName());
                vo.setScriptRoles(permission);
                eventHandlerService.update(vo.getId(), vo);
                break;
            default:
                break;
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) HashSet(java.util.HashSet)

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