Search in sources :

Example 76 with Role

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);
            }
        }
    });
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Role(com.serotonin.m2m2.vo.role.Role) Field(org.jooq.Field) ResultSet(java.sql.ResultSet) Record(org.jooq.Record) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataAccessException(org.springframework.dao.DataAccessException) HashSet(java.util.HashSet)

Example 77 with Role

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);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) ScriptEventHandlerVO(com.serotonin.m2m2.vo.event.ScriptEventHandlerVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) Set(java.util.Set) ApiModel(io.swagger.annotations.ApiModel) ScriptEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ScriptEventHandlerDefinition) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) RoleService(com.infiniteautomation.mango.spring.service.RoleService) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) RoleService(com.infiniteautomation.mango.spring.service.RoleService) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 78 with Role

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());
    }
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) HashMap(java.util.HashMap) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 79 with Role

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;
}
Also used : User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) IDataPoint(com.serotonin.m2m2.vo.IDataPoint)

Example 80 with Role

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);
}
Also used : User(com.serotonin.m2m2.vo.User) UsersService(com.infiniteautomation.mango.spring.service.UsersService) HashSet(java.util.HashSet)

Aggregations

Role (com.serotonin.m2m2.vo.role.Role)102 Test (org.junit.Test)59 HashSet (java.util.HashSet)40 Set (java.util.Set)38 User (com.serotonin.m2m2.vo.User)33 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)23 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)22 Collectors (java.util.stream.Collectors)18 Common (com.serotonin.m2m2.Common)17 MangoTestBase (com.serotonin.m2m2.MangoTestBase)15 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 List (java.util.List)15 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)12 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)12 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)11 DSLContext (org.jooq.DSLContext)11