AddEditOsUserWidget.java
/*
** Module : OsUserDefinition.java
** Abstract : OsUserDefinition is a view and controller delegate to add and to edit OS user accounts.
**
** Copyright (c) 2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 GBB 20230807 Created 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.application.home.accounts.osusers;
import com.goldencode.p2j.admin.*;
import com.goldencode.p2j.admin.client.application.home.accounts.*;
import com.goldencode.p2j.admin.client.widget.*;
import com.goldencode.p2j.admin.client.widget.dialog.*;
import com.goldencode.p2j.admin.client.widget.dialog.InputDialog.*;
import com.google.gwt.user.client.ui.*;
import com.google.inject.*;
import org.gwtbootstrap3.client.ui.form.validator.*;
import java.util.*;
import java.util.function.*;
/**
* Implements the add and edit OS user accounts use cases.
*/
public class AddEditOsUserWidget
extends Composite
{
/** The common input dialogs provider */
private final Provider<InputDialog> inputDialogProvider;
/** The edit boolean flag which true value indicates the edit use case.*/
private boolean edit;
/** The common input dialog */
private InputDialog inputDialog;
/** The account name field */
private Field acctName;
/** The account description field */
private Field description;
/** The account disabled field */
private Field acctDisabled;
/** The account security options field */
private Field secSelect;
/** The new password field */
private Field password;
/** The confirmed password field */
private Field password2;
/**
* Creates the User Account Definition delegate.
*
* @param inputDialogProvider
* The injected common input dialogs provider
*/
@Inject
public AddEditOsUserWidget(Provider<InputDialog> inputDialogProvider)
{
this.inputDialogProvider = inputDialogProvider;
}
/**
* Builds the dialog's fields of the OS User Account Definition View.
*
* @param uDef
* The given OS user account definition
* @param doneHandler
* The completion consumer
*/
public void build(OsUserDef uDef, Consumer<Boolean> doneHandler)
{
edit = uDef != null;
inputDialog = inputDialogProvider.get();
buildStandardFields(uDef, items ->
{
if (items != null)
{
inputDialog.setLabelSize(3);
Item[] itemsArray = items.toArray(new Item[0]);
buildStep2(uDef, itemsArray, doneHandler);
}
else
{
doneHandler.accept(false);
}
});
}
/**
* Shows the User Definition View dialog.
*
* @param parent
* The parent container for the User Definition View dialog
* @param uDef
* The OS user account definition
* @param doneHandler
* The consumer of the OS user account model
*/
public void show(HasWidgets parent, OsUserDef uDef, Consumer<OsUserDef> doneHandler)
{
inputDialog.show(parent, false, values ->
{
if (values == null)
{
inputDialog.dismiss();
doneHandler.accept(null);
return;
}
saveUIData(uDef, values);
inputDialog.dismiss();
doneHandler.accept(uDef);
});
}
/**
* Builds the extended OS user account fields.
*
* @param uDef
* The given OS user account definition
* @param items
* The standard OS user account fields
* @param doneHandler
* The completion consumer
*/
private void buildStep2(OsUserDef uDef, Item[] items, Consumer<Boolean> doneHandler)
{
inputDialog.build("OS User Account Definition", items);
if (edit)
{
EnumRadioGroup secSel = (EnumRadioGroup) inputDialog.getWidget(secSelect);
secSel.setEnabled(SecuritySelectorOptions.LEAVE_PASSWORD_UNCHANGED, false);
}
HasEnabled nameW = (HasEnabled) inputDialog.getWidget(acctName);
nameW.setEnabled(!edit);
updateUIState();
bindHandlers();
doneHandler.accept(true);
}
/**
* Builds the standard dialog's fields for the given OS user account definition.
*
* @param uDef
* The given OS user account definition
* @param doneHandler
* The consumer of the dialog's fields
*/
private void buildStandardFields(OsUserDef uDef, Consumer<ArrayList<Item>> doneHandler)
{
ArrayList<Item> res = new ArrayList<>(20);
acctName = new Field("Account Name", uDef == null ? "" : uDef.subjectId);
description = new Field("Description",
uDef == null || uDef.description == null ? "" : uDef.description,
true);
res.add(new Span(acctName, description));
acctDisabled = new Field("Account is Disabled", uDef == null ? false : !uDef.enabled);
res.add(acctDisabled);
secSelect = new Field(null, uDef == null ?
SecuritySelectorOptions.ENTER_PASSWORD :
SecuritySelectorOptions.LEAVE_PASSWORD_UNCHANGED);
res.add(secSelect);
password = new Field("Password", "").password(true);
String msg = "Password values must match";
AbstractValidator<String> pswdVal = new AbstractValidator<String>(msg)
{
@Override
public int getPriority()
{
return 999;
}
@Override
public boolean isValid(String value)
{
HasValue<String> pwd = inputDialog.getWidget(password);
String val1 = pwd.getValue();
if (val1 == null)
{
val1 = "";
}
return !((HasEnabled) pwd).isEnabled() || val1.equals(value);
}
};
password2 = new Field("Confirm Password", "", false, pswdVal).password(true);
Span pswdSpan = new Span(password, password2);
res.add(pswdSpan);
doneHandler.accept(res);
}
/** Attaches the dialog's input handlers */
private void bindHandlers()
{
HasValue<SecuritySelectorOptions> wSec = inputDialog.getWidget(secSelect);
wSec.addValueChangeHandler(event -> updateUIState());
}
/**
* Saves the user's input data into the target user account definition.
*
* @param target
* The target user account definition
* @param values
* The map of the dialog's fields to their values
*/
private void saveUIData(OsUserDef target, Map<Field, Object> values)
{
target.subjectId = (String) values.get(acctName);
target.description = (String) values.get(description);
target.enabled = !(boolean) values.get(acctDisabled);
SecuritySelectorOptions selectedSec = (SecuritySelectorOptions) values.get(secSelect);
target.protect = SecuritySelectorOptions.NO_PASSWORD_PROTECTION != selectedSec;
if (selectedSec == SecuritySelectorOptions.ENTER_PASSWORD)
{
target.plainPassword = (String) values.get(password);
}
}
/**
* Updates the input fields states depending on the custom authentication plugin input and
* the selected password option.
*/
private void updateUIState()
{
HasValue<SecuritySelectorOptions> wSec = inputDialog.getWidget(secSelect);
HasEnabled wPassword = (HasEnabled) inputDialog.getWidget(password);
HasEnabled wPassword2 = (HasEnabled) inputDialog.getWidget(password2);
switch (wSec.getValue())
{
case ENTER_PASSWORD:
wPassword.setEnabled(true);
wPassword2.setEnabled(true);
break;
case LEAVE_PASSWORD_UNCHANGED:
case NO_PASSWORD_PROTECTION:
wPassword.setEnabled(false);
wPassword2.setEnabled(false);
break;
}
}
}