Skip to content

Commit

Permalink
[SYNCOPE-1774] Refactoring Conf Params management in Console
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Aug 18, 2023
1 parent 404eeb6 commit 9a7b8cc
Show file tree
Hide file tree
Showing 23 changed files with 150 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ protected List<IColumn<U2FDevice, String>> getColumns() {
authProfileModal.header(new Model<>(getString("u2fRegisteredDevices", model)));
authProfileModal.show(true);
}
}, ActionLink.ActionType.FO_EDIT, AMEntitlement.AUTH_PROFILE_UPDATE);
}, ActionLink.ActionType.DEPROVISION, AMEntitlement.AUTH_PROFILE_UPDATE);

panel.add(new ActionLink<>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ secretKey=Secret Key
validationCode=Validation Code
scratchCodes=Scratch Codes
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=u2f Devices
deprovision.class=fab fa-usb
deprovision.title=u2f Devices
identifier=Identifier
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ secretKey=Secret Key
validationCode=Validation Code
scratchCodes=Scratch Codes
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=u2f Devices
deprovision.class=fab fa-usb
deprovision.title=u2f Devices
identifier=Identifier
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ secretKey=Chiave Segreta
validationCode=Codice Validazione
scratchCodes=Codici Scratch
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=dispositivi u2f
deprovision.class=fab fa-usb
deprovision.title=dispositivi u2f
identifier=Identificativo
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ secretKey=Secret Key
validationCode=Validation Code
scratchCodes=Scratch Codes
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=u2f Devices
deprovision.class=fab fa-usb
deprovision.title=u2f Devices
identifier=Identifier
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ secretKey=Secret Key
validationCode=Validation Code
scratchCodes=Scratch Codes
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=u2f Devices
deprovision.class=fab fa-usb
deprovision.title=u2f Devices
identifier=Identifier
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ secretKey=Secret Key
validationCode=Validation Code
scratchCodes=Scratch Codes
record=Record
fo_edit.class=fab fa-usb
fo_edit.title=u2f Devices
deprovision.class=fab fa-usb
deprovision.title=u2f Devices
identifier=Identifier
json=JSON
html.class=fas fa-at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public final class AjaxSpinnerFieldPanel<T extends Number> extends FieldPanel<T>

private final Options options;

private final boolean convertValuesToString;

private SpinnerBehavior behavior;

private AjaxSpinnerFieldPanel(
Expand All @@ -57,7 +59,8 @@ private AjaxSpinnerFieldPanel(
final Class<T> reference,
final IModel<T> model,
final Options options,
final boolean enableOnChange) {
final boolean enableOnChange,
final boolean convertValuesToString) {

super(id, name, model);

Expand Down Expand Up @@ -91,6 +94,7 @@ protected void onUpdate(final AjaxRequestTarget target) {
this.model = model;
this.reference = reference;
this.options = options;
this.convertValuesToString = convertValuesToString;
}

@Override
Expand Down Expand Up @@ -125,9 +129,7 @@ public T getObject() {
@Override
public void setObject(final T object) {
list.clear();
if (object != null) {
list.add(object.toString());
}
Optional.ofNullable(object).ifPresent(v -> list.add(convertValuesToString ? v.toString() : v));
}
});

Expand Down Expand Up @@ -221,7 +223,8 @@ public void setObject(final Serializable object) {

@Override
public AjaxSpinnerFieldPanel<T> clone() {
AjaxSpinnerFieldPanel<T> panel = new AjaxSpinnerFieldPanel<>(getId(), name, reference, model, options, false);
AjaxSpinnerFieldPanel<T> panel = new AjaxSpinnerFieldPanel<>(
getId(), name, reference, model, options, false, convertValuesToString);

panel.setRequired(isRequired());
panel.setReadOnly(isReadOnly());
Expand Down Expand Up @@ -251,6 +254,8 @@ public static class Builder<T extends Number> {

private boolean enableOnChange = false;

private boolean convertValuesToString = true;

public Builder<T> min(final T min) {
options.set("min", min);
return this;
Expand All @@ -271,13 +276,19 @@ public Builder<T> enableOnChange() {
return this;
}

public Builder<T> convertValuesToString(final boolean convertValuesToString) {
this.convertValuesToString = convertValuesToString;
return this;
}

public AjaxSpinnerFieldPanel<T> build(
final String id,
final String name,
final Class<T> reference,
final IModel<T> model) {

return new AjaxSpinnerFieldPanel<>(id, name, reference, model, options, enableOnChange);
return new AjaxSpinnerFieldPanel<>(
id, name, reference, model, options, enableOnChange, convertValuesToString);
}
}
}
9 changes: 9 additions & 0 deletions client/idrepo/console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ under the License.
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>

<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,71 @@
*/
package org.apache.syncope.client.console.panels;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.util.Base64;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
import org.apache.syncope.common.lib.to.PlainSchemaTO;
import org.apache.syncope.common.lib.types.AttrSchemaType;
import org.apache.wicket.PageReference;
import org.bouncycastle.util.io.pem.PemReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ParametersModalPanel extends AbstractModalPanel<ConfParam> {

private static final long serialVersionUID = 4024126489500665435L;

private static final JsonMapper JSON_MAPPER = JsonMapper.builder().findAndAddModules().build();

private static final SAXParserFactory SAX_PARSER_FACTORY = SAXParserFactory.newInstance();

private static boolean isDate(final String value) {
try {
DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.parse(value);
return true;
} catch (ParseException pe) {
return false;
}
}

private static boolean isJSON(final String value) {
try {
JSON_MAPPER.readTree(value);
return true;
} catch (JsonProcessingException jpe) {
return false;
}
}

private static boolean isXML(final String value) {
try {
SAX_PARSER_FACTORY.newSAXParser().getXMLReader().parse(new InputSource(new StringReader(value)));
return true;
} catch (IOException | ParserConfigurationException | SAXException xmle) {
return false;
}
}

private static boolean isPEM(final String value) {
try {
PemReader reader = new PemReader(new StringReader(value));
return reader.readPemObject() != null;
} catch (IOException e) {
return false;
}
}

private final ParametersWizardPanel.ParametersForm form;

public ParametersModalPanel(
Expand All @@ -42,21 +95,48 @@ public ParametersModalPanel(
super(modal, pageRef);

PlainSchemaTO schema = new PlainSchemaTO();
schema.setMultivalue(param.isMultivalue());
schema.setMimeType(MediaType.APPLICATION_OCTET_STREAM);
if (param.getSchema() != null) {
if (param.isInstance(Boolean.class)) {
schema.setType(AttrSchemaType.Boolean);
} else if (param.isInstance(Integer.class) || param.isInstance(Long.class)) {
schema.setType(AttrSchemaType.Long);
} else if (param.isInstance(Float.class) || param.isInstance(Double.class)) {
schema.setType(AttrSchemaType.Double);
} else {
schema.setType(AttrSchemaType.String);
} else // attempt to guess type from content: otherwise, it's bare String
if (!param.getValues().isEmpty()) {
// 1. is it Date?
if (isDate(param.getValues().get(0).toString())) {
schema.setType(AttrSchemaType.Date);
} else // 2. does it look like Base64?
if (org.apache.commons.codec.binary.Base64.isBase64(param.getValues().get(0).toString())) {
String value = new String(Base64.getDecoder().decode(param.getValues().get(0).toString()));

// 3. is it JSON?
if (isJSON(value)) {
schema.setType(AttrSchemaType.Binary);
schema.setMimeType(MediaType.APPLICATION_JSON);
} else // 4. is it XML?
if (isXML(value)) {
schema.setType(AttrSchemaType.Binary);
schema.setMimeType(MediaType.APPLICATION_XML);
} else // 5. is it PEM?
if (isPEM(value)) {
schema.setType(AttrSchemaType.Binary);
schema.setMimeType("application/x-pem-file");
}
}
}
schema.setMultivalue(param.isMultivalue());
schema.setMimeType(MediaType.APPLICATION_OCTET_STREAM);
}
form = new ParametersWizardPanel.ParametersForm(schema, param);

if (schema.getType() == AttrSchemaType.Binary) {
modal.size(Modal.Size.Extra_large);
} else {
modal.size(Modal.Size.Default);
}

form = new ParametersWizardPanel.ParametersForm(schema, param);
add(new ParametersWizardPanel(form, confParamOps, pageRef).build("parametersCreateWizardPanel", mode));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.syncope.client.console.panels;

import jakarta.ws.rs.core.MediaType;
import java.util.List;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
Expand Down Expand Up @@ -130,30 +129,31 @@ public Boolean getObject(

case Long:
panel = new AjaxSpinnerFieldPanel.Builder<Long>().
convertValuesToString(false).
build(id, valueHeaderName, Long.class, new Model<>());
break;

case Double:
panel = new AjaxSpinnerFieldPanel.Builder<Double>().
convertValuesToString(false).
build(id, valueHeaderName, Double.class, new Model<>());
break;

case Binary:
panel = new BinaryFieldPanel(id, valueHeaderName, new Model<>(),
MediaType.APPLICATION_OCTET_STREAM, schema.getModelObject());
plainSchemaTO.getMimeType(), schema.getModelObject());
break;

default:
panel = new AjaxTextFieldPanel(id, valueHeaderName, new Model<>(), false);
}

if (plainSchemaTO.isMultivalue()) {
return new MultiFieldPanel.Builder<>(
new PropertyModel<>(param, "values")).build(id, valueHeaderName, panel);
} else {
panel.setNewModel(param.getValues());
}

panel.setRequired("true".equalsIgnoreCase(plainSchemaTO.getMandatoryCondition()));
panel.setNewModel(param.getValues());
return panel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ParametersWizardPanel(

@Override
protected WizardModel buildModelSteps(final ParametersForm modelObject, final WizardModel wizardModel) {
wizardModel.add(new ParametersWizardSchemaStep(modelObject));
wizardModel.add(new ParametersWizardSchemaStep(mode, modelObject));
wizardModel.add(new ParametersWizardAttrStep(mode, modelObject));
return wizardModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
import org.apache.syncope.common.lib.types.AttrSchemaType;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.WebMarkupContainer;
Expand All @@ -30,19 +31,25 @@ public class ParametersWizardSchemaStep extends WizardStep {

private static final long serialVersionUID = -7843275202297616553L;

public ParametersWizardSchemaStep(final ParametersWizardPanel.ParametersForm modelObject) {
modelObject.getSchema().setMandatoryCondition("false");
public ParametersWizardSchemaStep(
final AjaxWizard.Mode mode,
final ParametersWizardPanel.ParametersForm modelObject) {

setOutputMarkupId(true);

WebMarkupContainer content = new WebMarkupContainer("content");
this.setOutputMarkupId(true);
content.setOutputMarkupId(true);
add(content);
add(content.setOutputMarkupId(true));

AjaxDropDownChoicePanel<AttrSchemaType> type = new AjaxDropDownChoicePanel<>(
"type", getString("type"), new PropertyModel<>(modelObject.getSchema(), "type"));
type.setReadOnly(mode != AjaxWizard.Mode.CREATE);
type.setChoices(List.of(
AttrSchemaType.String, AttrSchemaType.Long, AttrSchemaType.Double,
AttrSchemaType.Boolean, AttrSchemaType.Date, AttrSchemaType.Binary));
AttrSchemaType.String,
AttrSchemaType.Long,
AttrSchemaType.Double,
AttrSchemaType.Boolean,
AttrSchemaType.Date,
AttrSchemaType.Binary));
content.add(type);

content.add(new AjaxCheckBoxPanel("multivalue", getString("multivalue"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void onSubmit(final AjaxRequestTarget target) {
};

add(jsonPanel);

}

private static String getJSONInfo(final PropagationTaskTO taskTO) {
Expand Down
Loading

0 comments on commit 9a7b8cc

Please sign in to comment.