You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@syncope.apache.org by GitBox <gi...@apache.org> on 2022/10/10 09:05:34 UTC

[GitHub] [syncope] github-code-scanning[bot] commented on a diff in pull request #378: [SYNCOPE-1697] Command and Macro introduced

github-code-scanning[bot] commented on code in PR #378:
URL: https://github.com/apache/syncope/pull/378#discussion_r991060625


##########
client/idrepo/console/src/main/java/org/apache/syncope/client/console/wizards/CommandWizardBuilder.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.client.console.wizards;
+
+import java.io.Serializable;
+import org.apache.syncope.client.console.panels.BeanPanel;
+import org.apache.syncope.client.console.rest.CommandRestClient;
+import org.apache.syncope.common.lib.command.CommandTO;
+import org.apache.wicket.PageReference;
+import org.apache.wicket.extensions.wizard.WizardModel;
+import org.apache.wicket.extensions.wizard.WizardStep;
+import org.apache.wicket.model.LoadableDetachableModel;
+
+public class CommandWizardBuilder extends BaseAjaxWizardBuilder<CommandTO> {
+
+    private static final long serialVersionUID = 5288806466136582164L;
+
+    public CommandWizardBuilder(final CommandTO defaultItem, final PageReference pageRef) {
+        super(defaultItem, pageRef);
+    }
+
+    @Override
+    protected Serializable onApplyInternal(final CommandTO modelObject) {
+        return CommandRestClient.run(modelObject).getOutput();
+    }
+
+    @Override
+    protected WizardModel buildModelSteps(final CommandTO modelObject, final WizardModel wizardModel) {
+        wizardModel.add(new CommandArgs(modelObject));
+        return wizardModel;
+    }
+
+    public class CommandArgs extends WizardStep {

Review Comment:
   ## Inner class could be static
   
   CommandArgs could be made static, since the enclosing instance is used only in its constructor.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1180)



##########
common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/CommandService.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.common.rest.api.service;
+
+import io.swagger.v3.oas.annotations.security.SecurityRequirement;
+import io.swagger.v3.oas.annotations.security.SecurityRequirements;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import javax.ws.rs.BeanParam;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import org.apache.syncope.common.lib.command.CommandOutput;
+import org.apache.syncope.common.lib.command.CommandTO;
+import org.apache.syncope.common.lib.to.PagedResult;
+import org.apache.syncope.common.rest.api.RESTHeaders;
+import org.apache.syncope.common.rest.api.beans.CommandQuery;
+
+/**
+ * REST operations for commands.
+ */
+@Tag(name = "Commands")
+@SecurityRequirements({
+    @SecurityRequirement(name = "BasicAuthentication"),
+    @SecurityRequirement(name = "Bearer") })
+@Path("commands")
+public interface CommandService extends JAXRSService {

Review Comment:
   ## Constant interface anti-pattern
   
   Type CommandService implements constant interface [JAXRSService](1).
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1181)



##########
client/idrepo/console/src/main/java/org/apache/syncope/client/console/tasks/CommandComposeDirectoryPanel.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.client.console.tasks;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.syncope.client.console.SyncopeConsoleSession;
+import org.apache.syncope.client.console.commons.IdRepoConstants;
+import org.apache.syncope.client.console.panels.DirectoryPanel;
+import org.apache.syncope.client.console.rest.CommandRestClient;
+import org.apache.syncope.client.console.rest.TaskRestClient;
+import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
+import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
+import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
+import org.apache.syncope.client.ui.commons.Constants;
+import org.apache.syncope.client.ui.commons.DirectoryDataProvider;
+import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
+import org.apache.syncope.client.ui.commons.panels.ModalPanel;
+import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
+import org.apache.syncope.common.lib.SyncopeClientException;
+import org.apache.syncope.common.lib.command.CommandTO;
+import org.apache.syncope.common.lib.to.MacroTaskTO;
+import org.apache.syncope.common.lib.types.IdRepoEntitlement;
+import org.apache.syncope.common.lib.types.TaskType;
+import org.apache.wicket.PageReference;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
+import org.apache.wicket.event.Broadcast;
+import org.apache.wicket.event.IEvent;
+import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
+import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
+import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.repeater.Item;
+import org.apache.wicket.model.CompoundPropertyModel;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.ResourceModel;
+
+public class CommandComposeDirectoryPanel extends DirectoryPanel<
+        CommandWrapper, CommandWrapper, DirectoryDataProvider<CommandWrapper>, CommandRestClient>
+        implements ModalPanel {
+
+    private static final long serialVersionUID = 8899580817658145305L;
+
+    private final BaseModal<MacroTaskTO> baseModal;
+
+    private final String task;
+
+    public CommandComposeDirectoryPanel(
+            final BaseModal<MacroTaskTO> baseModal, final String task, final PageReference pageRef) {
+
+        super(BaseModal.CONTENT_ID, pageRef, false);
+
+        disableCheckBoxes();
+
+        this.baseModal = baseModal;
+        this.task = task;
+
+        enableUtilityButton();
+
+        addNewItemPanelBuilder(new CommandComposeWizardBuilder(task, new CommandWrapper(true), pageRef), true);
+
+        MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.TASK_UPDATE);
+        initResultTable();
+    }
+
+    @Override
+    protected List<IColumn<CommandWrapper, String>> getColumns() {
+        List<IColumn<CommandWrapper, String>> columns = new ArrayList<>();
+
+        columns.add(new AbstractColumn<>(new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME) {
+
+            private static final long serialVersionUID = -4008579357070833846L;
+
+            @Override
+            public void populateItem(
+                    final Item<ICellPopulator<CommandWrapper>> cellItem,
+                    final String componentId,
+                    final IModel<CommandWrapper> rowModel) {
+
+                cellItem.add(new Label(componentId, rowModel.getObject().getCommand().getKey()));
+            }
+        });
+
+        columns.add(new AbstractColumn<>(new ResourceModel("arguments"), "arguments") {
+
+            private static final long serialVersionUID = -4008579357070833846L;
+
+            @Override
+            public void populateItem(
+                    final Item<ICellPopulator<CommandWrapper>> cellItem,
+                    final String componentId,
+                    final IModel<CommandWrapper> rowModel) {
+
+                cellItem.add(new Label(componentId, rowModel.getObject().getCommand().getArgs().getClass().getName()));
+            }
+        });
+
+        return columns;
+    }
+
+    @Override
+    public ActionsPanel<CommandWrapper> getActions(final IModel<CommandWrapper> model) {
+        ActionsPanel<CommandWrapper> panel = super.getActions(model);
+
+        panel.add(new ActionLink<>() {
+
+            private static final long serialVersionUID = -3722207913631435501L;
+
+            @Override
+            public void onClick(final AjaxRequestTarget target, final CommandWrapper ignore) {
+                CommandComposeDirectoryPanel.this.getTogglePanel().close(target);
+                send(CommandComposeDirectoryPanel.this, Broadcast.EXACT,
+                        new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
+            }
+        }, ActionLink.ActionType.EDIT, IdRepoEntitlement.TASK_UPDATE);
+
+        panel.add(new ActionLink<>() {
+
+            private static final long serialVersionUID = -3722207913631435501L;
+
+            @Override
+            public void onClick(final AjaxRequestTarget target, final CommandWrapper ignore) {
+                try {
+                    MacroTaskTO actual = TaskRestClient.readTask(TaskType.MACRO, task);
+                    actual.getCommands().remove(model.getObject().getCommand());
+                    TaskRestClient.update(TaskType.MACRO, actual);
+
+                    SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
+                    customActionOnFinishCallback(target);
+                } catch (SyncopeClientException e) {
+                    LOG.error("While deleting {}", model.getObject(), e);

Review Comment:
   ## Use of default toString()
   
   Default toString(): CommandWrapper inherits toString() from Object, and so is not suitable for printing.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1182)



##########
client/idrepo/console/src/main/java/org/apache/syncope/client/console/tasks/CommandComposeWizardBuilder.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.client.console.tasks;
+
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.syncope.client.console.panels.BeanPanel;
+import org.apache.syncope.client.console.rest.CommandRestClient;
+import org.apache.syncope.client.console.rest.ImplementationRestClient;
+import org.apache.syncope.client.console.rest.TaskRestClient;
+import org.apache.syncope.client.console.wizards.BaseAjaxWizardBuilder;
+import org.apache.syncope.client.ui.commons.Constants;
+import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
+import org.apache.syncope.common.lib.command.CommandTO;
+import org.apache.syncope.common.lib.to.ImplementationTO;
+import org.apache.syncope.common.lib.to.MacroTaskTO;
+import org.apache.syncope.common.lib.types.IdRepoImplementationType;
+import org.apache.syncope.common.lib.types.ImplementationEngine;
+import org.apache.syncope.common.lib.types.TaskType;
+import org.apache.wicket.PageReference;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
+import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
+import org.apache.wicket.extensions.wizard.WizardModel;
+import org.apache.wicket.extensions.wizard.WizardStep;
+import org.apache.wicket.model.LoadableDetachableModel;
+import org.apache.wicket.model.PropertyModel;
+
+public class CommandComposeWizardBuilder extends BaseAjaxWizardBuilder<CommandWrapper> {
+
+    private static final long serialVersionUID = -2300926041782845851L;
+
+    private final LoadableDetachableModel<List<ImplementationTO>> commands = new LoadableDetachableModel<>() {
+
+        private static final long serialVersionUID = 4659376149825914247L;
+
+        @Override
+        protected List<ImplementationTO> load() {
+            return ImplementationRestClient.list(IdRepoImplementationType.COMMAND);
+        }
+    };
+
+    private final String task;
+
+    public CommandComposeWizardBuilder(
+            final String task, final CommandWrapper defaultItem, final PageReference pageRef) {
+
+        super(defaultItem, pageRef);
+        this.task = task;
+    }
+
+    @Override
+    protected Serializable onApplyInternal(final CommandWrapper modelObject) {
+        MacroTaskTO taskTO = TaskRestClient.readTask(TaskType.MACRO, task);
+        if (modelObject.isNew()) {
+            taskTO.getCommands().add(modelObject.getCommand());
+        } else {
+            taskTO.getCommands().stream().
+                    filter(cmd -> cmd.getKey().equals(modelObject.getCommand().getKey())).
+                    findFirst().
+                    ifPresent(cmd -> cmd.setArgs(modelObject.getCommand().getArgs()));
+        }
+
+        TaskRestClient.update(TaskType.MACRO, taskTO);
+        return modelObject;
+    }
+
+    @Override
+    protected WizardModel buildModelSteps(final CommandWrapper modelObject, final WizardModel wizardModel) {
+        wizardModel.add(new Profile(modelObject));
+        wizardModel.add(new CommandArgs(modelObject));
+        return wizardModel;
+    }
+
+    public class Profile extends WizardStep {
+
+        private static final long serialVersionUID = -3043839139187792810L;
+
+        private final CommandWrapper command;
+
+        public Profile(final CommandWrapper command) {
+            this.command = command;
+            MacroTaskTO taskTO = TaskRestClient.readTask(TaskType.MACRO, task);
+
+            AutoCompleteSettings settings = new AutoCompleteSettings();
+            settings.setShowCompleteListOnFocusGain(false);
+            settings.setShowListOnEmptyInput(false);
+
+            AutoCompleteTextField<String> args = new AutoCompleteTextField<>(
+                    "command", new PropertyModel<>(command, "command.key"), settings) {
+
+                private static final long serialVersionUID = -6556576139048844857L;
+
+                @Override
+                protected Iterator<String> getChoices(final String input) {
+                    return commands.getObject().stream().
+                            map(ImplementationTO::getKey).
+                            filter(cmd -> cmd.contains(input)
+                            && taskTO.getCommands().stream().noneMatch(c -> c.getKey().equals(cmd))).
+                            sorted().iterator();
+                }
+            };
+            args.setRequired(true);
+            args.setEnabled(command.isNew());
+            args.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
+
+                private static final long serialVersionUID = -6139318907146065915L;
+
+                @Override
+                protected void onUpdate(final AjaxRequestTarget target) {
+                    CommandTO cmd = CommandRestClient.read(command.getCommand().getKey());
+                    command.getCommand().setArgs(cmd.getArgs());
+                }
+            });
+            add(args);
+        }
+
+        @Override
+        public void applyState() {
+            commands.getObject().stream().
+                    filter(cmd -> cmd.getKey().equals(command.getCommand().getKey())
+                    && cmd.getEngine() == ImplementationEngine.GROOVY).
+                    findFirst().ifPresent(cmd -> getWizardModel().finish());
+        }
+    }
+
+    public class CommandArgs extends WizardStep {

Review Comment:
   ## Inner class could be static
   
   CommandArgs could be made static, since the enclosing instance is used only in its constructor.
   
   [Show more details](https://github.com/apache/syncope/security/code-scanning/1179)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@syncope.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org