/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.registration;
import java.util.List;
import java.util.Map;
import org.olat.basesecurity.ManagerFactory;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.FormElement;
import org.olat.core.gui.formelements.PasswordElement;
import org.olat.core.gui.formelements.SpacerElement;
import org.olat.core.gui.formelements.StaticSingleSelectionElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
import org.olat.core.util.StringHelper;
import org.olat.core.util.i18n.I18nManager;
import org.olat.user.UserManager;
import org.olat.user.propertyhandlers.UserPropertyHandler;
/**
* Description:
*
* @author Sabina Jeger
*/
public class RegistrationForm2 extends Form {
static final String USERPROPERTIES_FORM_IDENTIFYER = RegistrationForm2.class.getCanonicalName();
private String languageKey;
private List<UserPropertyHandler> userPropertyHandlers;
/**
* @param name
* @param languageKey
*/
public RegistrationForm2(String name, Translator translator, String languageKey) {
super(name, translator);
this.languageKey = languageKey;
init();
}
/**
* Initialize the form
*/
public void init() {
// first the configured user properties
UserManager um = UserManager.getInstance();
userPropertyHandlers = um.getUserPropertyHandlersFor(USERPROPERTIES_FORM_IDENTIFYER, false);
// Add all available user fields to this form
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
if (userPropertyHandler == null) continue;
FormElement ui = userPropertyHandler.getFormElement(getLocale(), null, USERPROPERTIES_FORM_IDENTIFYER, false);
addFormElement(userPropertyHandler.getName(), ui);
}
// second the user language
Map<String, String> languages = I18nManager.getInstance().getEnabledLanguagesTranslated();
StaticSingleSelectionElement singleSelection = new StaticSingleSelectionElement("user.language", StringHelper
.getMapKeysAsStringArray(languages), StringHelper.getMapValuesAsStringArray(languages));
singleSelection.select(languageKey, true);
addFormElement("lang", singleSelection);
// third the login name and password
addFormElement("space_username", new SpacerElement(true, true));
addFormElement("usr_login", new TextElement("user.login", "", 128));
getTextElement("usr_login").setMandatory(true);
addFormElement("pwd1", new PasswordElement("user.password", 255));
getTextElement("pwd1").setMandatory(true);
addFormElement("pwd2", new PasswordElement("user.password2", 255));
getTextElement("pwd2").setMandatory(true);
// last, the submit buttons
addSubmitKey("submit.speichernUndregistrieren","submit.speichernUndregistrieren");
setCancelKey("submit.cancel");
}
/**
* @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
*/
public boolean validate() {
// validate each user field
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
FormElement ui = getFormElement(userPropertyHandler.getName());
if (!userPropertyHandler.isValid(ui, null)) {
return false;
}
}
// validate user login name
TextElement tl = getTextElement("usr_login");
if ( ! UserManager.getInstance().syntaxCheckOlatLogin(tl.getValue())) {
tl.setErrorKey("form.check3");
return false;
}
Identity s = ManagerFactory.getManager().findIdentityByName(tl.getValue());
if (s != null || UserManager.isLoginOnBlacklist(tl.getValue())) {
tl.setErrorKey("form.check6");
return false;
}
// validate given password
if ( ! getTextElement("pwd1").notEmpty("form.check4")) {
return false;
}
if ( ! getTextElement("pwd2").notEmpty("form.check4")) {
return false;
}
if ( ! UserManager.getInstance().syntaxCheckOlatPassword(getTextElement("pwd1").getValue())) {
getTextElement("pwd1").setErrorKey("form.checkregex");
return false;
}
if ( ! getTextElement("pwd1").isEqual(getTextElement("pwd2").getValue(), "form.check5")) {
return false;
}
// all check passed
return true;
}
}