/**
* 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.admin.user;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.formelements.SpacerElement;
import org.olat.core.gui.formelements.StaticSingleSelectionElement;
import org.olat.core.gui.formelements.VisibilityDependsOnSelectionRule;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
/**
* Initial Date: Jan 27, 2006
* @author gnaegi
* <pre>
* Description:
* Form to configure the users system roles and the basic type
* of the user account (user or guest).
*
* This form is a bit tricky in its usage. All form elements are created in the
* constructor, however they are not yet added to the form. Then each form element
* must be set to the initial value using the setter methods. Every setter method
* has a flag to disable the form element. If this flag is set to true, the form
* element will be set to null. After calling all setters the finishInitialization
* method must be called. This method adds all form elements that have not-null values
* to the form.
* When using the getter methods to retrieve the values make sure you call the getter
* only on form elements that are not null. Otherwhise a nullpointer will be thrown.
*/
public class SystemRolesAndRightsForm extends Form {
// form elements
private StaticSingleSelectionElement isAnonymous;
private CheckBoxElement isGroupmanager;
private CheckBoxElement isUsermanager;
private CheckBoxElement isAuthor;
private CheckBoxElement isAdmin;
private CheckBoxElement isInstitutionalResourceManager;
private StaticSingleSelectionElement statusSelectionElement;
public SystemRolesAndRightsForm(String name, Translator translator, String institution) {
super(name, translator);
// 1) general accout type
String[] isAnonymousKeys = {"true", "false"};
String[] isAnonymousValues = {translate("rightsForm.isAnonymous.true"), translate("rightsForm.isAnonymous.false")};
isAnonymous = new StaticSingleSelectionElement("rightsForm.isAnonymous", isAnonymousKeys, isAnonymousValues);
// 2) system roles
isUsermanager = new CheckBoxElement("rightsForm.isUsermanager");
isGroupmanager = new CheckBoxElement("rightsForm.isGroupmanager");
isAuthor = new CheckBoxElement("rightsForm.isAuthor");
isAdmin = new CheckBoxElement("rightsForm.isAdmin");
// TODO allow arguments for label to inform user about the institution which will be affected by this option
// if(institution != null) {
// isInstitutionalResourceManager = new CheckBoxElement("rightsForm.isInstitutionalResourceManager.institution");
// isInstitutionalResourceManager.setLabelKeyArgs(new String[] {institution});
// } else {
isInstitutionalResourceManager = new CheckBoxElement("rightsForm.isInstitutionalResourceManager");
// }
// 3) other security config
statusSelectionElement = createStatusSelectionElement();
// now use setXXX methods and then finishInitialization
}
private StaticSingleSelectionElement createStatusSelectionElement() {
String[] statusKeys = { Integer.toString(Identity.STATUS_ACTIV), Integer.toString(Identity.STATUS_PERMANENT), Integer.toString(Identity.STATUS_LOGIN_DENIED) };
String[] statusValues = { translate("rightsForm.status.activ"), translate("rightsForm.status.permanent"), translate("rightsForm.status.login_denied") };
return new StaticSingleSelectionElement("rightsForm.status", statusKeys, statusValues);
}
public void finishInitialization() {
VisibilityDependsOnSelectionRule disableWhenGuestRule;
SpacerElement spacer = new SpacerElement(true, false);
if (isAnonymous != null) {
addFormElement("isAnonymous", isAnonymous);
addFormElement("spacer1", spacer);
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, spacer, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
if (isUsermanager != null) {
addFormElement("isUsermanager", isUsermanager);
if (isAnonymous != null) {
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, isUsermanager, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
}
if (isGroupmanager != null) {
addFormElement("isGroupmanager", isGroupmanager);
if (isAnonymous != null) {
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, isGroupmanager, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
}
if (isAuthor != null){
addFormElement("isAuthor", isAuthor);
if (isAnonymous != null) {
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, isAuthor, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
}
if (isAdmin != null){
addFormElement("isAdmin", isAdmin);
if (isAnonymous != null) {
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, isAdmin, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
}
if (isInstitutionalResourceManager!= null) {
addFormElement("isInstitutionalResourceManager", isInstitutionalResourceManager);
if (isAnonymous != null) {
disableWhenGuestRule = new VisibilityDependsOnSelectionRule(isAnonymous, isInstitutionalResourceManager, "true", false, "", true);
addVisibilityDependsOnSelectionRule(disableWhenGuestRule);
}
}
addFormElement("status", statusSelectionElement);
addSubmitKey("save");
setCancelButton();
}
public boolean validate() {
if (isAnonymous != null && isAnonymous.getSelectedKey().equals("true")) {
// if user is selected as as anonymous guest, all system roles must be set to false
if (isUsermanager != null && isUsermanager.isChecked()) {
isUsermanager.setErrorKey("rightForm.error.anonymous.no.roles");
return false;
} else if( isGroupmanager != null && isGroupmanager.isChecked()){
isGroupmanager.setErrorKey("rightForm.error.anonymous.no.roles");
return false;
} else if (isInstitutionalResourceManager != null && isInstitutionalResourceManager.isChecked()) {
isInstitutionalResourceManager.setErrorKey("rightForm.error.anonymous.no.roles");
return false;
} else if (isAuthor != null && isAuthor.isChecked()) {
isAuthor.setErrorKey("rightForm.error.anonymous.no.roles");
return false;
} else if (isAdmin != null && isAdmin.isChecked()) {
isAdmin.setErrorKey("rightForm.error.anonymous.no.roles");
return false;
}
}
return true;
}
public boolean isAdmin() {
return isAdmin.isChecked();
}
public void setAdmin(boolean isAdmin, boolean dontUse) {
if (dontUse)
this.isAdmin = null;
else
this.isAdmin.setChecked(isAdmin);
}
public boolean isAnonymous() {
return Boolean.valueOf(isAnonymous.getSelectedKey()).booleanValue();
}
public void setAnonymous(boolean isAnonymous, boolean dontUse) {
if (dontUse)
this.isAnonymous = null;
else {
this.isAnonymous.select("true", isAnonymous);
this.isAnonymous.select("false", !isAnonymous);
}
}
public boolean isAuthor() {
return isAuthor.isChecked();
}
public void setAuthor(boolean isAuthor, boolean dontUse) {
if (dontUse)
this.isAuthor = null;
else
this.isAuthor.setChecked(isAuthor);
}
public boolean isGroupmanager() {
return isGroupmanager.isChecked();
}
public void setGroupmanager(boolean isGroupmanager, boolean dontUse) {
if (dontUse)
this.isGroupmanager = null;
else
this.isGroupmanager.setChecked(isGroupmanager);
}
public boolean isUsermanager() {
return isUsermanager.isChecked();
}
public void setUsermanager(boolean isUsermanager, boolean dontUse) {
if (dontUse)
this.isUsermanager = null;
else
this.isUsermanager.setChecked(isUsermanager);
}
public void setInstitutionalResourceManager(boolean isInstitutionalResourceManager, boolean dontUse) {
if (dontUse)
this.isInstitutionalResourceManager = null;
else
this.isInstitutionalResourceManager.setChecked(isInstitutionalResourceManager);
}
public boolean isInstitutionalResourceManager() {
return isInstitutionalResourceManager==null?false:isInstitutionalResourceManager.isChecked();
}
public Integer getStatus() {
return new Integer(statusSelectionElement.getSelectedKey());
}
public void setStatus(Integer status) {
if (status == Identity.STATUS_DELETED) {
String[] statusKeys = { Identity.STATUS_DELETED.toString() };
String[] statusValues = {translate("rightsForm.status.deleted")};
statusSelectionElement = new StaticSingleSelectionElement("rightsForm.status", statusKeys, statusValues);
statusSelectionElement.setReadOnly(true);
} else {
statusSelectionElement = createStatusSelectionElement();
}
statusSelectionElement.select( status.toString(), true );
}
}