Search in sources :

Example 1 with Role

use of cz.metacentrum.perun.core.api.Role in project perun by CESNET.

the class AttributesManagerImpl method getAttributeRights.

@Override
public List<AttributeRights> getAttributeRights(PerunSession sess, final int attributeId) throws InternalErrorException {
    List<AttributeRights> rights = null;
    try {
        rights = jdbc.query("select " + attributeRightSelectQuery + " from attributes_authz join roles on " + "attributes_authz.role_id=roles.id join action_types on attributes_authz.action_type_id=action_types.id where " + "attributes_authz.attr_id=?", new AttributeRightsExtractor(attributeId), attributeId);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
    // set also empty rights for other roles (not present in DB)
    boolean roleExists;
    List<Role> listOfRoles = new ArrayList<Role>();
    listOfRoles.add(Role.FACILITYADMIN);
    listOfRoles.add(Role.GROUPADMIN);
    listOfRoles.add(Role.SELF);
    listOfRoles.add(Role.VOADMIN);
    for (Role roleToTry : listOfRoles) {
        roleExists = false;
        Iterator itr = rights.iterator();
        while ((itr.hasNext()) && (!roleExists)) {
            AttributeRights right = (AttributeRights) itr.next();
            if (right.getRole().equals(roleToTry)) {
                roleExists = true;
            }
        }
        if (!roleExists) {
            rights.add(new AttributeRights(attributeId, roleToTry, new ArrayList<ActionType>()));
        }
    }
    return rights;
}
Also used : Role(cz.metacentrum.perun.core.api.Role) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) AttributeRights(cz.metacentrum.perun.core.api.AttributeRights) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with Role

use of cz.metacentrum.perun.core.api.Role in project perun by CESNET.

the class AuthzResolverImpl method getRoles.

public AuthzRoles getRoles(User user) throws InternalErrorException {
    AuthzRoles authzRoles = new AuthzRoles();
    if (user != null) {
        try {
            // Get roles from Authz table
            List<Pair<Role, Map<String, Set<Integer>>>> authzRolesPairs = jdbc.query("select " + authzRoleMappingSelectQuery + ", roles.name as role_name from authz left join roles on authz.role_id=roles.id where authz.user_id=? or authorized_group_id in " + "(select groups.id from groups join groups_members on groups.id=groups_members.group_id join members on " + "members.id=groups_members.member_id join users on users.id=members.user_id where users.id=?)", AUTHZROLE_MAPPER, user.getId(), user.getId());
            for (Pair<Role, Map<String, Set<Integer>>> pair : authzRolesPairs) {
                authzRoles.putAuthzRoles(pair.getLeft(), pair.getRight());
            }
            // Get service users for user
            List<Integer> authzServiceUsers = jdbc.query("select specific_user_users.specific_user_id as id from users, " + "specific_user_users where users.id=specific_user_users.user_id and specific_user_users.status='0' and users.id=? " + "and specific_user_users.type=?", Utils.ID_MAPPER, user.getId(), SpecificUserType.SERVICE.getSpecificUserType());
            for (Integer serviceUserId : authzServiceUsers) {
                authzRoles.putAuthzRole(Role.SELF, User.class, serviceUserId);
            }
            // Get members for user
            List<Integer> authzMember = jdbc.query("select members.id as id from members where members.user_id=?", Utils.ID_MAPPER, user.getId());
            for (Integer memberId : authzMember) {
                authzRoles.putAuthzRole(Role.SELF, Member.class, memberId);
            }
        } catch (RuntimeException e) {
            throw new InternalErrorException(e);
        }
    }
    return authzRoles;
}
Also used : Role(cz.metacentrum.perun.core.api.Role) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) HashMap(java.util.HashMap) Map(java.util.Map) Pair(cz.metacentrum.perun.core.api.Pair)

Example 3 with Role

use of cz.metacentrum.perun.core.api.Role in project perun by CESNET.

the class AuthzResolverImpl method initialize.

public void initialize() throws InternalErrorException {
    if (perun.isPerunReadOnly())
        log.debug("Loading authzresolver manager init in readOnly version.");
    // Check if all roles defined in class Role exists in the DB
    for (Role role : Role.values()) {
        try {
            if (0 == jdbc.queryForInt("select count(*) from roles where name=?", role.getRoleName())) {
                //Skip creating not existing roles for read only Perun
                if (perun.isPerunReadOnly()) {
                    throw new InternalErrorException("One of deafult roles not exists in DB - " + role);
                } else {
                    int newId = Utils.getNewId(jdbc, "roles_id_seq");
                    jdbc.update("insert into roles (id, name) values (?,?)", newId, role.getRoleName());
                }
            }
        } catch (RuntimeException e) {
            throw new InternalErrorException(e);
        }
    }
}
Also used : Role(cz.metacentrum.perun.core.api.Role) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

Role (cz.metacentrum.perun.core.api.Role)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)3 AttributeRights (cz.metacentrum.perun.core.api.AttributeRights)1 Pair (cz.metacentrum.perun.core.api.Pair)1 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1