Search in sources :

Example 1 with Validation

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

the class MembersManagerBlImpl method createSponsoredMembers.

@Override
public List<Map<String, String>> createSponsoredMembers(PerunSession sess, Vo vo, String namespace, List<String> names, String email, User sponsor, LocalDate validityTo, boolean sendActivationLink, String url, Validation validation) {
    List<Map<String, String>> result = new ArrayList<>();
    PasswordManagerModule module = getPerunBl().getUsersManagerBl().getPasswordManagerModule(sess, namespace);
    Set<Member> createdMembers = new HashSet<>();
    for (String name : names) {
        Map<String, String> accountDataToReturn = new HashMap<>();
        SponsoredUserData data = new SponsoredUserData();
        data.setNamespace(namespace);
        if (name.contains(";")) {
            String[] split = name.split(";", 2);
            data.setFirstName(split[0]);
            data.setLastName(split[1]);
        } else {
            data.setGuestName(name);
        }
        String password = module.generateRandomPassword(sess, null);
        // create sponsored member
        User user;
        try {
            // async validation must be performed at the end, not directly during member creation
            Validation localValidation = (Objects.equals(Validation.ASYNC, validation)) ? Validation.NONE : validation;
            Member member = createSponsoredMember(sess, data, vo, sponsor, validityTo, sendActivationLink, url, localValidation);
            user = perunBl.getUsersManagerBl().getUserByMember(sess, member);
            // get login to return
            String login = perunBl.getAttributesManagerBl().getAttribute(sess, user, PasswordManagerModule.LOGIN_PREFIX + namespace).valueAsString();
            accountDataToReturn.put(STATUS, OK);
            accountDataToReturn.put(LOGIN, login);
            accountDataToReturn.put(PASSWORD, password);
            accountDataToReturn.put(NAME, name);
            result.add(accountDataToReturn);
            createdMembers.add(member);
        } catch (Exception e) {
            accountDataToReturn.put(NAME, name);
            accountDataToReturn.put(STATUS, e.getMessage());
            result.add(accountDataToReturn);
        }
    }
    // perform async validation if necessary
    if (Objects.equals(Validation.ASYNC, validation)) {
        for (Member member : createdMembers) {
            getPerunBl().getMembersManagerBl().validateMemberAsync(sess, member);
        }
    }
    return result;
}
Also used : Validation(cz.metacentrum.perun.core.api.Validation) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NamespaceRulesNotExistsException(cz.metacentrum.perun.core.api.exceptions.NamespaceRulesNotExistsException) RelationExistsException(cz.metacentrum.perun.core.api.exceptions.RelationExistsException) MemberAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.MemberAlreadyRemovedException) PasswordCreationFailedException(cz.metacentrum.perun.core.api.exceptions.PasswordCreationFailedException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) MemberGroupMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberGroupMismatchException) IllegalArgumentException(cz.metacentrum.perun.core.api.exceptions.IllegalArgumentException) UserExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) IOException(java.io.IOException) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) InvalidLoginException(cz.metacentrum.perun.core.api.exceptions.InvalidLoginException) RoleManagementRulesNotExistsException(cz.metacentrum.perun.core.api.exceptions.RoleManagementRulesNotExistsException) BanAlreadyExistsException(cz.metacentrum.perun.core.api.exceptions.BanAlreadyExistsException) InvalidSponsoredUserDataException(cz.metacentrum.perun.core.api.exceptions.InvalidSponsoredUserDataException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) LoginNotExistsException(cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException) MemberNotValidYetException(cz.metacentrum.perun.core.api.exceptions.MemberNotValidYetException) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) PasswordStrengthException(cz.metacentrum.perun.core.api.exceptions.PasswordStrengthException) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException) AttributeValueException(cz.metacentrum.perun.core.api.exceptions.AttributeValueException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BanNotExistsException(cz.metacentrum.perun.core.api.exceptions.BanNotExistsException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PasswordManagerModule(cz.metacentrum.perun.core.implApi.modules.pwdmgr.PasswordManagerModule) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) SponsoredUserData(cz.metacentrum.perun.core.api.SponsoredUserData) HashSet(java.util.HashSet)

Example 2 with Validation

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

the class MembersManagerEntry method createMember.

@Override
public Member createMember(PerunSession sess, Vo vo, ExtSource extSource, String login, List<Group> groups) throws WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, VoNotExistsException, ExtSourceNotExistsException, PrivilegeException, GroupNotExistsException {
    Utils.checkPerunSession(sess);
    getPerunBl().getVosManagerBl().checkVoExists(sess, vo);
    getPerunBl().getExtSourcesManagerBl().checkExtSourceExists(sess, extSource);
    // if any group is not from the vo, throw an exception
    if (groups != null) {
        for (Group group : groups) {
            perunBl.getGroupsManagerBl().checkGroupExists(sess, group);
            if (group.getVoId() != vo.getId())
                throw new InternalErrorException("Group " + group + " is not from the vo " + vo + " where user with login " + login + " from ExtSource " + extSource + " should be added.");
        }
    }
    // Authorization
    if (!AuthzResolver.authorizedInternal(sess, "createMember_Vo_ExtSource_String_List<Group>_policy", Arrays.asList(vo, extSource))) {
        // also group admin of all affected groups is ok
        if (groups != null && !groups.isEmpty()) {
            for (Group group : groups) {
                if (!AuthzResolver.authorizedInternal(sess, "createMember_Vo_ExtSource_String_List<Group>_policy", group)) {
                    throw new PrivilegeException(sess, "createMember - from login and extSource");
                }
            }
            // ExtSource has to be assigned to at least one of the groups
            boolean groupContainsExtSource = groups.stream().map(group -> getPerunBl().getExtSourcesManagerBl().getGroupExtSources(sess, group)).anyMatch(extSources -> extSources.contains(extSource));
            if (!groupContainsExtSource) {
                throw new PrivilegeException(sess, "createMember - from login and extSource");
            }
        } else {
            throw new PrivilegeException(sess, "createMember - from login and extSource");
        }
    }
    // we run async validation
    Member member = getMembersManagerBl().createMember(sess, vo, extSource, login, groups);
    getMembersManagerBl().validateMemberAsync(sess, member);
    return member;
}
Also used : InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Arrays(java.util.Arrays) Validation(cz.metacentrum.perun.core.api.Validation) Candidate(cz.metacentrum.perun.core.api.Candidate) AuthzResolver(cz.metacentrum.perun.core.api.AuthzResolver) Vo(cz.metacentrum.perun.core.api.Vo) Date(java.util.Date) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) LoggerFactory(org.slf4j.LoggerFactory) SpecificUserType(cz.metacentrum.perun.core.api.SpecificUserType) MembersManager(cz.metacentrum.perun.core.api.MembersManager) ExtSource(cz.metacentrum.perun.core.api.ExtSource) MembersManagerBl(cz.metacentrum.perun.core.bl.MembersManagerBl) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) LoginNotExistsException(cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException) NamespaceRulesNotExistsException(cz.metacentrum.perun.core.api.exceptions.NamespaceRulesNotExistsException) Map(java.util.Map) Role(cz.metacentrum.perun.core.api.Role) MemberNotValidYetException(cz.metacentrum.perun.core.api.exceptions.MemberNotValidYetException) Attribute(cz.metacentrum.perun.core.api.Attribute) MembersPageQuery(cz.metacentrum.perun.core.api.MembersPageQuery) MemberAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.MemberAlreadyRemovedException) MemberWithSponsors(cz.metacentrum.perun.core.api.MemberWithSponsors) PasswordCreationFailedException(cz.metacentrum.perun.core.api.exceptions.PasswordCreationFailedException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Collectors(java.util.stream.Collectors) User(cz.metacentrum.perun.core.api.User) Sponsor(cz.metacentrum.perun.core.api.Sponsor) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) List(java.util.List) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) LocalDate(java.time.LocalDate) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) RichMember(cz.metacentrum.perun.core.api.RichMember) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) PasswordStrengthException(cz.metacentrum.perun.core.api.exceptions.PasswordStrengthException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) SponsoredUserData(cz.metacentrum.perun.core.api.SponsoredUserData) Resource(cz.metacentrum.perun.core.api.Resource) PerunSession(cz.metacentrum.perun.core.api.PerunSession) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) NamespaceRules(cz.metacentrum.perun.core.api.NamespaceRules) Paginated(cz.metacentrum.perun.core.api.Paginated) Group(cz.metacentrum.perun.core.api.Group) ArrayList(java.util.ArrayList) ParentGroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.ParentGroupNotExistsException) MemberNotSuspendedException(cz.metacentrum.perun.core.api.exceptions.MemberNotSuspendedException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException) PasswordResetMailNotExistsException(cz.metacentrum.perun.core.api.exceptions.PasswordResetMailNotExistsException) Member(cz.metacentrum.perun.core.api.Member) Logger(org.slf4j.Logger) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) Iterator(java.util.Iterator) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) AlreadyMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Utils(cz.metacentrum.perun.core.impl.Utils) InvalidLoginException(cz.metacentrum.perun.core.api.exceptions.InvalidLoginException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) InvalidSponsoredUserDataException(cz.metacentrum.perun.core.api.exceptions.InvalidSponsoredUserDataException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) Collections(java.util.Collections) Status(cz.metacentrum.perun.core.api.Status) Group(cz.metacentrum.perun.core.api.Group) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Example 3 with Validation

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

