Search in sources :

Example 1 with NamespaceRulesNotExistsException

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

the class MembersManagerBlImpl method setEmailForUser.

/**
 * Sets preferred email for the given user. If the given email is null, a default namespace email
 * from configuration is used. If the namespace is also null, a defaul 'no-reply@perun-aai.org' is used.
 *
 * @param sess session
 * @param user user to whom is the email set
 * @param inputEmail email to be set
 * @param namespace namespace used to get default email
 * @return the actual email that was set
 * @throws WrongAttributeValueException
 * @throws WrongReferenceAttributeValueException
 */
private String setEmailForUser(PerunSession sess, User user, String inputEmail, String namespace) throws WrongAttributeValueException, WrongReferenceAttributeValueException {
    String email = inputEmail;
    if (email == null) {
        if (isNotBlank(namespace)) {
            try {
                var rules = getNamespaceRules(namespace);
                email = rules.getDefaultEmail();
            } catch (NamespaceRulesNotExistsException e) {
                log.warn("No rules found for namespace: '{}'", namespace, e);
                email = DEFAULT_NO_REPLY_EMAIL;
            }
        } else {
            email = DEFAULT_NO_REPLY_EMAIL;
        }
    }
    if (!Utils.emailPattern.matcher(email).matches()) {
        throw new InternalErrorException("Email has an invalid format: " + email);
    }
    try {
        Attribute mailAttr = getPerunBl().getAttributesManagerBl().getAttribute(sess, user, A_U_PREF_MAIL);
        mailAttr.setValue(email);
        getPerunBl().getAttributesManagerBl().setAttribute(sess, user, mailAttr);
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    }
    return email;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) NamespaceRulesNotExistsException(cz.metacentrum.perun.core.api.exceptions.NamespaceRulesNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

Attribute (cz.metacentrum.perun.core.api.Attribute)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 NamespaceRulesNotExistsException (cz.metacentrum.perun.core.api.exceptions.NamespaceRulesNotExistsException)1 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)1