use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class Upgrade4 method get.
public MangoPermission get(Integer id) {
// TODO Mango 4.0 improve performance
// Fist check to see if it exists as it may have no minterms
Integer foundId = create.select(PERMISSIONS.id).from(PERMISSIONS).where(PERMISSIONS.id.equal(id)).fetchOneInto(Integer.class);
if (foundId == null) {
return null;
}
List<Field<?>> fields = new ArrayList<>();
fields.add(roleTableIdAlias);
fields.add(roleTableXidAlias);
fields.add(PERMISSIONS_MINTERMS.mintermId);
SelectSeekStep2<Record, Integer, Integer> select = create.select(fields).from(PERMISSIONS_MINTERMS).join(MINTERMS_ROLES).on(PERMISSIONS_MINTERMS.mintermId.eq(MINTERMS_ROLES.mintermId)).join(roleTableAsAlias).on(roleTableIdAlias.eq(MINTERMS_ROLES.roleId)).where(PERMISSIONS_MINTERMS.permissionId.eq(id)).orderBy(PERMISSIONS_MINTERMS.permissionId.asc(), PERMISSIONS_MINTERMS.mintermId.asc());
String sql = select.getSQL();
List<Object> arguments = select.getBindValues();
Object[] argumentsArray = arguments.toArray(new Object[arguments.size()]);
return ejt.query(sql, argumentsArray, new ResultSetExtractor<MangoPermission>() {
private int roleIdIndex = 1;
private int roleXidIndex = 2;
private int minterIdIndex = 3;
@Override
public MangoPermission extractData(ResultSet rs) throws SQLException, DataAccessException {
if (rs.next()) {
Set<Set<Role>> roleSet = new HashSet<>();
Set<Role> minTerm = new HashSet<>();
roleSet.add(minTerm);
minTerm.add(new Role(rs.getInt(roleIdIndex), rs.getString(roleXidIndex)));
int mintermId = rs.getInt(minterIdIndex);
while (rs.next()) {
if (rs.getInt(minterIdIndex) == mintermId) {
// Add to current minterm
minTerm.add(new Role(rs.getInt(roleIdIndex), rs.getString(roleXidIndex)));
} else {
// Add to next minterm
minTerm = new HashSet<>();
roleSet.add(minTerm);
minTerm.add(new Role(rs.getInt(roleIdIndex), rs.getString(roleXidIndex)));
mintermId = rs.getInt(minterIdIndex);
}
}
MangoPermission permission = new MangoPermission(roleSet);
return permission.withId(id);
} else {
return new MangoPermission(id);
}
}
});
}
use of com.serotonin.m2m2.vo.role.Role in project ma-modules-public by infiniteautomation.
the class ScriptEventHandlerModel method readInto.
@Override
public void readInto(ScriptEventHandlerVO vo) {
super.readInto(vo);
vo.setScript(this.script);
vo.setEngineName(this.engineName);
RoleService roleService = Common.getBean(RoleService.class);
Set<Role> roleXids = scriptRoles.stream().map(xid -> {
try {
return roleService.get(xid).getRole();
} catch (NotFoundException e) {
return null;
}
}).filter(r -> r != null).collect(Collectors.toSet());
vo.setScriptRoles(roleXids);
}
use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.
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.Role in project ma-core-public by infiniteautomation.
the class MangoTestBase method createUsers.
/**
* Create users with password=password and supplied permissions
*/
protected List<User> createUsers(int count, Role... roles) {
List<User> users = new ArrayList<>();
for (int i = 0; i < count; i++) {
User user = createUser("User" + i, "user" + i, "password", "user" + i + "@yourMangoDomain.com", roles);
users.add(user);
}
return users;
}
use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.
the class MangoTestBase method createUser.
/**
* Create a user with pre-assigned ID
*/
protected User createUser(int id, String name, String username, String password, String email, Role... roles) {
User user = new User();
user.setId(id);
user.setName(name);
user.setUsername(username);
user.setPassword(Common.encrypt(password));
user.setEmail(email);
user.setPhone("");
user.setRoles(Collections.unmodifiableSet(new HashSet<>(Arrays.asList(roles))));
user.setDisabled(false);
UsersService service = Common.getBean(UsersService.class);
return service.insert(user);
}
Aggregations