Search in sources :

Example 1 with SingleColumnRowMapper

use of org.springframework.jdbc.core.SingleColumnRowMapper in project perun by CESNET.

the class RegistrarManagerImpl method getFormItemsWithPrefilledValues.

@Override
public List<ApplicationFormItemWithPrefilledValue> getFormItemsWithPrefilledValues(PerunSession sess, AppType appType, ApplicationForm form) throws PerunException {
    Vo vo = form.getVo();
    Group group = form.getGroup();
    // refresh session (user) to get correct data
    AuthzResolverBlImpl.refreshSession(sess);
    // get necessary params from session
    User user = sess.getPerunPrincipal().getUser();
    String actor = sess.getPerunPrincipal().getActor();
    String extSourceName = sess.getPerunPrincipal().getExtSourceName();
    String extSourceType = sess.getPerunPrincipal().getExtSourceType();
    int extSourceLoa = sess.getPerunPrincipal().getExtSourceLoa();
    Map<String, String> federValues = sess.getPerunPrincipal().getAdditionalInformations();
    RegistrarModule module = getRegistrarModule(form);
    if (module != null)
        module.canBeSubmitted(sess, federValues);
    // Check if it's not DuplicateRegistrationAttempt (for initial)
    if (AppType.INITIAL.equals(appType)) {
        List<Integer> regs = new ArrayList<Integer>();
        if (user != null) {
            // user is known
            try {
                Member m = membersManager.getMemberByUser(registrarSession, vo, user);
                if (group != null) {
                    // get members groups
                    List<Group> g = perun.getGroupsManager().getMemberGroups(registrarSession, m);
                    if (g.contains(group)) {
                        // user is member of group - can't post more initial applications
                        throw new AlreadyRegisteredException("You are already member of group " + group.getName() + ".");
                    } else {
                        // user isn't member of group
                        regs.clear();
                        regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id=? and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), group.getId(), AppState.NEW.toString(), user.getId(), actor, extSourceName));
                        regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id=? and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), group.getId(), AppState.VERIFIED.toString(), user.getId(), actor, extSourceName));
                        if (!regs.isEmpty()) {
                            // user have unprocessed application for group
                            throw new DuplicateRegistrationAttemptException("Initial application for Group: " + group.getName() + " already exists.", actor, extSourceName, regs.get(0));
                        }
                    // pass if have approved or rejected app
                    }
                } else {
                    // user is member of vo, can't post more initial applications
                    throw new AlreadyRegisteredException("You are already member of VO: " + vo.getName());
                }
            } catch (MemberNotExistsException ex) {
                // user is not member of vo
                if (group != null) {
                    // not member of VO - check for unprocessed applications to Group
                    regs.clear();
                    regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id=? and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), group.getId(), AppState.NEW.toString(), user.getId(), actor, extSourceName));
                    regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id=? and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), group.getId(), AppState.VERIFIED.toString(), user.getId(), actor, extSourceName));
                    if (!regs.isEmpty()) {
                        // user have unprocessed application for group - can't post more
                        throw new DuplicateRegistrationAttemptException("Initial application for Group: " + group.getName() + " already exists.", actor, extSourceName, regs.get(0));
                    }
                //throw new InternalErrorException("You must be member of vo: "+vo.getName()+" to apply for membership in group: "+group.getName());
                } else {
                    // not member of VO - check for unprocessed applications
                    regs.clear();
                    regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id is null and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), AppState.NEW.toString(), user.getId(), actor, extSourceName));
                    regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id is null and state=? and (user_id=? or (created_by=? and extSourceName=?))", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), AppState.VERIFIED.toString(), user.getId(), actor, extSourceName));
                    if (!regs.isEmpty()) {
                        // user have unprocessed application for VO - can't post more
                        throw new DuplicateRegistrationAttemptException("Initial application for VO: " + vo.getName() + " already exists.", actor, extSourceName, regs.get(0));
                    }
                // pass not member and have only approved or rejected apps
                }
            }
        } else {
            // user is not known
            if (group != null) {
                // group application
                // get registrations by user logged identity
                regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id=? and created_by=? and extSourceName=? and state<>?", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), group.getId(), actor, extSourceName, AppState.REJECTED.toString()));
                if (!regs.isEmpty()) {
                    throw new DuplicateRegistrationAttemptException("Initial application for Group: " + group.getName() + " already exists.", actor, extSourceName, regs.get(0));
                }
            } else {
                // vo application
                // get registrations by user logged identity
                regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id is null and created_by=? and extSourceName=? and state<>?", new SingleColumnRowMapper<Integer>(Integer.class), AppType.INITIAL.toString(), vo.getId(), actor, extSourceName, AppState.REJECTED.toString()));
                if (!regs.isEmpty()) {
                    throw new DuplicateRegistrationAttemptException("Initial application for VO: " + vo.getName() + " already exists", actor, extSourceName, regs.get(0));
                }
            }
        }
        // if false, throws exception with reason for GUI
        membersManager.canBeMemberWithReason(sess, vo, user, String.valueOf(extSourceLoa));
    }
    // if extension, user != null !!
    if (AppType.EXTENSION.equals(appType)) {
        if (user == null) {
            throw new RegistrarException("Trying to get extension application for non-existing user. Try to log-in with different identity known to Perun.");
        }
        if (form.getGroup() != null) {
            throw new RegistrarException("You are already member of group " + form.getGroup().getShortName() + ".");
        }
        // check for submitted registrations (only for VO)
        List<Integer> regs = new ArrayList<Integer>();
        regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id is null and user_id=? and state=?", new SingleColumnRowMapper<Integer>(Integer.class), AppType.EXTENSION.toString(), vo.getId(), user.getId(), AppState.NEW.toString()));
        regs.addAll(jdbc.query("select id from application where apptype=? and vo_id=? and group_id is null and user_id=? and state=?", new SingleColumnRowMapper<Integer>(Integer.class), AppType.EXTENSION.toString(), vo.getId(), user.getId(), AppState.VERIFIED.toString()));
        if (!regs.isEmpty()) {
            // user have unprocessed application for group
            throw new DuplicateRegistrationAttemptException("Extension application for VO: " + vo.getName() + " already exists.", actor, extSourceName, regs.get(0));
        }
        Member member = membersManager.getMemberByUser(sess, vo, user);
        // if false, throws exception with reason for GUI
        membersManager.canExtendMembershipWithReason(sess, member);
    }
    // PROCEED
    Map<String, String> parsedName = extractNames(federValues);
    List<ApplicationFormItem> formItems = getFormItems(registrarSession, form, appType);
    List<ApplicationFormItemWithPrefilledValue> itemsWithValues = new ArrayList<ApplicationFormItemWithPrefilledValue>();
    for (ApplicationFormItem item : formItems) {
        itemsWithValues.add(new ApplicationFormItemWithPrefilledValue(item, null));
    }
    // get user and member attributes from DB for existing users
    if (user != null) {
        Map<String, Attribute> map = new HashMap<String, Attribute>();
        // process user attributes
        List<Attribute> userAttributes = attrManager.getAttributes(registrarSession, user);
        for (Attribute att : userAttributes) {
            map.put(att.getName(), att);
        }
        // process member attributes
        try {
            Member member = membersManager.getMemberByUser(registrarSession, vo, user);
            List<Attribute> memberAttributes = attrManager.getAttributes(registrarSession, member);
            for (Attribute att : memberAttributes) {
                map.put(att.getName(), att);
            }
        } catch (MemberNotExistsException ex) {
        // we don't care that user is not yet member
        }
        Iterator<ApplicationFormItemWithPrefilledValue> it = ((Collection<ApplicationFormItemWithPrefilledValue>) itemsWithValues).iterator();
        while (it.hasNext()) {
            ApplicationFormItemWithPrefilledValue itemW = it.next();
            String dstAtt = itemW.getFormItem().getPerunDestinationAttribute();
            // skip items without perun attr reference
            if (dstAtt == null || dstAtt.equals(""))
                continue;
            // if attr exist and value != null
            if (map.get(dstAtt) != null && map.get(dstAtt).getValue() != null) {
                if (itemW.getFormItem().getType() == PASSWORD) {
                    // if login in namespace exists, do not return password field
                    // because application form is not place to change login or password
                    it.remove();
                } else {
                    // else set value
                    itemW.setPrefilledValue(BeansUtils.attributeValueToString(map.get(dstAtt)));
                }
            }
        }
    }
    List<ApplicationFormItemWithPrefilledValue> itemsWithMissingData = new ArrayList<ApplicationFormItemWithPrefilledValue>();
    // get user attributes from federation
    Iterator<ApplicationFormItemWithPrefilledValue> it = (itemsWithValues).iterator();
    while (it.hasNext()) {
        ApplicationFormItemWithPrefilledValue itemW = it.next();
        String fa = itemW.getFormItem().getFederationAttribute();
        if (fa != null && !fa.isEmpty()) {
            // FILL VALUE FROM FEDERATION
            String s = federValues.get(fa);
            if (s != null && !s.isEmpty()) {
                // In case of email, value from the federation can contain more than one entries, entries are separated by semi-colon
                if (itemW.getFormItem().getType().equals(ApplicationFormItem.Type.VALIDATED_EMAIL)) {
                    if (itemW.getPrefilledValue() != null && !itemW.getPrefilledValue().isEmpty()) {
                        s = itemW.getPrefilledValue() + ";" + s;
                    }
                }
                // remove password field if (login) prefilled from federation
                if (itemW.getFormItem().getType() == PASSWORD) {
                    it.remove();
                    continue;
                }
                itemW.setPrefilledValue(s);
                itemW.setAssuranceLevel(federValues.get(shibLoAVar));
            }
            // TRY TO CONSTRUCT THE VALUE FROM PARTIAL FED-INFO
            ApplicationFormItem item = itemW.getFormItem();
            String dstAtt = item.getPerunDestinationAttribute();
            if (URN_USER_TITLE_BEFORE.equals(dstAtt)) {
                String titleBefore = parsedName.get("titleBefore");
                if (titleBefore != null && !titleBefore.trim().isEmpty())
                    itemW.setPrefilledValue(titleBefore);
            } else if (URN_USER_TITLE_AFTER.equals(dstAtt)) {
                String titleAfter = parsedName.get("titleAfter");
                if (titleAfter != null && !titleAfter.trim().isEmpty())
                    itemW.setPrefilledValue(titleAfter);
            } else if (URN_USER_FIRST_NAME.equals(dstAtt)) {
                String firstName = parsedName.get("firstName");
                if (firstName != null && !firstName.trim().isEmpty())
                    itemW.setPrefilledValue(firstName);
            } else if (URN_USER_LAST_NAME.equals(dstAtt)) {
                String lastName = parsedName.get("lastName");
                if (lastName != null && !lastName.trim().isEmpty())
                    itemW.setPrefilledValue(lastName);
            } else if (URN_USER_DISPLAY_NAME.equals(dstAtt)) {
                // overwrite only if not filled by Perun
                if (itemW.getPrefilledValue() == null || itemW.getPrefilledValue().isEmpty()) {
                    String displayName = "";
                    if (parsedName.get("titleBefore") != null && !parsedName.get("titleBefore").isEmpty())
                        displayName += parsedName.get("titleBefore");
                    if (parsedName.get("firstName") != null && !parsedName.get("firstName").isEmpty()) {
                        if (!displayName.isEmpty())
                            displayName += " ";
                        displayName += parsedName.get("firstName");
                    }
                    if (parsedName.get("lastName") != null && !parsedName.get("lastName").isEmpty()) {
                        if (!displayName.isEmpty())
                            displayName += " ";
                        displayName += parsedName.get("lastName");
                    }
                    if (parsedName.get("titleAfter") != null && !parsedName.get("titleAfter").isEmpty()) {
                        if (!displayName.isEmpty())
                            displayName += " ";
                        displayName += parsedName.get("titleAfter");
                    }
                    itemW.setPrefilledValue(displayName);
                }
            }
            // We do require value from IDP (federation) if attribute is supposed to be pre-filled and item is required and not editable to users
            if ((itemW.getPrefilledValue() == null || itemW.getPrefilledValue().isEmpty()) && itemW.getFormItem().isRequired() && (Type.FROM_FEDERATION_HIDDEN.equals(itemW.getFormItem().getType()) || Type.FROM_FEDERATION_SHOW.equals(itemW.getFormItem().getType()))) {
                itemsWithMissingData.add(itemW);
            }
        }
    }
    if (!itemsWithMissingData.isEmpty() && extSourceType.equals(ExtSourcesManager.EXTSOURCE_IDP)) {
        // throw exception only if user is logged-in by Federation IDP
        String IDP = federValues.get("Shib-Identity-Provider");
        log.error("[REGISTRAR] IDP {} doesn't provide data for following form items: {}", IDP, itemsWithMissingData);
        throw new MissingRequiredDataException("Your IDP doesn't provide data required by this application form.", itemsWithMissingData);
    }
    // return prefilled form
    return itemsWithValues;
}
Also used : SingleColumnRowMapper(org.springframework.jdbc.core.SingleColumnRowMapper) ApplicationFormItemWithPrefilledValue(cz.metacentrum.perun.registrar.model.ApplicationFormItemWithPrefilledValue) ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem) RegistrarModule(cz.metacentrum.perun.registrar.RegistrarModule)

Aggregations

RegistrarModule (cz.metacentrum.perun.registrar.RegistrarModule)1 ApplicationFormItem (cz.metacentrum.perun.registrar.model.ApplicationFormItem)1 ApplicationFormItemWithPrefilledValue (cz.metacentrum.perun.registrar.model.ApplicationFormItemWithPrefilledValue)1 SingleColumnRowMapper (org.springframework.jdbc.core.SingleColumnRowMapper)1