Search in sources :

Example 66 with WrongAttributeAssignmentException

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

the class SearcherEntry method getMembersByUserAttributes.

public List<Member> getMembersByUserAttributes(PerunSession sess, Vo vo, Map<String, String> userAttributesWithSearchingValues) throws InternalErrorException, AttributeNotExistsException, PrivilegeException, WrongAttributeAssignmentException, VoNotExistsException {
    // Authorization
    perunBl.getVosManagerBl().checkVoExists(sess, vo);
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, vo) && !AuthzResolver.isAuthorized(sess, Role.VOOBSERVER, vo) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, vo)) {
        throw new PrivilegeException(sess, "getMembersByUserAttributes");
    }
    //If map is null or empty, return all members from vo
    if (userAttributesWithSearchingValues == null || userAttributesWithSearchingValues.isEmpty()) {
        return perunBl.getMembersManagerBl().getMembers(sess, vo);
    }
    Set<String> attrNames = userAttributesWithSearchingValues.keySet();
    List<AttributeDefinition> attrDefs = new ArrayList<>();
    for (String attrName : attrNames) {
        if (attrName == null || attrName.isEmpty())
            throw new InternalErrorException("One of attributes has empty name.");
        //throw AttributeNotExistsException if this attr_name not exists in DB
        AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, attrName);
        attrDefs.add(attrDef);
        //test namespace of attribute
        if (!getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attrDef, AttributesManager.NS_USER_ATTR)) {
            throw new WrongAttributeAssignmentException("Attribute can be only in user namespace " + attrDef);
        }
    }
    //get all found users
    List<User> users = searcherBl.getUsers(sess, userAttributesWithSearchingValues);
    List<Member> members = new ArrayList<>();
    for (User user : users) {
        //get member for user
        Member member;
        try {
            member = perunBl.getMembersManagerBl().getMemberByUser(sess, vo, user);
        } catch (MemberNotExistsException ex) {
            continue;
        }
        boolean isAuthorized = true;
        for (AttributeDefinition attrDef : attrDefs) {
            //Test if user has righ to read such attribute for specific user, if not, remove it from returning list
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, user, null)) {
                isAuthorized = false;
                break;
            }
        }
        if (isAuthorized)
            members.add(member);
    }
    return members;
}
Also used : User(cz.metacentrum.perun.core.api.User) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Member(cz.metacentrum.perun.core.api.Member)

Example 67 with WrongAttributeAssignmentException

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

the class urn_perun_user_attribute_def_def_vsupMailAliases method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    // map of reserved vsup mails
    Attribute reservedMailsAttribute;
    Map<String, String> reservedMailsAttributeValue;
    // other vsup mail attributes to get values from
    Attribute vsupMailAttribute;
    Attribute mailAliasAttribute;
    Attribute vsupPreferredMailAttribute;
    // output sets used for comparison
    Set<String> reservedMailsOfUser = new HashSet<>();
    Set<String> actualMailsOfUser = new HashSet<>();
    try {
        reservedMailsAttribute = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, usedMailsKeyVsup, usedMailsUrn);
        vsupMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailUrn);
        mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
        vsupPreferredMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupPreferredMailUrn);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    if (attribute.getValue() == null && reservedMailsAttribute.getValue() == null) {
        throw new ConsistencyErrorException("Entityless attribute 'urn:perun:entityless:attribute-def:def:usedMails' is empty, but we are removing 'vsupMailAliases' value, so there should have been entry in entityless attribute.");
    }
    if (reservedMailsAttribute.getValue() == null) {
        reservedMailsAttributeValue = new LinkedHashMap<>();
    } else {
        reservedMailsAttributeValue = (Map<String, String>) reservedMailsAttribute.getValue();
    }
    // if SET action and mail is already reserved by other user
    if (attribute.getValue() != null) {
        List<String> mails = (List<String>) attribute.getValue();
        for (String mail : mails) {
            String ownersUserId = reservedMailsAttributeValue.get(mail);
            if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
                // TODO - maybe get actual owners attribute and throw WrongReferenceAttributeException to be nice in a GUI ?
                throw new InternalErrorException("On of VŠUP mail aliases: '" + mail + "' is already in use by User ID: " + ownersUserId + ".");
            }
        }
    }
    for (Map.Entry<String, String> entry : reservedMailsAttributeValue.entrySet()) {
        if (Objects.equals(entry.getValue(), String.valueOf(user.getId()))) {
            // reserved mails of a user
            reservedMailsOfUser.add(entry.getKey());
        }
    }
    if (vsupMailAttribute.getValue() != null) {
        actualMailsOfUser.add((String) vsupMailAttribute.getValue());
    }
    if (vsupPreferredMailAttribute.getValue() != null) {
        actualMailsOfUser.add((String) vsupPreferredMailAttribute.getValue());
    }
    if (mailAliasAttribute.getValue() != null) {
        actualMailsOfUser.add((String) mailAliasAttribute.getValue());
    }
    for (String mail : reservedMailsOfUser) {
        if (!actualMailsOfUser.contains(mail)) {
            // Remove mail, which is not in attributes anymore
            reservedMailsAttributeValue.remove(mail);
        }
    }
    // Put in which is in attribute but not in a map
    if (attribute.getValue() != null) {
        List<String> mails = (List<String>) attribute.getValue();
        for (String mail : mails) {
            reservedMailsAttributeValue.putIfAbsent(mail, String.valueOf(user.getId()));
        }
    }
    // save changes in entityless attribute
    try {
        // always set value to attribute, since we might start with null in attribute and empty map in variable !!
        reservedMailsAttribute.setValue(reservedMailsAttributeValue);
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, usedMailsKeyVsup, reservedMailsAttribute);
    } catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ArrayList(java.util.ArrayList) List(java.util.List) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 68 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method validateMember.

