Search in sources :

Example 1 with PerunException

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

the class PublicationManagerBlImpl method deletePublication.

@Override
public void deletePublication(PerunSession sess, Publication publication) throws CabinetException {
    try {
        // delete authors
        for (Authorship a : getAuthorshipManagerBl().getAuthorshipsByPublicationId(publication.getId())) {
            getAuthorshipManagerBl().deleteAuthorship(sess, a);
        }
        // delete thanks
        for (Thanks t : getThanksManagerBl().getThanksByPublicationId(publication.getId())) {
            getThanksManagerBl().deleteThanks(sess, t);
        }
        // delete publication
        getPublicationManagerDao().deletePublication(publication);
        log.debug("{} deleted.", publication);
    // publications without authors are: "to be deleted by perun admin"
    } catch (DataIntegrityViolationException ex) {
        throw new CabinetException("Can't delete publication with Authors or Thanks. Please remove them first in order to delete publication.", ErrorCodes.PUBLICATION_HAS_AUTHORS_OR_THANKS);
    } catch (PerunException ex) {
        throw new CabinetException(ErrorCodes.PERUN_EXCEPTION, ex);
    }
}
Also used : Authorship(cz.metacentrum.perun.cabinet.model.Authorship) Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 2 with PerunException

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

the class AppAutoRejectionScheduler method rejectApplication.

/**
 * Rejects the given application with custom message. The 'attrValueKey' represents the
 * key of the message in the applicationAutoRejectMessages attribute. If the vo/group has no
 * such attribute set, the default message will be used.
 *
 * @param application application to be rejected
 * @param attrValueKey key of a message in the applicationAutoRejectMessages
 * @param defaultMessage default message if the appropriate vo/group has no
 *                       applicationAutoRejectMessages attribute set
 */
private void rejectApplication(Application application, String attrValueKey, String defaultMessage) {
    Group group = application.getGroup();
    String lang = getUserPreferredLang(application);
    Vo vo = application.getVo();
    Attribute messagesAttr = null;
    try {
        if (group != null) {
            messagesAttr = perun.getAttributesManagerBl().getAttribute(sess, group, A_GROUP_APP_REJECT_MESSAGES);
        } else {
            messagesAttr = perun.getAttributesManagerBl().getAttribute(sess, vo, A_VO_APP_REJECT_MESSAGES);
        }
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException e) {
        log.error("Failed to get attribute with reject messages.", e);
    }
    String message;
    if (group != null) {
        message = getRejectMessage(messagesAttr, lang, attrValueKey, defaultMessage).replaceAll(GROUP_PLACEHOLDER, group.getName());
    } else {
        message = getRejectMessage(messagesAttr, lang, attrValueKey, defaultMessage).replaceAll(VO_PLACEHOLDER, vo.getName());
    }
    try {
        registrarManager.rejectApplication(sess, application.getId(), message);
    } catch (PerunException e) {
        log.error("Failed to reject application {}.", application, e);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Vo(cz.metacentrum.perun.core.api.Vo) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException)

Example 3 with PerunException

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

the class AppAutoRejectionScheduler method getUserPreferredLang.

/**
 * Returns preferred language of user from the given application.
 * Preferred language is taken from the submitted application, or from the user's
 * preferred language attribute. If none is present, 'en' is returned.
 *
 * @param application user
 * @return preferred language, 'en' as default
 */
private String getUserPreferredLang(Application application) {
    User user = application.getUser();
    if (user == null) {
        String appLang = null;
        try {
            appLang = getPreferredLangFromApplication(application);
        } catch (PerunException e) {
            log.error("Failed to read user preferred lang from application.", e);
        }
        if (appLang == null) {
            return DEFAULT_LANG;
        }
        return appLang;
    }
    try {
        Attribute langAttr = perun.getAttributesManagerBl().getAttribute(sess, user, A_USER_PREFERRED_LANGUAGE);
        String attrValue = langAttr.valueAsString();
        if (attrValue != null && !attrValue.isBlank()) {
            return attrValue;
        }
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException e) {
        log.error("Failed to read user preferred lang attribute.", e);
    }
    return DEFAULT_LANG;
}
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) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException)

Example 4 with PerunException

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

the class ConsolidatorManagerImpl method checkForSimilarUsers.

