View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.panels;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.console.SyncopeConsoleSession;
29  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
30  import org.apache.syncope.client.console.layout.AnyLayoutUtils;
31  import org.apache.syncope.client.console.pages.BasePage;
32  import org.apache.syncope.client.console.panels.UserRequestFormDirectoryPanel.UserRequestFormProvider;
33  import org.apache.syncope.client.console.panels.UserRequestsPanel.UserRequestSearchEvent;
34  import org.apache.syncope.client.console.rest.AnyTypeRestClient;
35  import org.apache.syncope.client.console.rest.RoleRestClient;
36  import org.apache.syncope.client.console.rest.UserRequestRestClient;
37  import org.apache.syncope.client.console.rest.UserRestClient;
38  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.DatePropertyColumn;
39  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
40  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
41  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLinksTogglePanel;
42  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
43  import org.apache.syncope.client.console.widgets.UserRequestFormsWidget;
44  import org.apache.syncope.client.ui.commons.Constants;
45  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
46  import org.apache.syncope.common.lib.AnyOperations;
47  import org.apache.syncope.common.lib.SyncopeClientException;
48  import org.apache.syncope.common.lib.to.UserRequestForm;
49  import org.apache.syncope.common.lib.to.UserTO;
50  import org.apache.syncope.common.lib.types.AnyTypeKind;
51  import org.apache.syncope.common.lib.types.FlowableEntitlement;
52  import org.apache.wicket.PageReference;
53  import org.apache.wicket.ajax.AjaxRequestTarget;
54  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
55  import org.apache.wicket.event.Broadcast;
56  import org.apache.wicket.event.IEvent;
57  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
58  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
59  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
60  import org.apache.wicket.model.CompoundPropertyModel;
61  import org.apache.wicket.model.IModel;
62  import org.apache.wicket.model.Model;
63  import org.apache.wicket.model.ResourceModel;
64  import org.apache.wicket.spring.injection.annot.SpringBean;
65  
66  public class UserRequestFormDirectoryPanel
67          extends DirectoryPanel<UserRequestForm, UserRequestForm, UserRequestFormProvider, UserRequestRestClient> {
68  
69      private static final long serialVersionUID = -7122136682275797903L;
70  
71      protected static final String PREF_USER_REQUEST_FORM_PAGINATOR_ROWS = "userrequestform.paginator.rows";
72  
73      @SpringBean
74      protected AnyTypeRestClient anyTypeRestClient;
75  
76      @SpringBean
77      protected RoleRestClient roleRestClient;
78  
79      @SpringBean
80      protected UserRestClient userRestClient;
81  
82      protected final BaseModal<UserRequestForm> manageFormModal = new BaseModal<>("outer") {
83  
84          private static final long serialVersionUID = 389935548143327858L;
85  
86          @Override
87          protected void onConfigure() {
88              super.onConfigure();
89              addSubmitButton();
90              size(Modal.Size.Large);
91          }
92      };
93  
94      protected String keyword;
95  
96      public UserRequestFormDirectoryPanel(
97              final String id,
98              final UserRequestRestClient restClient,
99              final PageReference pageRef) {
100 
101         super(id, restClient, pageRef, true);
102         disableCheckBoxes();
103         setFooterVisibility(false);
104         modal.size(Modal.Size.Large);
105 
106         addOuterObject(manageFormModal);
107 
108         manageFormModal.setWindowClosedCallback(target -> {
109             updateResultTable(target);
110 
111             Serializable widget = SyncopeConsoleSession.get().getAttribute(UserRequestFormsWidget.class.getName());
112             if (widget instanceof final UserRequestFormsWidget urfw) {
113                 urfw.refresh(target);
114             }
115 
116             if (actionTogglePanel.isVisibleInHierarchy()) {
117                 actionTogglePanel.toggle(target, false);
118             }
119 
120             manageFormModal.show(false);
121         });
122 
123         initResultTable();
124 
125         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
126     }
127 
128     @Override
129     protected List<IColumn<UserRequestForm, String>> getColumns() {
130         List<IColumn<UserRequestForm, String>> columns = new ArrayList<>();
131 
132         columns.add(new PropertyColumn<>(
133                 new ResourceModel("bpmnProcess"), "bpmnProcess", "bpmnProcess"));
134         columns.add(new PropertyColumn<>(
135                 new ResourceModel("key"), "formKey", "formKey"));
136         columns.add(new PropertyColumn<>(
137                 new ResourceModel("username"), "username"));
138         columns.add(new DatePropertyColumn<>(
139                 new ResourceModel("createTime"), "createTime", "createTime"));
140         columns.add(new DatePropertyColumn<>(
141                 new ResourceModel("dueDate"), "dueDate", "dueDate"));
142         columns.add(new PropertyColumn<>(
143                 new ResourceModel("assignee"), "assignee", "assignee"));
144 
145         return columns;
146     }
147 
148     @Override
149     protected ActionLinksTogglePanel<UserRequestForm> actionTogglePanel() {
150         return new ActionLinksTogglePanel<>(Constants.OUTER, pageRef) {
151 
152             private static final long serialVersionUID = -7688359318035249200L;
153 
154             @Override
155             public void updateHeader(final AjaxRequestTarget target, final Serializable object) {
156                 if (object instanceof final UserRequestForm userRequestForm) {
157                     setHeader(target, StringUtils.abbreviate(
158                             userRequestForm.getUsername(), HEADER_FIRST_ABBREVIATION));
159                 } else {
160                     super.updateHeader(target, object);
161                 }
162             }
163         };
164     }
165 
166     @Override
167     public ActionsPanel<UserRequestForm> getActions(final IModel<UserRequestForm> model) {
168         final ActionsPanel<UserRequestForm> panel = super.getActions(model);
169 
170         panel.add(new ActionLink<>() {
171 
172             private static final long serialVersionUID = -3722207913631435501L;
173 
174             @Override
175             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
176                 try {
177                     restClient.claimForm(model.getObject().getTaskId());
178                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
179                     target.add(container);
180                 } catch (Exception e) {
181                     SyncopeConsoleSession.get().onException(e);
182                 }
183                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
184             }
185 
186         }, ActionLink.ActionType.CLAIM, FlowableEntitlement.USER_REQUEST_FORM_CLAIM);
187 
188         panel.add(new ActionLink<>() {
189 
190             private static final long serialVersionUID = -4496313424398213416L;
191 
192             @Override
193             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
194                 try {
195                     restClient.unclaimForm(model.getObject().getTaskId());
196                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
197                     UserRequestFormDirectoryPanel.this.getTogglePanel().close(target);
198                     target.add(container);
199                 } catch (Exception e) {
200                     SyncopeConsoleSession.get().onException(e);
201                 }
202                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
203             }
204 
205             @Override
206             protected boolean statusCondition(final UserRequestForm modelObject) {
207                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
208                         equals(model.getObject().getAssignee());
209             }
210 
211         }, ActionLink.ActionType.UNCLAIM, FlowableEntitlement.USER_REQUEST_FORM_UNCLAIM);
212 
213         panel.add(new ActionLink<>() {
214 
215             private static final long serialVersionUID = -3722207913631435501L;
216 
217             @Override
218             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
219                 manageFormModal.setFormModel(new CompoundPropertyModel<>(model.getObject()));
220 
221                 target.add(manageFormModal.setContent(
222                         new UserRequestFormModal(manageFormModal, pageRef, model.getObject()) {
223 
224                     private static final long serialVersionUID = 5546519445061007248L;
225 
226                     @Override
227                     public void onSubmit(final AjaxRequestTarget target) {
228                         try {
229                             super.onSubmit(target);
230 
231                             UserRequestFormDirectoryPanel.this.getTogglePanel().close(target);
232                         } catch (SyncopeClientException e) {
233                             SyncopeConsoleSession.get().onException(e);
234                         }
235                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
236                     }
237                 }));
238 
239                 manageFormModal.header(new Model<>(getString("form.manage", new Model<>(model.getObject()))));
240                 manageFormModal.show(true);
241             }
242 
243             @Override
244             protected boolean statusCondition(final UserRequestForm modelObject) {
245                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
246                         equals(model.getObject().getAssignee());
247             }
248 
249         }, ActionLink.ActionType.MANAGE_APPROVAL, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
250 
251         // SYNCOPE-1200 edit user while in approval state
252         panel.add(new ActionLink<>() {
253 
254             private static final long serialVersionUID = -3722207913631435501L;
255 
256             @Override
257             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
258                 modal.setFormModel(new CompoundPropertyModel<>(model.getObject()));
259 
260                 UserRequestForm formTO = model.getObject();
261                 UserTO newUserTO;
262                 if (formTO.getUserUR() == null) {
263                     newUserTO = formTO.getUserTO();
264                     if (newUserTO != null) {
265                         // SYNCOPE-1563 do not use the password into formTO.getUserTO()
266                         newUserTO.setPassword(null);
267                     }
268                 } else if (formTO.getUserTO() == null) {
269                     // make it stronger by handling possible NPE
270                     UserTO previousUserTO = new UserTO();
271                     previousUserTO.setKey(formTO.getUserUR().getKey());
272                     newUserTO = AnyOperations.patch(previousUserTO, formTO.getUserUR());
273                 } else {
274                     formTO.getUserTO().setKey(formTO.getUserUR().getKey());
275                     formTO.getUserTO().setPassword(null);
276                     newUserTO = AnyOperations.patch(formTO.getUserTO(), formTO.getUserUR());
277                 }
278 
279                 AjaxWizard.EditItemActionEvent<UserTO> editItemActionEvent =
280                         new AjaxWizard.EditItemActionEvent<>(newUserTO, target);
281                 editItemActionEvent.forceModalPanel(AnyLayoutUtils.newLayoutInfo(newUserTO,
282                         anyTypeRestClient.read(AnyTypeKind.USER.name()).getClasses(),
283                         AnyLayoutUtils.fetch(roleRestClient, List.of(AnyTypeKind.USER.name())).getUser(),
284                         userRestClient,
285                         pageRef).
286                         build(BaseModal.CONTENT_ID, 0, AjaxWizard.Mode.EDIT_APPROVAL));
287 
288                 send(UserRequestFormDirectoryPanel.this, Broadcast.EXACT, editItemActionEvent);
289             }
290 
291             @Override
292             protected boolean statusCondition(final UserRequestForm modelObject) {
293                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
294                         equals(model.getObject().getAssignee());
295             }
296 
297         }, ActionLink.ActionType.EDIT_APPROVAL, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
298 
299         return panel;
300     }
301 
302     @Override
303     protected UserRequestFormProvider dataProvider() {
304         return new UserRequestFormProvider(rows);
305     }
306 
307     @Override
308     protected String paginatorRowsKey() {
309         return PREF_USER_REQUEST_FORM_PAGINATOR_ROWS;
310     }
311 
312     @Override
313     protected Collection<ActionLink.ActionType> getBatches() {
314         return List.of();
315     }
316 
317     @Override
318     public void onEvent(final IEvent<?> event) {
319         if (event.getPayload() instanceof final UserRequestSearchEvent payload) {
320             keyword = payload.getKeyword();
321 
322             updateResultTable(payload.getTarget());
323         } else {
324             super.onEvent(event);
325         }
326     }
327 
328     protected final class UserRequestFormProvider extends DirectoryDataProvider<UserRequestForm> {
329 
330         private static final long serialVersionUID = -2311716167583335852L;
331 
332         public UserRequestFormProvider(final int paginatorRows) {
333             super(paginatorRows);
334 
335             setSort("createTime", SortOrder.ASCENDING);
336         }
337 
338         @Override
339         public Iterator<UserRequestForm> iterator(final long first, final long count) {
340             int page = ((int) first / paginatorRows);
341             return restClient.listForms(
342                     keyword, (page < 0 ? 0 : page) + 1, paginatorRows, getSort()).iterator();
343         }
344 
345         @Override
346         public long size() {
347             return restClient.countForms(keyword);
348         }
349 
350         @Override
351         public IModel<UserRequestForm> model(final UserRequestForm form) {
352             return Model.of(form);
353         }
354     }
355 }