public Member validateMember(PerunSession sess, Member member) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException {
    if (this.haveStatus(sess, member, Status.VALID)) {
        log.debug("Trying to validate member who is already valid. " + member);
        return member;
    }
    Status oldStatus = member.getStatus();
    getMembersManagerImpl().setStatus(sess, member, Status.VALID);
    member.setStatus(Status.VALID);
    getPerunBl().getAuditer().log(sess, "{} validated.", member);
    if (oldStatus.equals(Status.INVALID) || oldStatus.equals(Status.DISABLED)) {
        try {
            getPerunBl().getAttributesManagerBl().doTheMagic(sess, member);
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        }
    }
    return member;
}
Also used : Status(cz.metacentrum.perun.core.api.Status) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 69 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method sendPasswordResetLinkEmail.

public void sendPasswordResetLinkEmail(PerunSession sess, Member member, String namespace, String url) throws InternalErrorException {
    User user = perunBl.getUsersManagerBl().getUserByMember(sess, member);
    List<Attribute> logins = perunBl.getAttributesManagerBl().getLogins(sess, user);
    boolean found = false;
    for (Attribute a : logins) {
        if (a.getFriendlyNameParameter().equals(namespace))
            found = true;
    }
    if (!found)
        throw new InternalErrorException(user.toString() + " doesn't have login in namespace: " + namespace);
    String email = "";
    try {
        Attribute a = perunBl.getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredMail");
        if (a != null && a.getValue() != null) {
            email = (String) a.getValue();
        }
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    int id = getMembersManagerImpl().storePasswordResetRequest(sess, user, namespace);
    Utils.sendPasswordResetEmail(user, email, namespace, url, id);
}
Also used : User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 70 with WrongAttributeAssignmentException

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

the class MembersManagerBlImpl method getNewExtendMembership.

public Date getNewExtendMembership(PerunSession sess, Vo vo, String loa) throws InternalErrorException, ExtendMembershipException {
    // Check if the VO has set membershipExpirationRules attribute
    LinkedHashMap<String, String> membershipExpirationRules;
    Attribute membershipExpirationRulesAttribute = null;
    try {
        membershipExpirationRulesAttribute = getPerunBl().getAttributesManagerBl().getAttribute(sess, vo, MembersManager.membershipExpirationRulesAttributeName);
        membershipExpirationRules = (LinkedHashMap<String, String>) membershipExpirationRulesAttribute.getValue();
        // If attribute was not filled, then silently exit with null
        if (membershipExpirationRules == null)
            return null;
    } catch (AttributeNotExistsException e) {
        // No rules set, so leave it as it is
        return null;
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException("Shouldn't happen.");
    }
    // Which LOA we won't extend? This is applicable only for members who have already set expiration from the previous period
    if (membershipExpirationRules.get(MembersManager.membershipDoNotExtendLoaKeyName) != null) {
        String[] doNotExtendLoas = membershipExpirationRules.get(MembersManager.membershipDoNotExtendLoaKeyName).split(",");
        for (String doNotExtendLoa : doNotExtendLoas) {
            if (doNotExtendLoa.equals(loa)) {
                // LOA provided is not allowed for extension
                throw new ExtendMembershipException(ExtendMembershipException.Reason.INSUFFICIENTLOA, "Provided LoA " + loa + " doesn't have required level for VO id " + vo.getId() + ".");
            }
        }
    }
    Calendar calendar = Calendar.getInstance();
    String period = null;
    // Default extension
    if (membershipExpirationRules.get(MembersManager.membershipPeriodKeyName) != null) {
        period = membershipExpirationRules.get(MembersManager.membershipPeriodKeyName);
    }
    // Do we extend particular LoA? Attribute syntax LoA|[period][.]
    if (membershipExpirationRules.get(MembersManager.membershipPeriodLoaKeyName) != null) {
        // Which period
        String[] membershipPeriodLoa = membershipExpirationRules.get(MembersManager.membershipPeriodLoaKeyName).split("\\|");
        String membershipLoa = membershipPeriodLoa[0];
        String periodLoa = membershipPeriodLoa[1];
        // Does the user have this LoA?
        if (membershipLoa.equals(loa)) {
            period = periodLoa;
        }
    }
    // Do we extend for x months or for static date?
    if (period != null) {
        if (period.startsWith("+")) {
            // By default do not add nothing
            int amount = 0;
            int field;
            // We will add days/months/years
            Pattern p = Pattern.compile("\\+([0-9]+)([dmy]?)");
            Matcher m = p.matcher(period);
            if (m.matches()) {
                String countString = m.group(1);
                amount = Integer.valueOf(countString);
                String dmyString = m.group(2);
                if (dmyString.equals("d")) {
                    field = Calendar.DAY_OF_YEAR;
                } else if (dmyString.equals("m")) {
                    field = Calendar.MONTH;
                } else if (dmyString.equals("y")) {
                    field = Calendar.YEAR;
                } else {
                    throw new InternalErrorException("Wrong format of period in VO membershipExpirationRules attribute. Period: " + period);
                }
            } else {
                throw new InternalErrorException("Wrong format of period in VO membershipExpirationRules attribute. Period: " + period);
            }
            // Add days/months/years
            calendar.add(field, amount);
        } else {
            // We will extend to particular date
            // Parse date
            Pattern p = Pattern.compile("([0-9]+).([0-9]+).");
            Matcher m = p.matcher(period);
            if (m.matches()) {
                int day = Integer.valueOf(m.group(1));
                int month = Integer.valueOf(m.group(2));
                // Get current year
                int year = calendar.get(Calendar.YEAR);
                // We must detect if the extension date is in current year or in a next year
                boolean extensionInNextYear;
                Calendar extensionCalendar = Calendar.getInstance();
                extensionCalendar.set(year, month - 1, day);
                Calendar today = Calendar.getInstance();
                if (extensionCalendar.before(today)) {
                    // Extension date is in a next year
                    extensionInNextYear = true;
                } else {
                    // Extension is in the current year
                    extensionInNextYear = false;
                }
                // Set the date to which the membershi should be extended, can be changed if there was grace period, see next part of the code
                // month is 0-based
                calendar.set(year, month - 1, day);
                if (extensionInNextYear) {
                    calendar.add(Calendar.YEAR, 1);
                }
                // Is there a grace period?
                if (membershipExpirationRules.get(MembersManager.membershipGracePeriodKeyName) != null) {
                    String gracePeriod = membershipExpirationRules.get(MembersManager.membershipGracePeriodKeyName);
                    // If the extension is requested in period-gracePeriod then extend to next period
                    // Get the value of the grace period
                    p = Pattern.compile("([0-9]+)([dmy]?)");
                    m = p.matcher(gracePeriod);
                    if (m.matches()) {
                        String countString = m.group(1);
                        int amount = Integer.valueOf(countString);
                        // Set the gracePeriodCalendar to the extension date
                        Calendar gracePeriodCalendar = Calendar.getInstance();
                        gracePeriodCalendar.set(year, month - 1, day);
                        if (extensionInNextYear) {
                            gracePeriodCalendar.add(Calendar.YEAR, 1);
                        }
                        int field;
                        String dmyString = m.group(2);
                        if (dmyString.equals("d")) {
                            field = Calendar.DAY_OF_YEAR;
                        } else if (dmyString.equals("m")) {
                            field = Calendar.MONTH;
                        } else if (dmyString.equals("y")) {
                            field = Calendar.YEAR;
                        } else {
                            throw new InternalErrorException("Wrong format of gracePeriod in VO membershipExpirationRules attribute. gracePeriod: " + gracePeriod);
                        }
                        // subtracts period definition, e.g. 3m
                        gracePeriodCalendar.add(field, -amount);
                        // Check if we are in grace period
                        if (gracePeriodCalendar.before(Calendar.getInstance())) {
                            // We are in grace period, so extend to the next period
                            calendar.add(Calendar.YEAR, 1);
                        }
                    }
                }
            } else {
                throw new InternalErrorException("Wrong format of period in VO membershipExpirationRules attribute. Period: " + period);
            }
        }
        // Reset hours, minutes and seconds to 0
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
    return calendar.getTime();
}
Also used : Pattern(java.util.regex.Pattern) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Calendar(java.util.Calendar) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Aggregations

WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)127 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)97 Attribute (cz.metacentrum.perun.core.api.Attribute)95 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)61 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)59 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)55 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)52 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)42 User (cz.metacentrum.perun.core.api.User)31 ArrayList (java.util.ArrayList)31 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)14 Facility (cz.metacentrum.perun.core.api.Facility)14 LinkedHashMap (java.util.LinkedHashMap)11 Member (cz.metacentrum.perun.core.api.Member)10 Map (java.util.Map)9 Group (cz.metacentrum.perun.core.api.Group)8 List (java.util.List)8 Resource (cz.metacentrum.perun.core.api.Resource)7 Vo (cz.metacentrum.perun.core.api.Vo)6 HashMap (java.util.HashMap)6