use of cz.metacentrum.perun.core.api.SponsoredUserData in project perun by CESNET.
the class MembersManagerBlImpl method createSingleSponsoredMemberFromCSV.
/**
* Creates a new user from given data and sponsors him in the given vo.
*
* @param sess sess
* @param vo vo, where the new user will be sponsored
* @param namespace namespace used to define an external system where
* the user will have a new login generated (currently, only 'mu' namespace is supported)
* @param data values used to create the new user.
* Required values are - firstname, lastname, urn:perun:user:attribute-def:def:preferredMail
* Optional values are - urn:perun:user:attribute-def:def:note
* @param sponsor user, who will be set as a sponsor to the newly created user
* @param validityTo validity of the sponsorship. If null, the sponsorship will not be automatically canceled.
* @param url base URL of Perun Instance
* @param validation Which type of validation to perform. If you are using ASYNC, do not call this method in a cycle!
* @param groups groups, to which will be the created users assigned
* @return result of the procedure
*/
private Map<String, Object> createSingleSponsoredMemberFromCSV(PerunSession sess, Vo vo, String namespace, Map<String, String> data, User sponsor, LocalDate validityTo, boolean sendActivationLink, String url, Validation validation, List<Group> groups) {
for (String requiredField : SPONSORED_MEMBER_REQUIRED_FIELDS) {
if (!data.containsKey(requiredField)) {
log.error("Invalid data passed, missing required value: {}", requiredField);
throw new InternalErrorException("Invalid data passed, missing required value: " + requiredField);
}
}
Set<String> additionalValues = new HashSet<>(data.keySet());
additionalValues.removeAll(SPONSORED_MEMBER_REQUIRED_FIELDS);
for (String valueName : additionalValues) {
if (!SPONSORED_MEMBER_ADDITIONAL_FIELDS.contains(valueName)) {
log.error("Not allowed additional value passed, value: {}", valueName);
throw new InternalErrorException("Not allowed additional value passed, value: " + valueName);
}
}
// We don't want these values to be set as attributes
additionalValues.removeIf(val -> !val.startsWith(ATTR_PREFIX));
String email = data.get(A_U_PREF_MAIL);
SponsoredUserData input = new SponsoredUserData();
input.setNamespace(namespace);
input.setFirstName(data.get("firstname"));
input.setLastName(data.get("lastname"));
input.setEmail(email);
String password = null;
if (isNotBlank(namespace)) {
PasswordManagerModule module = getPerunBl().getUsersManagerBl().getPasswordManagerModule(sess, namespace);
password = module.generateRandomPassword(sess, null);
input.setPassword(password);
}
if (data.containsKey("login")) {
input.setLogin(data.get("login"));
}
// create sponsored member
Map<String, Object> status = new HashMap<>();
Member member = null;
try {
member = createSponsoredMember(sess, input, vo, sponsor, validityTo, sendActivationLink, url, validation);
User user = perunBl.getUsersManagerBl().getUserByMember(sess, member);
// get login to return
String login = null;
if (isNotBlank(namespace)) {
login = perunBl.getAttributesManagerBl().getAttribute(sess, user, PasswordManagerModule.LOGIN_PREFIX + namespace).valueAsString();
}
status.put(LOGIN, login);
status.put(PASSWORD, password);
setAdditionalValues(sess, additionalValues, data, user, member);
// we must pass member back for the purpose of validation
status.put(MEMBER, member);
status.put(STATUS, OK);
} catch (Exception e) {
log.error("Failed to create a sponsored user.", e);
status.put(STATUS, e.getMessage());
}
if (groups != null && !groups.isEmpty()) {
Map<Integer, String> groupAssignmentErrors = new HashMap<>();
if (member != null) {
for (Group group : groups) {
try {
perunBl.getGroupsManagerBl().addMember(sess, group, member);
} catch (Exception e) {
groupAssignmentErrors.put(group.getId(), e.getMessage());
log.error("Failed to add a member to a group. Member: {}, Group: {}", member, group, e);
}
}
}
status.put(GROUP_ADDING_ERRORS, groupAssignmentErrors);
}
return status;
}
use of cz.metacentrum.perun.core.api.SponsoredUserData 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.SponsoredUserData in project perun by CESNET.
the class MembersManagerBlImpl method setSponsoredMember.
@Override
public Member setSponsoredMember(PerunSession session, Vo vo, User userToBeSponsored, String namespace, String password, User sponsor, Validation validation) throws AlreadyMemberException, ExtendMembershipException, UserNotInRoleException, WrongAttributeValueException, WrongReferenceAttributeValueException, LoginNotExistsException, PasswordCreationFailedException, InvalidLoginException, ExtSourceNotExistsException, AlreadySponsorException, InvalidSponsoredUserDataException, NamespaceRulesNotExistsException, PasswordStrengthException {
SponsoredUserData data = new SponsoredUserData();
data.setNamespace(namespace);
data.setPassword(password);
return setSponsoredMember(session, data, vo, userToBeSponsored, sponsor, null, validation);
}
use of cz.metacentrum.perun.core.api.SponsoredUserData in project perun by CESNET.
the class MembersManagerEntryIntegrationTest method createSponsoredMember.
private Member createSponsoredMember(PerunSession sess, Vo vo, String namespace, Map<String, String> names, String password, String email, User sponsor) throws Exception {
SponsoredUserData input = new SponsoredUserData();
input.setNamespace(namespace);
input.setFirstName(names.get("firstName"));
input.setLastName(names.get("lastName"));
input.setGuestName(names.get("guestName"));
input.setTitleBefore(names.get("titleBefore"));
input.setTitleAfter(names.get("titleAfter"));
input.setPassword(password);
input.setEmail(email);
return perun.getMembersManagerBl().createSponsoredMember(sess, input, vo, sponsor, null, false, null, Validation.SYNC);
}
Aggregations