the class MembersManagerBlImpl method createSponsoredMembersFromCSV.

@Override
public List<Map<String, String>> createSponsoredMembersFromCSV(PerunSession sess, Vo vo, String namespace, List<String> data, String header, User sponsor, LocalDate validityTo, boolean sendActivationLink, String url, Validation validation, List<Group> groups) {
    List<Map<String, String>> totalResult = new ArrayList<>();
    Set<Member> createdMembers = new HashSet<>();
    List<String> dataWithHeader = new ArrayList<>();
    dataWithHeader.add(header);
    dataWithHeader.addAll(data);
    MappingIterator<Map<String, String>> dataIterator;
    try {
        byte[] bytes = String.join("\n", dataWithHeader).getBytes(StandardCharsets.UTF_8);
        dataIterator = new CsvMapper().readerFor(Map.class).with(CsvSchema.emptySchema().withHeader().withColumnSeparator(';')).readValues(bytes);
    } catch (IOException e) {
        log.error("Failed to parse received CSV data.", e);
        throw new InternalErrorException("Failed to parse received CSV data.", e);
    }
    int processedCounter = 0;
    while (dataIterator.hasNext()) {
        Map<String, String> singleRow = dataIterator.next();
        // async validation must be performed at the end, not directly during member creation
        Validation localValidation = (Objects.equals(Validation.ASYNC, validation)) ? Validation.NONE : validation;
        Map<String, Object> originalResult = createSingleSponsoredMemberFromCSV(sess, vo, namespace, singleRow, sponsor, validityTo, sendActivationLink, url, localValidation, groups);
        // convert result to expected "type" for outer API
        Map<String, String> newResult = new HashMap<>();
        if (OK.equals(originalResult.get(STATUS))) {
            newResult.put(STATUS, (String) originalResult.get(STATUS));
            newResult.put(LOGIN, (String) originalResult.get(LOGIN));
            newResult.put(PASSWORD, (String) originalResult.get(PASSWORD));
            createdMembers.add((Member) originalResult.get(MEMBER));
        } else {
            // error when creating
            newResult.put(STATUS, (String) originalResult.get(STATUS));
        }
        newResult.put(NAME, data.get(processedCounter++));
        totalResult.add(newResult);
    }
    // perform async validation if necessary
    if (Objects.equals(Validation.ASYNC, validation)) {
        for (Member member : createdMembers) {
            getPerunBl().getMembersManagerBl().validateMemberAsync(sess, member);
        }
    }
    return totalResult;
}
Also used : Validation(cz.metacentrum.perun.core.api.Validation) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Aggregations

Member (cz.metacentrum.perun.core.api.Member)3 RichMember (cz.metacentrum.perun.core.api.RichMember)3 Validation (cz.metacentrum.perun.core.api.Validation)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 SponsoredUserData (cz.metacentrum.perun.core.api.SponsoredUserData)2 User (cz.metacentrum.perun.core.api.User)2 AlreadyMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException)2 AlreadySponsorException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException)2 AlreadySponsoredMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException)2 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)2 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)2 ExtendMembershipException (cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)2 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)2 GroupResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException)2 InvalidLoginException (cz.metacentrum.perun.core.api.exceptions.InvalidLoginException)2 InvalidSponsoredUserDataException (cz.metacentrum.perun.core.api.exceptions.InvalidSponsoredUserDataException)2 LoginNotExistsException (cz.metacentrum.perun.core.api.exceptions.LoginNotExistsException)2 MemberAlreadyRemovedException (cz.metacentrum.perun.core.api.exceptions.MemberAlreadyRemovedException)2