use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.
the class NewUserForm method initForm.
@Override
protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
setFormTitle("title.newuser");
setFormDescription("new.form.please.enter");
setFormContextHelp("User management");
formLayout.setElementCssClass("o_sel_id_create");
usernameTextElement = uifactory.addTextElement(LOGINNAME, "username", 128, "", formLayout);
usernameTextElement.setMandatory(true);
usernameTextElement.setDisplaySize(30);
usernameTextElement.setElementCssClass("o_sel_id_username");
UserManager um = UserManager.getInstance();
userPropertyHandlers = um.getUserPropertyHandlersFor(formIdentifyer, true);
// Add all available user fields to this form
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
if (userPropertyHandler == null)
continue;
FormItem formItem = userPropertyHandler.addFormItem(ureq.getLocale(), null, formIdentifyer, true, formLayout);
// special case to handle email field
if (userPropertyHandler.getName().equals(UserConstants.EMAIL)) {
emailTextElement = (TextElement) formItem;
if (!userModule.isEmailMandatory()) {
formItem.setMandatory(false);
}
}
formItem.setElementCssClass("o_sel_id_" + userPropertyHandler.getName().toLowerCase());
}
Map<String, String> languages = I18nManager.getInstance().getEnabledLanguagesTranslated();
String[] langKeys = StringHelper.getMapKeysAsStringArray(languages);
String[] langValues = StringHelper.getMapValuesAsStringArray(languages);
ArrayHelper.sort(langKeys, langValues, false, true, false);
// Build css classes for reference languages
languageSingleSelection = uifactory.addDropdownSingleselect("new.form.language", formLayout, langKeys, langValues, null);
// select default language in form
languageSingleSelection.select(I18nModule.getDefaultLocale().toString(), true);
// add password fields!!!
if (showPasswordFields) {
uifactory.addStaticTextElement("heading2", null, translate("new.form.please.enter.pwd"), formLayout);
// checkBox: generate user with OLAT authentication or not
String[] authKeys = { "xx" };
String[] authValues = { translate("new.form.auth.true") };
authCheckbox = uifactory.addCheckboxesHorizontal("new.form.auth", formLayout, authKeys, authValues);
authCheckbox.select("xx", showPasswordFields);
authCheckbox.addActionListener(FormEvent.ONCLICK);
// if OLAT authentication is used, use the pwd below
psw1TextElement = uifactory.addPasswordElement(PASSWORD_NEW1, "new.form.password.new1", 255, "", formLayout);
psw1TextElement.setMandatory(true);
psw1TextElement.setDisplaySize(30);
psw1TextElement.setVisible(showPasswordFields);
psw1TextElement.setElementCssClass("o_sel_id_password1");
psw1TextElement.setAutocomplete("new-password");
psw2TextElement = uifactory.addPasswordElement(PASSWORD_NEW2, "new.form.password.new2", 255, "", formLayout);
psw2TextElement.setMandatory(true);
psw2TextElement.setDisplaySize(30);
psw2TextElement.setVisible(showPasswordFields);
psw2TextElement.setElementCssClass("o_sel_id_password2");
psw2TextElement.setAutocomplete("new-password");
}
uifactory.addFormSubmitButton("save", "submit.save", formLayout);
}
use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.
the class NewUserForm method doCreateAndPersistIdentity.
private Identity doCreateAndPersistIdentity() {
String lang = languageSingleSelection.getSelectedKey();
String username = usernameTextElement.getValue();
String pwd = null;
// use password only when configured to do so
if (showPasswordFields && authCheckbox.isSelected(0)) {
pwd = psw1TextElement.getValue();
if (!StringHelper.containsNonWhitespace(pwd)) {
// treat white-space passwords as no-password. This is fine, a password can be set later on
pwd = null;
}
}
// Create new user and identity and put user to users group
// Create transient user without firstName,lastName, email
UserManager um = UserManager.getInstance();
User newUser = um.createUser(null, null, null);
// Now add data from user fields (firstName,lastName and email are mandatory)
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
FormItem propertyItem = this.flc.getFormComponent(userPropertyHandler.getName());
userPropertyHandler.updateUserFromFormItem(newUser, propertyItem);
}
// Init preferences
newUser.getPreferences().setLanguage(lang);
newUser.getPreferences().setInformSessionTimeout(true);
// Save everything in database
Identity ident = securityManager.createAndPersistIdentityAndUserWithDefaultProviderAndUserGroup(username, null, pwd, newUser);
return ident;
}
use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.
the class UserSearchFlexiController method validateForm.
private boolean validateForm(UserRequest ureq) {
// override for sys admins
if (ureq.getUserSession() != null && ureq.getUserSession().getRoles() != null && ureq.getUserSession().getRoles().isOLATAdmin()) {
return true;
}
boolean filled = !loginEl.isEmpty();
StringBuilder full = new StringBuilder(loginEl.getValue().trim());
FormItem lastFormElement = loginEl;
// "this e-mail exists already"
for (UserPropertyHandler userPropertyHandler : userSearchFormPropertyHandlers) {
FormItem ui = propFormItems.get(userPropertyHandler.getName());
String uiValue = userPropertyHandler.getStringValue(ui);
// add value for later non-empty search check
if (StringHelper.containsNonWhitespace(uiValue)) {
full.append(uiValue.trim());
filled = true;
} else {
// its an empty field
filled = filled || false;
}
lastFormElement = ui;
}
// Don't allow searches with * or % or @ chars only (wild cards). We don't want
// users to get a complete list of all OLAT users this easily.
String fullString = full.toString();
boolean onlyStar = fullString.matches("^[\\*\\s@\\%]*$");
if (!filled || onlyStar) {
// set the error message
lastFormElement.setErrorKey("error.search.form.notempty", null);
return false;
}
if (fullString.contains("**")) {
lastFormElement.setErrorKey("error.search.form.no.wildcard.dublicates", null);
return false;
}
if (fullString.length() < 4) {
lastFormElement.setErrorKey("error.search.form.to.short", null);
return false;
}
return true;
}
use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.
the class UserSearchFlexiController method doSearch.
public void doSearch() {
String login = loginEl.getValue();
// build user fields search map
Map<String, String> userPropertiesSearch = new HashMap<>();
for (UserPropertyHandler userPropertyHandler : userSearchFormPropertyHandlers) {
if (userPropertyHandler == null)
continue;
FormItem ui = propFormItems.get(userPropertyHandler.getName());
String uiValue = userPropertyHandler.getStringValue(ui);
if (userPropertyHandler.getName().startsWith("genericCheckboxProperty")) {
if (!"false".equals(uiValue)) {
userPropertiesSearch.put(userPropertyHandler.getName(), uiValue);
}
} else if (StringHelper.containsNonWhitespace(uiValue)) {
userPropertiesSearch.put(userPropertyHandler.getName(), uiValue);
}
}
if (userPropertiesSearch.isEmpty()) {
userPropertiesSearch = null;
}
tableEl.reset();
List<Identity> users = searchUsers(login, userPropertiesSearch, true);
if (!users.isEmpty()) {
userTableModel.setObjects(users);
flc.contextPut("showButton", "true");
} else {
getWindowControl().setInfo(translate("error.no.user.found"));
}
}
use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.
the class UserSearchForm method validateForm.
private boolean validateForm(UserRequest ureq) {
// override for sys admins
if (ureq.getUserSession() != null && ureq.getUserSession().getRoles() != null && ureq.getUserSession().getRoles().isOLATAdmin()) {
return true;
}
boolean filled = !login.isEmpty();
StringBuilder full = new StringBuilder(login.getValue().trim());
FormItem lastFormElement = login;
// "this e-mail exists already"
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
FormItem ui = propFormItems.get(userPropertyHandler.getName());
String uiValue = userPropertyHandler.getStringValue(ui);
// add value for later non-empty search check
if (StringHelper.containsNonWhitespace(uiValue)) {
full.append(uiValue.trim());
filled = true;
} else {
// its an empty field
filled = filled || false;
}
lastFormElement = ui;
}
// Don't allow searches with * or % or @ chars only (wild cards). We don't want
// users to get a complete list of all OLAT users this easily.
String fullString = full.toString();
boolean onlyStar = fullString.matches("^[\\*\\s@\\%]*$");
if (!filled || onlyStar) {
// set the error message
lastFormElement.setErrorKey("error.search.form.notempty", null);
return false;
}
if (fullString.contains("**")) {
lastFormElement.setErrorKey("error.search.form.no.wildcard.dublicates", null);
return false;
}
int MIN_LENGTH = 4;
if (fullString.length() < MIN_LENGTH) {
lastFormElement.setErrorKey("error.search.form.to.short", null);
return false;
}
return true;
}
Aggregations