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 UserRequestFormsWidget) {
113                 ((UserRequestFormsWidget) widget).refreshLatestAlerts(target);
114             }
115 
116             manageFormModal.show(false);
117         });
118 
119         initResultTable();
120 
121         MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
122     }
123 
124     @Override
125     protected List<IColumn<UserRequestForm, String>> getColumns() {
126         List<IColumn<UserRequestForm, String>> columns = new ArrayList<>();
127 
128         columns.add(new PropertyColumn<>(
129                 new ResourceModel("bpmnProcess"), "bpmnProcess", "bpmnProcess"));
130         columns.add(new PropertyColumn<>(
131                 new ResourceModel("key"), "formKey", "formKey"));
132         columns.add(new PropertyColumn<>(
133                 new ResourceModel("username"), "username"));
134         columns.add(new DatePropertyColumn<>(
135                 new ResourceModel("createTime"), "createTime", "createTime"));
136         columns.add(new DatePropertyColumn<>(
137                 new ResourceModel("dueDate"), "dueDate", "dueDate"));
138         columns.add(new PropertyColumn<>(
139                 new ResourceModel("assignee"), "assignee", "assignee"));
140 
141         return columns;
142     }
143 
144     @Override
145     protected ActionLinksTogglePanel<UserRequestForm> actionTogglePanel() {
146         return new ActionLinksTogglePanel<>(Constants.OUTER, pageRef) {
147 
148             private static final long serialVersionUID = -7688359318035249200L;
149 
150             @Override
151             public void updateHeader(final AjaxRequestTarget target, final Serializable object) {
152                 if (object instanceof UserRequestForm) {
153                     setHeader(target, StringUtils.abbreviate(
154                             ((UserRequestForm) object).getUsername(), HEADER_FIRST_ABBREVIATION));
155                 } else {
156                     super.updateHeader(target, object);
157                 }
158             }
159         };
160     }
161 
162     @Override
163     public ActionsPanel<UserRequestForm> getActions(final IModel<UserRequestForm> model) {
164         final ActionsPanel<UserRequestForm> panel = super.getActions(model);
165 
166         panel.add(new ActionLink<>() {
167 
168             private static final long serialVersionUID = -3722207913631435501L;
169 
170             @Override
171             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
172                 try {
173                     restClient.claimForm(model.getObject().getTaskId());
174                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
175                     target.add(container);
176                 } catch (Exception e) {
177                     SyncopeConsoleSession.get().onException(e);
178                 }
179                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
180             }
181 
182         }, ActionLink.ActionType.CLAIM, FlowableEntitlement.USER_REQUEST_FORM_CLAIM);
183 
184         panel.add(new ActionLink<>() {
185 
186             private static final long serialVersionUID = -4496313424398213416L;
187 
188             @Override
189             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
190                 try {
191                     restClient.unclaimForm(model.getObject().getTaskId());
192                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
193                     UserRequestFormDirectoryPanel.this.getTogglePanel().close(target);
194                     target.add(container);
195                 } catch (Exception e) {
196                     SyncopeConsoleSession.get().onException(e);
197                 }
198                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
199             }
200 
201             @Override
202             protected boolean statusCondition(final UserRequestForm modelObject) {
203                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
204                         equals(model.getObject().getAssignee());
205             }
206 
207         }, ActionLink.ActionType.UNCLAIM, FlowableEntitlement.USER_REQUEST_FORM_UNCLAIM);
208 
209         panel.add(new ActionLink<>() {
210 
211             private static final long serialVersionUID = -3722207913631435501L;
212 
213             @Override
214             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
215                 manageFormModal.setFormModel(new CompoundPropertyModel<>(model.getObject()));
216 
217                 target.add(manageFormModal.setContent(new UserRequestFormModal(manageFormModal, pageRef, model.
218                         getObject()) {
219 
220                     private static final long serialVersionUID = 5546519445061007248L;
221 
222                     @Override
223                     public void onSubmit(final AjaxRequestTarget target) {
224                         try {
225                             super.onSubmit(target);
226 
227                             UserRequestFormDirectoryPanel.this.getTogglePanel().close(target);
228                         } catch (SyncopeClientException e) {
229                             SyncopeConsoleSession.get().onException(e);
230                         }
231                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
232                     }
233                 }));
234 
235                 manageFormModal.header(new Model<>(getString("form.manage", new Model<>(model.getObject()))));
236                 manageFormModal.show(true);
237             }
238 
239             @Override
240             protected boolean statusCondition(final UserRequestForm modelObject) {
241                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
242                         equals(model.getObject().getAssignee());
243             }
244 
245         }, ActionLink.ActionType.MANAGE_APPROVAL, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
246 
247         // SYNCOPE-1200 edit user while in approval state
248         panel.add(new ActionLink<>() {
249 
250             private static final long serialVersionUID = -3722207913631435501L;
251 
252             @Override
253             public void onClick(final AjaxRequestTarget target, final UserRequestForm ignore) {
254                 modal.setFormModel(new CompoundPropertyModel<>(model.getObject()));
255 
256                 UserRequestForm formTO = model.getObject();
257                 UserTO newUserTO;
258                 if (formTO.getUserUR() == null) {
259                     newUserTO = formTO.getUserTO();
260                     if (newUserTO != null) {
261                         // SYNCOPE-1563 do not use the password into formTO.getUserTO()
262                         newUserTO.setPassword(null);
263                     }
264                 } else if (formTO.getUserTO() == null) {
265                     // make it stronger by handling possible NPE
266                     UserTO previousUserTO = new UserTO();
267                     previousUserTO.setKey(formTO.getUserUR().getKey());
268                     newUserTO = AnyOperations.patch(previousUserTO, formTO.getUserUR());
269                 } else {
270                     formTO.getUserTO().setKey(formTO.getUserUR().getKey());
271                     formTO.getUserTO().setPassword(null);
272                     newUserTO = AnyOperations.patch(formTO.getUserTO(), formTO.getUserUR());
273                 }
274 
275                 AjaxWizard.EditItemActionEvent<UserTO> editItemActionEvent =
276                         new AjaxWizard.EditItemActionEvent<>(newUserTO, target);
277                 editItemActionEvent.forceModalPanel(AnyLayoutUtils.newLayoutInfo(newUserTO,
278                         anyTypeRestClient.read(AnyTypeKind.USER.name()).getClasses(),
279                         AnyLayoutUtils.fetch(roleRestClient, List.of(AnyTypeKind.USER.name())).getUser(),
280                         userRestClient,
281                         pageRef).
282                         build(BaseModal.CONTENT_ID, 0, AjaxWizard.Mode.EDIT_APPROVAL));
283 
284                 send(UserRequestFormDirectoryPanel.this, Broadcast.EXACT, editItemActionEvent);
285             }
286 
287             @Override
288             protected boolean statusCondition(final UserRequestForm modelObject) {
289                 return SyncopeConsoleSession.get().getSelfTO().getUsername().
290                         equals(model.getObject().getAssignee());
291             }
292 
293         }, ActionLink.ActionType.EDIT_APPROVAL, FlowableEntitlement.USER_REQUEST_FORM_SUBMIT);
294 
295         return panel;
296     }
297 
298     @Override
299     protected UserRequestFormProvider dataProvider() {
300         return new UserRequestFormProvider(rows);
301     }
302 
303     @Override
304     protected String paginatorRowsKey() {
305         return PREF_USER_REQUEST_FORM_PAGINATOR_ROWS;
306     }
307 
308     @Override
309     protected Collection<ActionLink.ActionType> getBatches() {
310         return List.of();
311     }
312 
313     @Override
314     public void onEvent(final IEvent<?> event) {
315         if (event.getPayload() instanceof UserRequestSearchEvent) {
316             UserRequestSearchEvent payload = UserRequestSearchEvent.class.cast(event.getPayload());
317             keyword = payload.getKeyword();
318 
319             updateResultTable(payload.getTarget());
320         } else {
321             super.onEvent(event);
322         }
323     }
324 
325     protected final class UserRequestFormProvider extends DirectoryDataProvider<UserRequestForm> {
326 
327         private static final long serialVersionUID = -2311716167583335852L;
328 
329         public UserRequestFormProvider(final int paginatorRows) {
330             super(paginatorRows);
331 
332             setSort("createTime", SortOrder.ASCENDING);
333         }
334 
335         @Override
336         public Iterator<UserRequestForm> iterator(final long first, final long count) {
337             int page = ((int) first / paginatorRows);
338             return restClient.listForms(
339                     keyword, (page < 0 ? 0 : page) + 1, paginatorRows, getSort()).iterator();
340         }
341 
342         @Override
343         public long size() {
344             return restClient.countForms(keyword);
345         }
346 
347         @Override
348         public IModel<UserRequestForm> model(final UserRequestForm form) {
349             return Model.of(form);
350         }
351     }
352 }