use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class RoleImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
String name = json.getString("name");
RoleVO vo = null;
if (StringUtils.isBlank(xid)) {
xid = service.generateUniqueXid();
} else {
try {
vo = service.get(xid);
} catch (NotFoundException e) {
}
}
if (vo == null) {
vo = new RoleVO(Common.NEW_ID, xid, name);
}
try {
// Read into the VO to get all properties
ctx.getReader().readInto(vo, json);
boolean isnew = vo.getId() == Common.NEW_ID;
if (isnew) {
service.insert(vo);
} else {
service.update(vo.getId(), vo);
}
addSuccessMessage(isnew, "emport.role.prefix", xid);
} catch (ValidationException e) {
setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
} catch (JsonException e) {
addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
}
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class SystemSettingsImporter method importImpl.
@Override
protected void importImpl() {
try {
Map<String, Object> settings = new HashMap<String, Object>();
// Finish reading it in.
for (String key : json.keySet()) {
JsonValue value = json.get(key);
// Don't import null values or database schemas
if ((value != null) && (!key.startsWith(SystemSettingsDao.DATABASE_SCHEMA_VERSION))) {
Object o = value.toNative();
if (o instanceof String) {
PermissionDefinition def = ModuleRegistry.getPermissionDefinition(key);
if (def != null) {
// Legacy permission import
try {
Set<String> xids = PermissionService.explodeLegacyPermissionGroups((String) o);
Set<Set<Role>> roles = new HashSet<>();
for (String xid : xids) {
RoleVO role = roleService.get(xid);
if (role != null) {
roles.add(Collections.singleton(role.getRole()));
} else {
roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
}
}
permissionService.update(new MangoPermission(roles), def);
addSuccessMessage(false, "emport.permission.prefix", key);
} catch (ValidationException e) {
setValidationMessages(e.getValidationResult(), "emport.permission.prefix", key);
return;
}
} else {
// Could be an export code so try and convert it
Integer id = SystemSettingsDao.getInstance().convertToValueFromCode(key, (String) o);
if (id != null)
settings.put(key, id);
else
settings.put(key, o);
}
} else {
settings.put(key, o);
}
}
}
// Now validate it. Use a new response object so we can distinguish errors in this vo
// from
// other errors.
ProcessResult voResponse = new ProcessResult();
SystemSettingsDao.getInstance().validate(settings, voResponse, user);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
else {
SystemSettingsDao.getInstance().updateSettings(settings);
addSuccessMessage(false, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
}
} catch (Exception e) {
addFailureMessage("emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()), e.getMessage());
}
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class RoleServiceTest method testQuery.
@Test
public void testQuery() {
List<RoleVO> vos = new ArrayList<>();
for (int i = 0; i < 5; i++) {
vos.add(insertNewVO(readUser));
}
AtomicInteger count = new AtomicInteger();
service.buildQuery().equal("xid", vos.get(0).getXid()).query(r -> {
assertVoEqual(vos.get(0), r);
assertEquals(count.incrementAndGet(), 1);
});
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class RoleServiceTest method adminCanSeeAllRoles.
@Test
public void adminCanSeeAllRoles() {
RoleVO vo = insertNewVO(readUser);
Set<String> roleXids = service.list().stream().map(AbstractVO::getXid).collect(Collectors.toSet());
Assert.assertTrue("Should see anonymous role", roleXids.contains(PermissionHolder.ANONYMOUS_ROLE_XID));
Assert.assertTrue("Should see user role", roleXids.contains(PermissionHolder.USER_ROLE_XID));
Assert.assertTrue("Should see superadmin role", roleXids.contains(PermissionHolder.SUPERADMIN_ROLE_XID));
Assert.assertTrue("Superadmin should see all roles", roleXids.contains(vo.getXid()));
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class RoleServiceTest method cannotInsertSuperadminRole.
@Test
@ExpectValidationException("xid")
public void cannotInsertSuperadminRole() {
RoleVO vo = new RoleVO(Common.NEW_ID, PermissionHolder.SUPERADMIN_ROLE_XID, "Superadmin default");
service.insert(vo);
}
Aggregations