Search in sources :

Example 41 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

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 42 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class AbstractVOServiceWithPermissionsTest method addRoleToCreatePermission.

void addRoleToCreatePermission(Role vo) {
    String permissionType = getCreatePermissionType();
    if (permissionType != null) {
        PermissionDefinition def = ModuleRegistry.getPermissionDefinition(getCreatePermissionType());
        Set<Set<Role>> roleSet = def.getPermission().getRoles();
        Set<Set<Role>> newRoles = new HashSet<>();
        newRoles.add(Collections.singleton(vo));
        for (Set<Role> roles : roleSet) {
            newRoles.add(new HashSet<>(roles));
        }
        Common.getBean(SystemPermissionService.class).update(new MangoPermission(newRoles), def);
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 43 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class UsersServiceTest method testChangeUsernameWithPermission.

@Test
public void testChangeUsernameWithPermission() {
    // Add read role to change username permission
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(ChangeOwnUsernamePermissionDefinition.PERMISSION);
    Set<Set<Role>> roleSet = def.getPermission().getRoles();
    Set<Set<Role>> newRoles = new HashSet<>();
    newRoles.add(Collections.singleton(readRole));
    for (Set<Role> roles : roleSet) {
        newRoles.add(new HashSet<>(roles));
    }
    systemPermissionService.update(new MangoPermission(newRoles), def);
    // Ensure they can edit self
    setEditSelfPermission(MangoPermission.requireAnyRole(readRole));
    User vo = newVO(readUser);
    vo.setRoles(Collections.singleton(readRole));
    service.insert(vo);
    User saved = service.get(vo.getId());
    runAs.runAs(saved, () -> {
        saved.setUsername(randomXid());
        service.update(saved.getId(), saved);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ChangeOwnUsernamePermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.ChangeOwnUsernamePermissionDefinition) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) HashSet(java.util.HashSet) Set(java.util.Set) User(com.serotonin.m2m2.vo.User) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-core-public by infiniteautomation.

the class UsersServiceTest method testChangeUsernameWithoutPermission.

@Test
@ExpectValidationException("username")
public void testChangeUsernameWithoutPermission() {
    setEditSelfPermission(MangoPermission.requireAnyRole(readRole));
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(ChangeOwnUsernamePermissionDefinition.PERMISSION);
    Set<Set<Role>> roleSet = def.getPermission().getRoles();
    Set<Set<Role>> newRoles = new HashSet<>();
    newRoles.add(Collections.singleton(editRole));
    for (Set<Role> roles : roleSet) {
        if (roles.contains(PermissionHolder.USER_ROLE)) {
            // skip the user role
            continue;
        }
        newRoles.add(roles);
    }
    systemPermissionService.update(new MangoPermission(newRoles), def);
    User vo = newVO(readUser);
    vo.setRoles(Collections.singleton(readRole));
    service.insert(vo);
    User saved = service.get(vo.getId());
    runAs.runAs(saved, () -> {
        saved.setUsername(randomXid());
        service.update(saved.getId(), saved);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ChangeOwnUsernamePermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.ChangeOwnUsernamePermissionDefinition) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) HashSet(java.util.HashSet) Set(java.util.Set) User(com.serotonin.m2m2.vo.User) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 45 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class UserModel method toVO.

@Override
public User toVO() {
    User user = super.toVO();
    user.setUsername(username);
    user.setEmail(email);
    user.setPhone(phone);
    user.setDisabled(disabled);
    user.setHomeUrl(homeUrl);
    user.setReceiveAlarmEmails(receiveAlarmEmails);
    user.setTimezone(StringUtils.isBlank(timezone) ? null : timezone);
    user.setMuted(muted);
    user.setReceiveOwnAuditEvents(receiveOwnAuditEvents);
    if (roles != null) {
        // TODO move this into the model mapper and use map/unmap anywhere
        // a user model is needed
        user.setRoles(Common.getBean(PermissionService.class).explodeLegacyPermissionGroupsToRoles(roles));
    }
    user.setLocale(StringUtils.isBlank(locale) ? null : locale);
    if (!StringUtils.isEmpty(hashAlgorithm)) {
        String password = this.password != null ? this.password : "";
        user.setPasswordHash(this.hashAlgorithm, password);
    } else if (!StringUtils.isEmpty(password)) {
        user.setPlainTextPassword(password);
    }
    user.setSessionExpirationOverride(sessionExpirationOverride);
    if (sessionExpirationPeriod != null) {
        user.setSessionExpirationPeriods(sessionExpirationPeriod.getPeriods());
        if (sessionExpirationPeriod.getType() != null)
            user.setSessionExpirationPeriodType(sessionExpirationPeriod.getType().name());
    }
    user.setOrganization(organization);
    user.setOrganizationalRole(organizationalRole);
    user.setData(data);
    user.setEditPermission(editPermission != null ? editPermission.getPermission() : new MangoPermission());
    user.setReadPermission(readPermission != null ? readPermission.getPermission() : new MangoPermission());
    return user;
}
Also used : User(com.serotonin.m2m2.vo.User) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

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