Search in sources :

Example 56 with InternalErrorException

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

the class VosManagerImpl method getAdmins.

public List<User> getAdmins(PerunSession sess, Vo vo, Role role) throws InternalErrorException {
    try {
        Set<User> setOfAdmins = new HashSet<User>();
        // direct admins
        setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from authz join users on authz.user_id=users.id " + "where authz.vo_id=? and authz.role_id=(select id from roles where name=?)", UsersManagerImpl.USER_MAPPER, vo.getId(), role.getRoleName()));
        // admins through a group
        List<Group> listOfGroupAdmins = getAdminGroups(sess, vo, role);
        for (Group group : listOfGroupAdmins) {
            setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from users join members on users.id=members.user_id " + "join groups_members on groups_members.member_id=members.id where groups_members.group_id=?", UsersManagerImpl.USER_MAPPER, group.getId()));
        }
        return new ArrayList(setOfAdmins);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<User>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) User(cz.metacentrum.perun.core.api.User) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) HashSet(java.util.HashSet)

Example 57 with InternalErrorException

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

the class GroupsManagerImpl method getName.

public String getName(int id) throws InternalErrorException {
    List name = null;
    try {
        name = jdbc.query("group.name as (with temp (name, id, parent_group_id) as ((select name, id, parent_group_id from GROUPS where parent_group_id is null) union all (select cast((temp.name + ':' + groups.name) as varchar(128)), " + "groups.id, groups.parent_group_id from groups inner join temp on temp.id = groups.parent_group_id )) select name from temp where group.id = ?", new RowMapper() {

            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getString(1);
            }
        }, id);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
    String result = (String) name.get(0);
    return result;
}
Also used : ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 58 with InternalErrorException

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

the class GroupsManagerImpl method createGroup.

public Group createGroup(PerunSession sess, Vo vo, Group group) throws GroupExistsException, InternalErrorException {
    Utils.notNull(group, "group");
    Utils.notNull(group.getName(), "group.getName()");
    // Check if the group already exists
    if (group.getParentGroupId() == null) {
        // check if the TOP level group exists
        if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id IS NULL", group.getName(), vo.getId())) {
            throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id is [NULL]");
        }
    } else {
        // check if subgroup exists under parent group
        if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id=?", group.getName(), vo.getId(), group.getParentGroupId())) {
            throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id [" + group.getParentGroupId() + "]");
        }
    }
    // Check the group name, it can contain only a-Z0-9_- and space
    if (!group.getShortName().matches("^[- a-zA-Z.0-9_]+$")) {
        throw new InternalErrorException(new IllegalArgumentException("Wrong group name, group name can contain only a-Z0-9.-_: and space characters. " + group));
    }
    try {
        // Store the group into the DB
        int newId = Utils.getNewId(jdbc, "groups_id_seq");
        jdbc.update("insert into groups (id, parent_group_id, name, dsc, vo_id, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, group.getParentGroupId(), group.getName(), group.getDescription(), vo.getId(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
        group.setId(newId);
        group.setVoId(vo.getId());
        return group;
    } catch (RuntimeException err) {
        throw new InternalErrorException(err);
    }
}
Also used : GroupExistsException(cz.metacentrum.perun.core.api.exceptions.GroupExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 59 with InternalErrorException

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

the class GroupsManagerImpl method getGroupsByIds.

public List<Group> getGroupsByIds(PerunSession sess, List<Integer> groupsIds) throws InternalErrorException {
    // If groupsIds are empty, we can immediately return empty result
    if (groupsIds.size() == 0) {
        return new ArrayList<Group>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", groupsIds);
    try {
        return this.namedParameterJdbcTemplate.query("select " + groupMappingSelectQuery + " from groups where groups.id in ( :ids )", parameters, GROUP_MAPPER);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Group>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 60 with InternalErrorException

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

the class ResourcesManagerImpl method getAdmins.

public List<User> getAdmins(PerunSession sess, Resource resource) throws InternalErrorException {
    try {
        Set<User> setOfAdmins = new HashSet<>();
        // Direct admins
        setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from authz join users on authz.user_id=users.id" + "  where authz.resource_id=? and authz.role_id=(select id from roles where name=?)", UsersManagerImpl.USER_MAPPER, resource.getId(), Role.RESOURCEADMIN.getRoleName()));
        // Admins through a group
        List<Group> listOfGroupAdmins = getAdminGroups(sess, resource);
        for (Group authorizedGroup : listOfGroupAdmins) {
            setOfAdmins.addAll(jdbc.query("select " + UsersManagerImpl.userMappingSelectQuery + " from users join members on users.id=members.user_id " + "join groups_members on groups_members.member_id=members.id where groups_members.group_id=?", UsersManagerImpl.USER_MAPPER, authorizedGroup.getId()));
        }
        return new ArrayList(setOfAdmins);
    } catch (EmptyResultDataAccessException e) {
        return new ArrayList<>();
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)376 Attribute (cz.metacentrum.perun.core.api.Attribute)119 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)104 ArrayList (java.util.ArrayList)94 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)89 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)78 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)68 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)67 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)44 User (cz.metacentrum.perun.core.api.User)37 Group (cz.metacentrum.perun.core.api.Group)36 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)33 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)33 HashMap (java.util.HashMap)30 IOException (java.io.IOException)28 Member (cz.metacentrum.perun.core.api.Member)24 Map (java.util.Map)24 Facility (cz.metacentrum.perun.core.api.Facility)23 List (java.util.List)23 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)22