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.audit.AuditHistoryModal;
30  import org.apache.syncope.client.console.commons.AMConstants;
31  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
32  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
33  import org.apache.syncope.client.console.pages.BasePage;
34  import org.apache.syncope.client.console.panels.AuthModuleDirectoryPanel.AuthModuleProvider;
35  import org.apache.syncope.client.console.rest.AuditRestClient;
36  import org.apache.syncope.client.console.rest.AuthModuleRestClient;
37  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
38  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
39  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
40  import org.apache.syncope.client.console.wizards.AuthModuleWizardBuilder;
41  import org.apache.syncope.client.ui.commons.Constants;
42  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
43  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
44  import org.apache.syncope.common.lib.to.AuthModuleTO;
45  import org.apache.syncope.common.lib.types.AMEntitlement;
46  import org.apache.syncope.common.lib.types.AuditElements;
47  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
48  import org.apache.wicket.PageReference;
49  import org.apache.wicket.ajax.AjaxRequestTarget;
50  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
51  import org.apache.wicket.event.Broadcast;
52  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
53  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
54  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
55  import org.apache.wicket.markup.html.basic.Label;
56  import org.apache.wicket.markup.repeater.Item;
57  import org.apache.wicket.model.CompoundPropertyModel;
58  import org.apache.wicket.model.IModel;
59  import org.apache.wicket.model.Model;
60  import org.apache.wicket.model.ResourceModel;
61  import org.apache.wicket.model.StringResourceModel;
62  import org.apache.wicket.spring.injection.annot.SpringBean;
63  
64  public class AuthModuleDirectoryPanel
65          extends DirectoryPanel<AuthModuleTO, AuthModuleTO, AuthModuleProvider, AuthModuleRestClient> {
66  
67      private static final long serialVersionUID = 1005345990563741296L;
68  
69      @SpringBean
70      protected AuditRestClient auditRestClient;
71  
72      protected final BaseModal<Serializable> historyModal;
73  
74      public AuthModuleDirectoryPanel(
75              final String id,
76              final AuthModuleRestClient restClient,
77              final PageReference pageRef) {
78  
79          super(id, restClient, pageRef);
80  
81          disableCheckBoxes();
82  
83          addNewItemPanelBuilder(new AuthModuleWizardBuilder(new AuthModuleTO(), restClient, pageRef), true);
84  
85          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, AMEntitlement.AUTH_MODULE_CREATE);
86  
87          modal.size(Modal.Size.Extra_large);
88          initResultTable();
89  
90          historyModal = new BaseModal<>(Constants.OUTER);
91          historyModal.size(Modal.Size.Large);
92          addOuterObject(historyModal);
93      }
94  
95      @Override
96      protected AuthModuleProvider dataProvider() {
97          return new AuthModuleProvider(rows);
98      }
99  
100     @Override
101     protected String paginatorRowsKey() {
102         return AMConstants.PREF_AUTHMODULE_PAGINATOR_ROWS;
103     }
104 
105     @Override
106     protected Collection<ActionLink.ActionType> getBatches() {
107         return List.of();
108     }
109 
110     @Override
111     protected List<IColumn<AuthModuleTO, String>> getColumns() {
112         List<IColumn<AuthModuleTO, String>> columns = new ArrayList<>();
113         columns.add(new PropertyColumn<>(
114                 new StringResourceModel(Constants.KEY_FIELD_NAME, this),
115                 Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
116         columns.add(new PropertyColumn<>(new ResourceModel(Constants.DESCRIPTION_FIELD_NAME),
117                 Constants.DESCRIPTION_FIELD_NAME, Constants.DESCRIPTION_FIELD_NAME));
118         columns.add(new PropertyColumn<>(new ResourceModel("type"), "conf") {
119 
120             private static final long serialVersionUID = -1822504503325964706L;
121 
122             @Override
123             public void populateItem(
124                     final Item<ICellPopulator<AuthModuleTO>> item,
125                     final String componentId,
126                     final IModel<AuthModuleTO> rowModel) {
127 
128                 item.add(new Label(componentId, rowModel.getObject().getConf() == null
129                         ? StringUtils.EMPTY
130                         : StringUtils.substringBefore(
131                                 rowModel.getObject().getConf().getClass().getSimpleName(), "AuthModuleConf")));
132             }
133         });
134         columns.add(new PropertyColumn<>(new ResourceModel("state"), "state", "state"));
135         columns.add(new PropertyColumn<>(new ResourceModel("order"), "order", "order"));
136         return columns;
137     }
138 
139     @Override
140     public ActionsPanel<AuthModuleTO> getActions(final IModel<AuthModuleTO> model) {
141         ActionsPanel<AuthModuleTO> panel = super.getActions(model);
142 
143         panel.add(new ActionLink<>() {
144 
145             private static final long serialVersionUID = -3722207913631435501L;
146 
147             @Override
148             public void onClick(final AjaxRequestTarget target, final AuthModuleTO ignore) {
149                 send(AuthModuleDirectoryPanel.this, Broadcast.EXACT, new AjaxWizard.EditItemActionEvent<>(
150                         restClient.read(model.getObject().getKey()), target));
151             }
152         }, ActionLink.ActionType.EDIT, AMEntitlement.AUTH_MODULE_UPDATE);
153 
154         panel.add(new ActionLink<>() {
155 
156             private static final long serialVersionUID = -5432034353017728756L;
157 
158             @Override
159             public void onClick(final AjaxRequestTarget target, final AuthModuleTO ignore) {
160                 model.setObject(restClient.read(model.getObject().getKey()));
161 
162                 target.add(historyModal.setContent(new AuditHistoryModal<>(
163                         AuditElements.EventCategoryType.LOGIC,
164                         "AuthModuleLogic",
165                         model.getObject(),
166                         AMEntitlement.AUTH_MODULE_UPDATE,
167                         auditRestClient) {
168 
169                     private static final long serialVersionUID = -3712506022627033822L;
170 
171                     @Override
172                     protected void restore(final String json, final AjaxRequestTarget target) {
173                         try {
174                             AuthModuleTO updated = MAPPER.readValue(json, AuthModuleTO.class);
175                             restClient.update(updated);
176 
177                             SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
178                         } catch (Exception e) {
179                             LOG.error("While restoring AuthModule {}", model.getObject().getKey(), e);
180                             SyncopeConsoleSession.get().onException(e);
181                         }
182                         ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
183                     }
184                 }));
185 
186                 historyModal.header(new Model<>(getString("auditHistory.title", new Model<>(model.getObject()))));
187 
188                 historyModal.show(true);
189             }
190         }, ActionLink.ActionType.VIEW_AUDIT_HISTORY, String.format("%s,%s", AMEntitlement.AUTH_MODULE_READ,
191                 IdRepoEntitlement.AUDIT_LIST));
192 
193         panel.add(new ActionLink<>() {
194 
195             private static final long serialVersionUID = -3722207913631435501L;
196 
197             @Override
198             public void onClick(final AjaxRequestTarget target, final AuthModuleTO ignore) {
199                 try {
200                     restClient.delete(model.getObject().getKey());
201 
202                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
203                     target.add(container);
204                 } catch (Exception e) {
205                     LOG.error("While deleting {}", model.getObject().getKey(), e);
206                     SyncopeConsoleSession.get().onException(e);
207                 }
208                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
209             }
210         }, ActionLink.ActionType.DELETE, AMEntitlement.AUTH_MODULE_DELETE, true);
211 
212         return panel;
213     }
214 
215     protected final class AuthModuleProvider extends DirectoryDataProvider<AuthModuleTO> {
216 
217         private static final long serialVersionUID = -185944053385660794L;
218 
219         private final SortableDataProviderComparator<AuthModuleTO> comparator;
220 
221         private AuthModuleProvider(final int paginatorRows) {
222             super(paginatorRows);
223             comparator = new SortableDataProviderComparator<>(this);
224         }
225 
226         @Override
227         public Iterator<AuthModuleTO> iterator(final long first, final long count) {
228             List<AuthModuleTO> authModules = restClient.list();
229             authModules.sort(comparator);
230             return authModules.subList((int) first, (int) first + (int) count).iterator();
231         }
232 
233         @Override
234         public long size() {
235             return restClient.list().size();
236         }
237 
238         @Override
239         public IModel<AuthModuleTO> model(final AuthModuleTO object) {
240             return new CompoundPropertyModel<>(object);
241         }
242     }
243 }