use of cz.metacentrum.perun.registrar.model.ApplicationFormItemData 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<RichUser>();
List<String> attrNames = new ArrayList<String>();
attrNames.add("urn:perun:user:attribute-def:def:preferredMail");
attrNames.add("urn:perun:user:attribute-def:def:organization");
Application app = registrarManager.getApplicationById(registrarSession, appId);
if (app.getGroup() == null) {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, app.getVo())) {
if (sess.getPerunPrincipal().getUser() != null) {
// check if application to find similar users by belongs to user
if (!sess.getPerunPrincipal().getUser().equals(app.getUser()))
throw new PrivilegeException("checkForSimilarUsers");
} else {
if (!sess.getPerunPrincipal().getExtSourceName().equals(app.getExtSourceName()) && !sess.getPerunPrincipal().getActor().equals(app.getCreatedBy()))
throw new PrivilegeException("checkForSimilarUsers");
}
}
} else {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, app.getVo()) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, app.getGroup())) {
if (sess.getPerunPrincipal().getUser() != null) {
// check if application to find similar users by belongs to user
if (!sess.getPerunPrincipal().getUser().equals(app.getUser()))
throw new PrivilegeException("checkForSimilarUsers");
} else {
if (!sess.getPerunPrincipal().getExtSourceName().equals(app.getExtSourceName()) && !sess.getPerunPrincipal().getActor().equals(app.getCreatedBy()))
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 {
User u = perun.getUsersManager().getUserByExtSourceNameAndExtLogin(registrarSession, app.getExtSourceName(), app.getCreatedBy());
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.getUsersManager().findRichUsersWithAttributesByExactMatch(registrarSession, email, attrNames) : new ArrayList<RichUser>();
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.getUsersManager().findRichUsersWithAttributesByExactMatch(registrarSession, email, attrNames) : new ArrayList<RichUser>();
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 (newName != null && !newName.isEmpty()) {
name = newName;
}
}
} catch (Exception ex) {
log.error("[REGISTRAR] Unable to parse new user's display/common name when searching for similar users. Exception: {}", ex);
}
if (name != null && !name.isEmpty())
break;
}
}
users = (name != null && !name.isEmpty()) ? perun.getUsersManager().findRichUsersWithAttributesByExactMatch(registrarSession, name, attrNames) : new ArrayList<RichUser>();
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.getUsersManager().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);
}
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItemData in project perun by CESNET.
the class RegistrarBaseIntegrationTest method applyForMembershipInVO.
private static void applyForMembershipInVO(RegistrarManager registrarManager, PerunBl perun, Vo vo, PerunSession user) throws PerunException, DuplicateRegistrationAttemptException {
Map<String, String> feder = new HashMap<String, String>();
feder.put("Shib-Person-displayName", "pplk. doc. Ing. Václav Rumcajs, DrSc.");
feder.put("Shib-Person-commonName", "Václav Rumcajs");
feder.put("Shib-Person-givenName", "Václav");
feder.put("Shib-Person-sureName", "Rumcajs");
feder.put("Shib-Person-o", "Les Řáholec");
feder.put("Shib-EP-Affiliation", "member");
feder.put("Shib-InetOrgPerson-mail", "mail@gmail.org");
feder.put("Shib-EP-PrincipalName", user.getPerunPrincipal().getActor());
user.getPerunPrincipal().getAdditionalInformations().putAll(feder);
List<ApplicationFormItemWithPrefilledValue> prefilledForm = registrarManager.getFormItemsWithPrefilledValues(user, INITIAL, registrarManager.getFormForVo(vo));
//data z federace a od uzivatele
Application application = new Application();
application.setType(INITIAL);
application.setCreatedAt(user.getPerunPrincipal().getActor());
application.setExtSourceName(user.getPerunPrincipal().getExtSourceName());
application.setExtSourceType(ExtSourcesManager.EXTSOURCE_IDP);
application.setFedInfo(feder.toString());
application.setVo(vo);
List<ApplicationFormItemData> data = new ArrayList<ApplicationFormItemData>();
for (ApplicationFormItemWithPrefilledValue itemW : prefilledForm) {
ApplicationFormItem item = itemW.getFormItem();
//log.info("prefilled item "+itemW);
if (item.getShortname().equals("preferredMail")) {
data.add(new ApplicationFormItemData(item, item.getShortname(), "rumcajs@gmail.com", "0"));
} else if (item.getShortname().equals("username")) {
data.add(new ApplicationFormItemData(item, item.getShortname(), "rumcik", "0"));
} else {
//nechej predvyplnenou hodnotu
data.add(new ApplicationFormItemData(item, item.getShortname(), itemW.getPrefilledValue(), itemW.getAssuranceLevel()));
}
}
registrarManager.createApplication(user, application, data);
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItemData in project perun by CESNET.
the class MailManagerImpl method setUsersMailAsTo.
/**
* Set users mail as TO param for mail message.
*
* Default value is empty (mail won't be sent).
*
* Mail is taken from first founded form item of type VALIDATED_MAIL.
* If none found and user exists, it's taken from
* user's attribute: preferredMail
*
* @param message message to set TO param
* @param app application
* @param data application data
*/
private void setUsersMailAsTo(SimpleMailMessage message, Application app, List<ApplicationFormItemData> data) {
message.setTo("");
try {
// get TO param from VALIDATED_EMAIL form items (it's best fit)
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (ApplicationFormItem.Type.VALIDATED_EMAIL.equals(item.getType())) {
if (value != null && !value.isEmpty()) {
message.setTo(d.getValue());
// use first mail address
return;
}
}
}
// get TO param from other form items related to "user - preferredMail"
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (item.getPerunDestinationAttribute() != null && !item.getPerunDestinationAttribute().isEmpty()) {
if (item.getPerunDestinationAttribute().equalsIgnoreCase(URN_USER_PREFERRED_MAIL)) {
if (value != null && !value.isEmpty()) {
message.setTo(d.getValue());
// use first mail address
return;
}
}
}
}
// get TO param from other form items related to "member - mail"
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
String value = d.getValue();
if (item.getPerunDestinationAttribute() != null && !item.getPerunDestinationAttribute().isEmpty()) {
if (item.getPerunDestinationAttribute().equalsIgnoreCase(URN_MEMBER_MAIL)) {
if (value != null && !value.isEmpty()) {
message.setTo(d.getValue());
// use first mail address
return;
}
}
}
}
// get TO param from user if not present on application form
if (app.getUser() != null) {
User u = usersManager.getUserById(registrarSession, app.getUser().getId());
Attribute a = attrManager.getAttribute(registrarSession, u, URN_USER_PREFERRED_MAIL);
if (a != null && a.getValue() != null) {
message.setTo(BeansUtils.attributeValueToString(a));
}
}
} catch (Exception ex) {
// we don't care about exceptions - we have backup address (empty = mail not sent)
log.error("[MAIL MANAGER] Exception thrown when getting users mail address for application: {}", app);
}
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItemData in project perun by CESNET.
the class MailManagerImpl method substituteCommonStrings.
/**
* Substitute common strings in mail text by data provided by
* application, application data and perun itself.
*
* Substituted strings are:
*
* {voName} - full vo name
* {groupName} - group short name
* {displayName} - user's display name returned from federation if present on form
* {firstName} - first name of user if present on form as separate form item
* {lastName} - last name of user if present on form as separate form item
* {appId} - application id
* {actor} - login in external system used when submitting application
* {extSource} - external system used for authentication when submitting application
* {appGuiUrl} - url to application GUI for user to see applications state
*
* {appGuiUrl-[authz]} - url to application GUI for user to see applications state
* {perunGuiUrl-[authz]} - url to perun GUI (user detail)
* {appDetailUrl-[authz]} - link for VO admin to approve / reject application
*
* {logins} - list of all logins from application
* {membershipExpiration} - membership expiration date
* {mail} - user preferred mail submitted on application or stored in a system
*
* {customMessage} - message passed by admin to mail (e.g. reason of application reject)
* {errors} - include errors which ocured when processing registrar actions
* (e.g. login reservation errors passed to mail for VO admin)
*
* (if possible links are for: Kerberos, Federation and Certificate authz)
*
* @param app Application to substitute strings for (get VO etc.)
* @param data ApplicationData needed for sustitution (displayName etc.)
* @param mailText String to substitute parts of
* @param reason Custom message passed by vo admin
* @param exceptions list of exceptions thrown when processing registrar actions
* @return modified text
*/
private String substituteCommonStrings(Application app, List<ApplicationFormItemData> data, String mailText, String reason, List<Exception> exceptions) {
// replace app ID
if (mailText.contains("{appId}")) {
mailText = mailText.replace("{appId}", app.getId() + "");
}
// replace actor (app created by)
if (mailText.contains("{actor}")) {
mailText = mailText.replace("{actor}", app.getCreatedBy() + "");
}
// replace ext source (app created by)
if (mailText.contains("{extSource}")) {
mailText = mailText.replace("{extSource}", app.getExtSourceName() + "");
}
// replace voName
if (mailText.contains("{voName}")) {
mailText = mailText.replace("{voName}", app.getVo().getName());
}
// replace groupName
if (mailText.contains("{groupName}")) {
if (app.getGroup() != null) {
mailText = mailText.replace("{groupName}", app.getGroup().getShortName());
} else {
mailText = mailText.replace("{groupName}", "");
}
}
// replace customMessage (reason)
if (mailText.contains("{customMessage}")) {
if (reason != null && !reason.isEmpty()) {
mailText = mailText.replace("{customMessage}", reason);
} else {
mailText = mailText.replace("{customMessage}", "");
}
}
// replace displayName
if (mailText.contains("{displayName}")) {
// backup
String nameText = "";
for (ApplicationFormItemData d : data) {
// core attribute
if ("urn:perun:user:attribute-def:core:displayName".equals(d.getFormItem().getPerunDestinationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
nameText = d.getValue();
break;
}
}
// federation attribute
if ("cn".equals(d.getFormItem().getFederationAttribute()) || "displayName".equals(d.getFormItem().getFederationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
nameText = d.getValue();
break;
}
}
}
if (nameText.isEmpty()) {
User user = null;
if (app.getUser() != null) {
user = app.getUser();
} else {
try {
user = usersManager.getUserByExtSourceNameAndExtLogin(registrarSession, app.getExtSourceName(), app.getCreatedBy());
} catch (Exception ex) {
// user not found is ok
}
}
if (user != null)
nameText = user.getDisplayName();
}
mailText = mailText.replace("{displayName}", nameText);
}
// replace firstName
if (mailText.contains("{firstName}")) {
// backup
String nameText = "";
for (ApplicationFormItemData d : data) {
if ("urn:perun:user:attribute-def:core:firstName".equals(d.getFormItem().getPerunDestinationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
nameText = d.getValue();
break;
}
}
}
if (nameText.isEmpty()) {
User user = null;
if (app.getUser() != null) {
user = app.getUser();
} else {
try {
user = usersManager.getUserByExtSourceNameAndExtLogin(registrarSession, app.getExtSourceName(), app.getCreatedBy());
} catch (Exception ex) {
// user not found is ok
}
}
if (user != null)
nameText = user.getFirstName();
}
mailText = mailText.replace("{firstName}", nameText);
}
// replace lastName
if (mailText.contains("{lastName}")) {
// backup
String nameText = "";
for (ApplicationFormItemData d : data) {
if ("urn:perun:user:attribute-def:core:lastName".equals(d.getFormItem().getPerunDestinationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
nameText = d.getValue();
break;
}
}
}
if (nameText.isEmpty()) {
User user = null;
if (app.getUser() != null) {
user = app.getUser();
} else {
try {
user = usersManager.getUserByExtSourceNameAndExtLogin(registrarSession, app.getExtSourceName(), app.getCreatedBy());
} catch (Exception ex) {
// user not found is ok
}
}
if (user != null)
nameText = user.getLastName();
}
mailText = mailText.replace("{lastName}", nameText);
}
// replace exceptions
if (mailText.contains("{errors}")) {
String errorText = "";
if (exceptions != null && !exceptions.isEmpty()) {
for (Exception ex : exceptions) {
errorText = errorText.concat("\n\n" + ex.toString());
}
}
mailText = mailText.replace("{errors}", errorText);
}
// replace logins
if (mailText.contains("{login-")) {
Pattern pattern = Pattern.compile("\\{login-[^\\}]+\\}");
Matcher m = pattern.matcher(mailText);
while (m.find()) {
// whole "{login-something}"
String toSubstitute = m.group(0);
// new login value to replace in text
String newValue = "";
Pattern namespacePattern = Pattern.compile("\\-(.*?)\\}");
Matcher m2 = namespacePattern.matcher(toSubstitute);
while (m2.find()) {
// only namespace "meta", "egi-ui",...
String namespace = m2.group(1);
// if user not known -> search through form items to get login
for (ApplicationFormItemData d : data) {
ApplicationFormItem item = d.getFormItem();
if (item != null) {
if (ApplicationFormItem.Type.USERNAME.equals(item.getType())) {
// if username match namespace
if (item.getPerunDestinationAttribute().contains("login-namespace:" + namespace)) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
// save not null or empty value and break cycle
newValue = d.getValue();
break;
}
}
}
}
}
// since we do no allow to overwrite login by application
try {
if (app.getUser() != null) {
List<Attribute> logins = attrManager.getLogins(registrarSession, app.getUser());
for (Attribute a : logins) {
// replace only correct namespace
if (a.getFriendlyNameParameter().equalsIgnoreCase(namespace)) {
if (a.getValue() != null) {
newValue = BeansUtils.attributeValueToString(a);
break;
}
}
}
}
} catch (Exception ex) {
log.error("[MAIL MANAGER] Error thrown when replacing login in namespace \"" + namespace + "\" for mail. {}", ex);
}
}
// substitute {login-namespace} with actual value or empty string
mailText = mailText.replace(toSubstitute, newValue);
}
}
mailText = replaceAppDetailUrl(mailText, app.getId(), app.getVo(), app.getGroup());
mailText = replaceAppGuiUrl(mailText, app.getVo(), app.getGroup());
mailText = replacePerunGuiUrl(mailText, app.getVo(), app.getGroup());
// membership expiration
if (mailText.contains("{membershipExpiration}")) {
String expiration = "";
if (app.getUser() != null) {
try {
User u = usersManager.getUserById(registrarSession, app.getUser().getId());
Member m = membersManager.getMemberByUser(registrarSession, app.getVo(), u);
Attribute a = attrManager.getAttribute(registrarSession, m, URN_MEMBER_EXPIRATION);
if (a != null && a.getValue() != null) {
// attribute value is string
expiration = ((String) a.getValue());
}
} catch (Exception ex) {
log.error("[MAIL MANAGER] Error thrown when getting membership expiration param for mail. {}", ex);
}
}
// replace by date or empty
mailText = mailText.replace("{membershipExpiration}", expiration);
}
// user mail
if (mailText.contains("{mail}")) {
String mail = "";
if (app.getUser() != null) {
try {
User u = usersManager.getUserById(registrarSession, app.getUser().getId());
Attribute a = attrManager.getAttribute(registrarSession, u, URN_USER_PREFERRED_MAIL);
if (a != null && a.getValue() != null) {
// attribute value is string
mail = ((String) a.getValue());
}
} catch (Exception ex) {
log.error("[MAIL MANAGER] Error thrown when getting preferred mail param for mail. {}", ex);
}
} else {
for (ApplicationFormItemData d : data) {
if ("urn:perun:member:attribute-def:def:mail".equals(d.getFormItem().getPerunDestinationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
mail = d.getValue();
break;
}
}
}
for (ApplicationFormItemData d : data) {
if ("urn:perun:user:attribute-def:def:preferredMail".equals(d.getFormItem().getPerunDestinationAttribute())) {
if (d.getValue() != null && !d.getValue().isEmpty()) {
mail = d.getValue();
break;
}
}
}
}
// replace by mail or empty
mailText = mailText.replace("{mail}", mail);
}
// mail footer
if (mailText.contains("{mailFooter}")) {
String footer = "";
// get proper value from attribute
try {
Attribute attribute;
if (app.getGroup() != null) {
attribute = attrManager.getAttribute(registrarSession, app.getGroup(), URN_GROUP_MAIL_FOOTER);
if (attribute == null || attribute.getValue() == null) {
attribute = attrManager.getAttribute(registrarSession, app.getVo(), URN_VO_MAIL_FOOTER);
}
} else {
attribute = attrManager.getAttribute(registrarSession, app.getVo(), URN_VO_MAIL_FOOTER);
}
if (attribute != null && attribute.getValue() != null) {
footer = BeansUtils.attributeValueToString(attribute);
}
} catch (Exception ex) {
// we dont care about exceptions here
log.error("[MAIL MANAGER] Exception thrown when getting VO's footer for email from attribute.", ex);
}
// replace by footer or empty
mailText = mailText.replace("{mailFooter}", (footer != null) ? footer : "");
}
return mailText;
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItemData in project perun by CESNET.
the class RegistrarManagerImpl method storeApplicationAttributes.
/**
* Store values from application data as user/member attributes
*
* New values are set if old are empty, or merged if not empty.
* Empty new values are skipped (not even merged) as well as core attributes.
*
* User and Member must already exists !!
*
* !! LOGIN ATTRIBUTES ARE SKIPPED BY THIS METHOD AND MUST BE
* SET LATER BY storeApplicationLoginAttributes() METHOD !!
* !! USE unreserveNewLoginsFromSameNamespace() BEFORE DOING SO !!
*
* @param app Application to process attributes for
* @throws PerunException
*/
private void storeApplicationAttributes(Application app) throws PerunException {
// user and member must exists if it's extension !!
User user = usersManager.getUserById(registrarSession, app.getUser().getId());
Member member = membersManager.getMemberByUser(registrarSession, app.getVo(), user);
// get all app items
List<ApplicationFormItemData> items = getApplicationDataById(registrarSession, app.getId());
// attributes to set
List<Attribute> attributes = new ArrayList<Attribute>();
for (ApplicationFormItemData item : items) {
String destAttr = item.getFormItem().getPerunDestinationAttribute();
String newValue = item.getValue();
// do not store null or empty values at all
if (newValue == null || newValue.isEmpty())
continue;
// if correct destination attribute
if (destAttr != null && !destAttr.isEmpty()) {
// get attribute (for user and member only)
Attribute a = null;
if (destAttr.contains("urn:perun:user:")) {
a = attrManager.getAttribute(registrarSession, user, destAttr);
} else if (destAttr.contains("urn:perun:member:")) {
a = attrManager.getAttribute(registrarSession, member, destAttr);
} else {
continue;
}
// NEVER STORE LOGINS THIS WAY TO PREVENT ACCIDENTAL OVERWRITE
if (a != null && "login-namespace".equals(a.getBaseFriendlyName())) {
continue;
}
// if attribute exists
if (a != null) {
if (a.getType().equalsIgnoreCase("java.util.LinkedHashMap")) {
// FIXME do not set hash map attributes - not supported in GUI and registrar
continue;
} else if (a.getType().equalsIgnoreCase("java.util.ArrayList") || a.getType().equalsIgnoreCase(BeansUtils.largeArrayListClassName)) {
// we expects that list contains strings
ArrayList<String> value = ((ArrayList<String>) a.getValue());
// if value not present in list => add
if (value == null) {
// set as new value
value = new ArrayList<String>();
value.add(newValue);
} else if (!value.contains(newValue)) {
// add value between old values
value.add(newValue);
}
a.setValue(value);
attributes.add(a);
continue;
} else {
// other attributes are handled like strings
a.setValue(newValue);
attributes.add(a);
}
}
}
}
// set attributes
if (!attributes.isEmpty()) {
// set them if not empty (member+user)
attrManager.setAttributes(registrarSession, member, attributes, true);
}
}
Aggregations