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());
}
}
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);
}
}
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);
});
}
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);
});
}
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;
}
Aggregations