use of cz.metacentrum.perun.registrar.model.ApplicationFormItem 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;
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class RegistrarManagerImpl method createApplicationInternal.
@Override
@Transactional(rollbackFor = ApplicationNotCreatedException.class)
public Application createApplicationInternal(PerunSession session, Application application, List<ApplicationFormItemData> data) throws PerunException {
// exceptions to send to vo admin with new app created email
List<Exception> exceptions = new ArrayList<Exception>();
boolean applicationNotCreated = false;
try {
// 1) create application
int appId = Utils.getNewId(jdbc, "APPLICATION_ID_SEQ");
application.setId(appId);
application.setState(AppState.NEW);
// optional group
Integer groupId = null;
Integer userId = null;
if (application.getGroup() != null) {
groupId = application.getGroup().getId();
}
if (application.getUser() != null) {
userId = application.getUser().getId();
}
jdbc.update("insert into application(id,vo_id,group_id,user_id,apptype,fed_info,extSourceName,extSourceType,extSourceLoa,state,created_by,modified_by) values (?,?,?,?,?,?,?,?,?,?,?,?)", appId, application.getVo().getId(), groupId, userId, application.getType().toString(), application.getFedInfo(), application.getExtSourceName(), application.getExtSourceType(), application.getExtSourceLoa(), application.getState().toString(), application.getCreatedBy(), application.getCreatedBy());
// 2) process & store app data
for (ApplicationFormItemData itemData : data) {
Type itemType = itemData.getFormItem().getType();
if (itemType == HTML_COMMENT || itemType == SUBMIT_BUTTON || itemType == AUTO_SUBMIT_BUTTON || itemType == PASSWORD || itemType == HEADING)
continue;
// Check if mails needs to be validated
if (itemType == VALIDATED_EMAIL) {
// default = mail not same as pre-filled
itemData.setAssuranceLevel("");
// We must use contains, because IdP can send more than one email, emails are separated by semi-colon
if (itemData.getPrefilledValue() != null && itemData.getValue() != null && !itemData.getValue().isEmpty()) {
if (itemData.getPrefilledValue().toLowerCase().contains(itemData.getValue().toLowerCase())) {
itemData.setAssuranceLevel("1");
}
}
// it's save, empty attributes are not set to DB nor any notification is sent
if (!itemData.getFormItem().isRequired() && (itemData.getValue() == null || itemData.getValue().isEmpty())) {
itemData.setAssuranceLevel("1");
}
}
try {
itemData.setId(Utils.getNewId(jdbc, "APPLICATION_DATA_ID_SEQ"));
jdbc.update("insert into application_data(id,app_id,item_id,shortname,value,assurance_level) values (?,?,?,?,?,?)", itemData.getId(), appId, itemData.getFormItem().getId(), itemData.getFormItem().getShortname(), itemData.getValue(), itemData.getAssuranceLevel());
} catch (Exception ex) {
// log and store exception so vo manager could see error in notification.
log.error("[REGISTRAR] Storing form item {} caused exception {}", itemData, ex);
exceptions.add(ex);
}
}
// 3) process all logins and passwords
// create list of logins and passwords to process
List<ApplicationFormItemData> logins = new ArrayList<ApplicationFormItemData>();
for (ApplicationFormItemData itemData : data) {
Type itemType = itemData.getFormItem().getType();
if (itemType == USERNAME || itemType == PASSWORD) {
// skip unchanged pre-filled logins, since they must have been handled last time
if (itemData.getValue().equals(itemData.getPrefilledValue()) && itemType != PASSWORD)
continue;
logins.add(itemData);
}
}
for (ApplicationFormItemData loginItem : logins) {
if (loginItem.getFormItem().getType() == USERNAME) {
// values to store
String login = loginItem.getValue();
// filled later
String pass = "";
// Get login namespace
String dstAttr = loginItem.getFormItem().getPerunDestinationAttribute();
AttributeDefinition loginAttribute = attrManager.getAttributeDefinition(registrarSession, dstAttr);
String loginNamespace = loginAttribute.getFriendlyNameParameter();
// try to book new login in namespace if the application hasn't been approved yet
if (perun.getUsersManagerBl().isLoginAvailable(registrarSession, loginNamespace, login)) {
try {
// Reserve login
jdbc.update("insert into application_reserved_logins(login,namespace,app_id,created_by,created_at) values(?,?,?,?,?)", login, loginNamespace, appId, application.getCreatedBy(), new Date());
log.debug("[REGISTRAR] Added login reservation for login: {} in namespace: {}.", login, loginNamespace);
// process password for this login
for (ApplicationFormItemData passItem : logins) {
ApplicationFormItem item = passItem.getFormItem();
if (item.getType() == PASSWORD && item.getPerunDestinationAttribute() != null) {
if (item.getPerunDestinationAttribute().equals(dstAttr)) {
pass = passItem.getValue();
try {
// reserve password
perun.getUsersManagerBl().reservePassword(registrarSession, login, loginNamespace, pass);
log.debug("[REGISTRAR] Password for login: {} in namespace: {} successfully reserved in external system.", login, loginNamespace);
} catch (Exception ex) {
// login reservation fail must cause rollback !!
log.error("[REGISTRAR] Unable to reserve password for login: {} in namespace: {} in external system. Exception: " + ex, login, loginNamespace);
throw new ApplicationNotCreatedException("Application was not created. Reason: Unable to reserve password for login: " + login + " in namespace: " + loginNamespace + " in external system. Please contact support to fix this issue before new application submission.", login, loginNamespace);
}
// use first pass with correct namespace
break;
}
}
}
} catch (ApplicationNotCreatedException ex) {
// re-throw
throw ex;
} catch (Exception ex) {
// unable to book login
log.error("[REGISTRAR] Unable to reserve login: {} in namespace: {}. Exception: " + ex, login, loginNamespace);
exceptions.add(ex);
}
} else {
// login is not available
log.error("[REGISTRAR] Login: " + login + " in namespace: " + loginNamespace + " is already occupied but it shouldn't (race condition).");
exceptions.add(new InternalErrorException("Login: " + login + " in namespace: " + loginNamespace + " is already occupied but it shouldn't."));
}
}
}
// call registrar module before auto validation so createAction is trigerred first
RegistrarModule module;
if (application.getGroup() != null) {
module = getRegistrarModule(getFormForGroup(application.getGroup()));
} else {
module = getRegistrarModule(getFormForVo(application.getVo()));
}
if (module != null) {
module.createApplication(session, application, data);
}
} catch (ApplicationNotCreatedException ex) {
// prevent action in finally block
applicationNotCreated = true;
// re-throw
throw ex;
} catch (Exception ex) {
// any exception during app creation process => add it to list
// exceptions when handling logins are catched before
log.error("{}", ex);
exceptions.add(ex);
} finally {
// process rest only if it was not exception related to PASSWORDS creation
if (!applicationNotCreated) {
getMailManager().sendMessage(application, MailType.APP_CREATED_USER, null, null);
getMailManager().sendMessage(application, MailType.APP_CREATED_VO_ADMIN, null, exceptions);
// if there were exceptions, throw some to let know GUI about it
if (!exceptions.isEmpty()) {
RegistrarException ex = new RegistrarException("Your application (ID=" + application.getId() + ") has been created with errors. Administrator of " + application.getVo().getName() + " has been notified. If you want, you can use \"Send report to RT\" button to send this information to administrators directly.");
log.error("[REGISTRAR] New application {} created with errors {}. This is case of PerunException {}", new Object[] { application, exceptions, ex.getErrorId() });
throw ex;
}
log.info("New application {} created.", application);
perun.getAuditer().log(session, "New {} created.", application);
}
}
// return stored data
return application;
}
use of cz.metacentrum.perun.registrar.model.ApplicationFormItem in project perun by CESNET.
the class RegistrarManagerImpl method copyFormFromVoToVo.
@Override
public void copyFormFromVoToVo(PerunSession sess, Vo fromVo, Vo toVo) throws PerunException {
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromVo) || !AuthzResolver.isAuthorized(sess, Role.VOADMIN, toVo)) {
throw new PrivilegeException(sess, "copyFormFromVoToVo");
}
List<ApplicationFormItem> items = getFormItems(sess, getFormForVo(fromVo));
for (ApplicationFormItem item : items) {
// reset order, id is always new inside add method
item.setOrdnum(null);
addFormItem(sess, getFormForVo(toVo), item);
}
}
Aggregations