@Override
public List<Identity> checkForSimilarUsers(PerunSession sess, int appId) throws PerunException {
    String email = "";
    String name = "";
    List<RichUser> result = new ArrayList<>();
    List<String> attrNames = new ArrayList<>();
    attrNames.add("urn:perun:user:attribute-def:def:preferredMail");
    attrNames.add("urn:perun:user:attribute-def:def:organization");
    Application app = registrarManager.getApplicationById(registrarSession, appId);
    // Authorization
    if (app.getGroup() != null) {
        if (!AuthzResolver.authorizedInternal(sess, "group-checkForSimilarUsers_int_policy", Arrays.asList(app.getGroup(), app.getVo())) && !AuthzResolver.selfAuthorizedForApplication(sess, app)) {
            throw new PrivilegeException("checkForSimilarUsers");
        }
    } else {
        if (!AuthzResolver.authorizedInternal(sess, "vo-checkForSimilarUsers_int_policy", Collections.singletonList(app.getVo())) && !AuthzResolver.selfAuthorizedForApplication(sess, app)) {
            throw new PrivilegeException("checkForSimilarUsers");
        }
    }
    // only for initial VO applications if user==null
    if (app.getType().equals(Application.AppType.INITIAL) && app.getGroup() == null && app.getUser() == null) {
        try {
            LinkedHashMap<String, String> additionalAttributes = BeansUtils.stringToMapOfAttributes(app.getFedInfo());
            PerunPrincipal applicationPrincipal = new PerunPrincipal(app.getCreatedBy(), app.getExtSourceName(), app.getExtSourceType(), app.getExtSourceLoa(), additionalAttributes);
            User u = perun.getUsersManagerBl().getUserByExtSourceInformation(registrarSession, applicationPrincipal);
            if (u != null) {
                // do not show error message in GUI by returning an empty array.
                return convertToIdentities(result);
            }
        } catch (Exception ex) {
        // we don't care, let's try to search by name
        }
        List<ApplicationFormItemData> data = registrarManager.getApplicationDataById(sess, appId);
        // search by email, which should be unique (check is more precise)
        for (ApplicationFormItemData item : data) {
            if ("urn:perun:user:attribute-def:def:preferredMail".equals(item.getFormItem().getPerunDestinationAttribute())) {
                email = item.getValue();
            }
            if (email != null && !email.isEmpty())
                break;
        }
        List<RichUser> users = (email != null && !email.isEmpty()) ? perun.getUsersManagerBl().findRichUsersWithAttributesByExactMatch(registrarSession, email, attrNames) : new ArrayList<>();
        if (users != null && !users.isEmpty()) {
            // found by preferredMail
            return convertToIdentities(users);
        }
        // search by different mail
        // clear previous value
        email = "";
        for (ApplicationFormItemData item : data) {
            if ("urn:perun:member:attribute-def:def:mail".equals(item.getFormItem().getPerunDestinationAttribute())) {
                email = item.getValue();
            }
            if (email != null && !email.isEmpty())
                break;
        }
        users = (email != null && !email.isEmpty()) ? perun.getUsersManagerBl().findRichUsersWithAttributesByExactMatch(registrarSession, email, attrNames) : new ArrayList<>();
        if (users != null && !users.isEmpty()) {
            // found by member mail
            return convertToIdentities(users);
        }
        for (ApplicationFormItemData item : data) {
            if (RegistrarManagerImpl.URN_USER_DISPLAY_NAME.equals(item.getFormItem().getPerunDestinationAttribute())) {
                name = item.getValue();
                // use parsed name to drop mistakes on IDP side
                try {
                    if (name != null && !name.isEmpty()) {
                        Map<String, String> nameMap = Utils.parseCommonName(name);
                        // drop name titles to spread search
                        String newName = "";
                        if (nameMap.get("firstName") != null && !nameMap.get("firstName").isEmpty()) {
                            newName += nameMap.get("firstName") + " ";
                        }
                        if (nameMap.get("lastName") != null && !nameMap.get("lastName").isEmpty()) {
                            newName += nameMap.get("lastName");
                        }
                        // fill parsed name instead of input
                        if (StringUtils.isNotBlank(newName)) {
                            name = newName;
                        }
                    }
                } catch (Exception ex) {
                    log.error("[REGISTRAR] Unable to parse new user's display/common name when searching for similar users.", ex);
                }
                if (name != null && !name.isEmpty())
                    break;
            }
        }
        users = (name != null && !name.isEmpty()) ? perun.getUsersManagerBl().findRichUsersWithAttributesByExactMatch(registrarSession, name, attrNames) : new ArrayList<>();
        if (users != null && !users.isEmpty()) {
            // found by member display name
            return convertToIdentities(users);
        }
        // continue to search by last name
        // clear previous value
        name = "";
        for (ApplicationFormItemData item : data) {
            if (RegistrarManagerImpl.URN_USER_LAST_NAME.equals(item.getFormItem().getPerunDestinationAttribute())) {
                name = item.getValue();
                if (name != null && !name.isEmpty())
                    break;
            }
        }
        if (name != null && !name.isEmpty()) {
            // what was found by name
            return convertToIdentities(perun.getUsersManagerBl().findRichUsersWithAttributesByExactMatch(registrarSession, name, attrNames));
        } else {
            // not found by name
            return convertToIdentities(result);
        }
    } else {
        // not found, since not proper type of application to check users for
        return convertToIdentities(result);
    }
}
Also used : ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Application(cz.metacentrum.perun.registrar.model.Application)

Example 5 with PerunException

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

the class CabinetManagerBlImpl method updatePriorityCoefficient.

public void updatePriorityCoefficient(PerunSession sess, Integer userId, Double rank) throws CabinetException {
    try {
        // get definition
        AttributeDefinition attrDef = perun.getAttributesManager().getAttributeDefinition(cabinetSession, ATTR_COEF_NAMESPACE + ":" + ATTR_COEF_FRIENDLY_NAME);
        // Set attribute value
        Attribute attr = new Attribute(attrDef);
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        attr.setValue(String.valueOf(twoDForm.format(rank)));
        // get user
        User user = perun.getUsersManager().getUserById(cabinetSession, userId);
        // assign or update user's attribute
        perun.getAttributesManager().setAttribute(cabinetSession, user, attr);
    } catch (PerunException e) {
        throw new CabinetException("Failed to update priority coefficient in Perun.", ErrorCodes.PERUN_EXCEPTION, e);
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException)

Aggregations

PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)12 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)3 Attribute (cz.metacentrum.perun.core.api.Attribute)3 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)3 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)3 User (cz.metacentrum.perun.core.api.User)2 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)2 Vo (cz.metacentrum.perun.core.api.Vo)2 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)2 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)2 PerunRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException)2 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)2 ArrayList (java.util.ArrayList)2 Authorship (cz.metacentrum.perun.cabinet.model.Authorship)1 Thanks (cz.metacentrum.perun.cabinet.model.Thanks)1 ThanksForGUI (cz.metacentrum.perun.cabinet.model.ThanksForGUI)1 Group (cz.metacentrum.perun.core.api.Group)1 Member (cz.metacentrum.perun.core.api.Member)1 PerunClient (cz.metacentrum.perun.core.api.PerunClient)1