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;
}
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;
}
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;
}
Aggregations