DirectoryRightsEditor.java
/*
** Module : DirectoryRightsEditor.java
** Abstract : Directory rights editor.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------Description-----------------------------------
** 001 HC 20170612 Initial version.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.admin.client.editors;
import com.goldencode.p2j.admin.client.widget.dialog.*;
import com.goldencode.p2j.admin.client.widget.dialog.InputDialog.*;
import com.goldencode.p2j.directory.*;
import com.goldencode.p2j.security.*;
import com.goldencode.p2j.security.BitSet;
import com.goldencode.p2j.security.Description;
import com.google.gwt.user.client.ui.*;
import org.gwtbootstrap3.client.ui.CheckBox;
import org.gwtbootstrap3.extras.select.client.ui.*;
import java.util.*;
import java.util.function.*;
/**
* Directory rights editor.
*/
public class DirectoryRightsEditor
implements RightsEditor
{
/** Description array */
private Description[] description;
/**
* Returns the compatible resource type name.
*
* @return Resource type name.
*/
public static String getResourceType()
{
return "directory";
}
/**
* Gives chance to editors to initialize their data structures.
* <p>
* This is a one time call for this kind of resource for the life time
* of the admin session.
*
* @param description
* rights structure description
*/
@Override
public void initialize(Description[] description)
{
this.description = description;
}
/**
* Creates an instance of the <code>Rights</code>, which does not take
* any input and is considered a default starting point in editing.
*
* @return an instance of <code>Rights</code>
*/
@Override
public Rights createDefaultRights()
{
return new DirectoryRights();
}
/**
* Edits the given instance of the <code>Rights</code>.
* The implementation should prepare its modal dialog using the initial state
* from the passed instance of the <code>Rights</code>, show the dialog,
* provide event processing until the Save or Cancel action is applied, then
* hide the dialog and return the appropriate result.
*
* @param name
* name of the resource instance being edited
* @param exact
* nature of the name
* @param rights
* instance of <code>Rights</code> to be edited
* @param submitHandler
* Submit handler.
* @param cancelHandler
* Cancel handler.
*/
@Override
public void edit(String name,
boolean exact,
Rights rights,
Consumer<Rights> submitHandler,
Runnable cancelHandler)
{
String condition = ((DirectoryRights)rights).getCondition();
boolean extra = condition != null;
String[] bitNames = description[0].getBitNames();
BitSet perms = ((DirectoryRights)rights).getPermissions();
boolean denied = perms.isSet(DirectoryRights.BIT_DENIED_ACCESS);
RightsEditorContext ctxt = RightsEditorContext.instance();
InputDialog dlg = ctxt.getInputDialog();
// 1st row - Enumerate permission
Field fEnum = new Field("E (" + bitNames[0] + ")",
perms.isSet(DirectoryRights.BIT_ENUMERATE_ACCESS) && !denied)
.withTooltip("allows enumerating objects");
// 2nd row - Read permission
Field fRead = new Field("R (" + bitNames[1] + ")",
perms.isSet(DirectoryRights.BIT_READ_ACCESS) && !denied)
.withTooltip("allows reading attribute values");
// 3rd row - Write permission
Field fWrite = new Field("W (" + bitNames[2] + ")",
perms.isSet(DirectoryRights.BIT_WRITE_ACCESS) && !denied)
.withTooltip("allows writing attribute values");
// 4th row - Add permission
Field fAdd = new Field("A (" + bitNames[3] + ")",
perms.isSet(DirectoryRights.BIT_ADD_ACCESS) && !denied)
.withTooltip("allows adding attribute values");
// 5th row - Add permission
Field fCreate = new Field("C (" + bitNames[4] + ")",
perms.isSet(DirectoryRights.BIT_CREATE_ACCESS) && !denied)
.withTooltip("allows creating objects and attributes");
// 6th row - Delete permission
Field fDelete = new Field("D (" + bitNames[5] + ")",
perms.isSet(DirectoryRights.BIT_DELETE_ACCESS) && !denied)
.withTooltip("allows deleting objects and attributes");
// 7th row - Denied access
Field fDenied = new Field("N (" + bitNames[6] + ")",
perms.isSet(DirectoryRights.BIT_DENIED_ACCESS) && !denied)
.withTooltip("denies any and all accesses");
// extra
Field fExtra = new Field("Enable extra condition", extra);
String logicValue = perms.isSet(DirectoryRights.BIT_LOGIC) ? "AND" : "OR";
Field fAndOr = new SelectField("", Arrays.asList("AND", "OR"), logicValue);
// condition
Field fCond = new Field("Condition", condition == null ? String.class : condition)
.withTooltip(description[1].getDescription());
Item[] fields = new Item[]
{
fEnum, fRead, fWrite, fAdd, fCreate, fDelete, fDenied, fExtra, fAndOr, fCond
};
dlg.setLabelSize(2);
dlg.build("Directory Rights", fields);
CheckBox extraBox = (CheckBox) dlg.getWidget(fExtra);
SelectBase andOr = (SelectBase) dlg.getWidget(fAndOr);
HasEnabled cond = (HasEnabled) dlg.getWidget(fCond);
andOr.setEnabled(extra);
cond.setEnabled(extra);
extraBox.addValueChangeHandler(event ->
{
boolean val = event.getValue();
andOr.setEnabled(val);
cond.setEnabled(val);
});
CheckBox deniedBox = (CheckBox) dlg.getWidget(fDenied);
CheckBox enumBox = (CheckBox) dlg.getWidget(fEnum);
CheckBox readBox = (CheckBox) dlg.getWidget(fRead);
CheckBox writeBox = (CheckBox) dlg.getWidget(fWrite);
CheckBox addBox = (CheckBox) dlg.getWidget(fAdd);
CheckBox createBox = (CheckBox) dlg.getWidget(fCreate);
CheckBox deleteBox = (CheckBox) dlg.getWidget(fDelete);
Consumer<Boolean> updateBoxes = val ->
{
enumBox.setEnabled(val);
readBox.setEnabled(val);
writeBox.setEnabled(val);
addBox.setEnabled(val);
createBox.setEnabled(val);
deleteBox.setEnabled(val);
};
updateBoxes.accept(!denied);
deniedBox.addValueChangeHandler(event ->
{
boolean val = event.getValue();
updateBoxes.accept(!val);
});
dlg.show(ctxt.getParent(), vals ->
{
if (vals != null)
{
boolean enumVal = (Boolean) vals.get(fEnum);
boolean readVal = (Boolean) vals.get(fRead);
boolean writeVal = (Boolean) vals.get(fWrite);
boolean addVal = (Boolean) vals.get(fAdd);
boolean createVal = (Boolean) vals.get(fCreate);
boolean deleteVal = (Boolean) vals.get(fDelete);
boolean deniedVal = (Boolean) vals.get(fDenied);
perms.set(DirectoryRights.BIT_ENUMERATE_ACCESS, enumVal && !deniedVal);
perms.set(DirectoryRights.BIT_READ_ACCESS, readVal && !deniedVal);
perms.set(DirectoryRights.BIT_WRITE_ACCESS, writeVal && !deniedVal);
perms.set(DirectoryRights.BIT_ADD_ACCESS, addVal && !deniedVal);
perms.set(DirectoryRights.BIT_CREATE_ACCESS, createVal && !deniedVal);
perms.set(DirectoryRights.BIT_DELETE_ACCESS, deleteVal && !deniedVal);
perms.set(DirectoryRights.BIT_DENIED_ACCESS, deniedVal);
boolean extraVal = (boolean) vals.get(fExtra);
String condVal = null;
String andOrVal = (String) vals.get(fAndOr);
if (extraVal)
{
condVal = (String) vals.get(fCond);
perms.set(DirectoryRights.BIT_LOGIC, andOrVal == "AND");
}
else
{
perms.unset(DirectoryRights.BIT_LOGIC);
}
submitHandler.accept(new DirectoryRights(perms, condVal));
}
else
{
cancelHandler.run();
}
});
}
}