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 java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
27  import org.apache.syncope.client.console.commons.IdRepoConstants;
28  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
29  import org.apache.syncope.client.console.rest.ApplicationRestClient;
30  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
31  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
32  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink.ActionType;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
34  import org.apache.syncope.client.ui.commons.Constants;
35  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
36  import org.apache.syncope.client.ui.commons.panels.ModalPanel;
37  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
38  import org.apache.syncope.common.lib.SyncopeClientException;
39  import org.apache.syncope.common.lib.to.ApplicationTO;
40  import org.apache.syncope.common.lib.to.PrivilegeTO;
41  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
42  import org.apache.wicket.PageReference;
43  import org.apache.wicket.ajax.AjaxRequestTarget;
44  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
45  import org.apache.wicket.event.Broadcast;
46  import org.apache.wicket.event.IEvent;
47  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
48  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
49  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
50  import org.apache.wicket.model.CompoundPropertyModel;
51  import org.apache.wicket.model.IModel;
52  import org.apache.wicket.model.ResourceModel;
53  
54  public class PrivilegeDirectoryPanel extends DirectoryPanel<
55          PrivilegeTO, PrivilegeTO, DirectoryDataProvider<PrivilegeTO>, ApplicationRestClient>
56          implements ModalPanel {
57  
58      private static final long serialVersionUID = 6524374026036584897L;
59  
60      private final ApplicationTO application;
61  
62      private final BaseModal<PrivilegeTO> baseModal;
63  
64      protected PrivilegeDirectoryPanel(
65              final BaseModal<PrivilegeTO> baseModal,
66              final ApplicationRestClient restClient,
67              final ApplicationTO application,
68              final PageReference pageRef) {
69  
70          super(BaseModal.CONTENT_ID, restClient, pageRef, false);
71          this.baseModal = baseModal;
72          this.application = application;
73  
74          disableCheckBoxes();
75          enableUtilityButton();
76  
77          addNewItemPanelBuilder(new PrivilegeWizardBuilder(application, new PrivilegeTO(), restClient, pageRef), true);
78  
79          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.APPLICATION_UPDATE);
80          initResultTable();
81      }
82  
83      @Override
84      protected List<IColumn<PrivilegeTO, String>> getColumns() {
85          final List<IColumn<PrivilegeTO, String>> columns = new ArrayList<>();
86  
87          columns.add(new PropertyColumn<>(
88                  new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
89          columns.add(new PropertyColumn<>(new ResourceModel(Constants.DESCRIPTION_FIELD_NAME),
90                  Constants.DESCRIPTION_FIELD_NAME, Constants.DESCRIPTION_FIELD_NAME));
91  
92          return columns;
93      }
94  
95      @Override
96      protected ActionsPanel<PrivilegeTO> getActions(final IModel<PrivilegeTO> model) {
97          ActionsPanel<PrivilegeTO> panel = super.getActions(model);
98  
99          panel.add(new ActionLink<>() {
100 
101             private static final long serialVersionUID = -3722207913631435501L;
102 
103             @Override
104             public void onClick(final AjaxRequestTarget target, final PrivilegeTO ignore) {
105                 PrivilegeDirectoryPanel.this.getTogglePanel().close(target);
106                 send(PrivilegeDirectoryPanel.this, Broadcast.EXACT,
107                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
108             }
109         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.APPLICATION_UPDATE);
110 
111         panel.add(new ActionLink<>() {
112 
113             private static final long serialVersionUID = -3722207913631435501L;
114 
115             @Override
116             public void onClick(final AjaxRequestTarget target, final PrivilegeTO ignore) {
117                 try {
118                     application.getPrivileges().remove(model.getObject());
119                     restClient.update(application);
120 
121                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
122                     customActionOnFinishCallback(target);
123                 } catch (SyncopeClientException e) {
124                     LOG.error("While deleting {}", model.getObject().getKey(), e);
125                     SyncopeConsoleSession.get().onException(e);
126                 }
127                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
128             }
129         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.APPLICATION_UPDATE, true);
130 
131         return panel;
132     }
133 
134     @Override
135     protected Collection<ActionType> getBatches() {
136         return List.of();
137     }
138 
139     @Override
140     protected PrivilegeDataProvider dataProvider() {
141         return new PrivilegeDataProvider(rows);
142     }
143 
144     @Override
145     protected String paginatorRowsKey() {
146         return IdRepoConstants.PREF_PRIVILEGE_PAGINATOR_ROWS;
147     }
148 
149     protected class PrivilegeDataProvider extends DirectoryDataProvider<PrivilegeTO> {
150 
151         private static final long serialVersionUID = 4725679400450513556L;
152 
153         private final SortableDataProviderComparator<PrivilegeTO> comparator;
154 
155         public PrivilegeDataProvider(final int paginatorRows) {
156             super(paginatorRows);
157 
158             setSort(Constants.DESCRIPTION_FIELD_NAME, SortOrder.ASCENDING);
159             comparator = new SortableDataProviderComparator<>(this);
160         }
161 
162         @Override
163         public Iterator<PrivilegeTO> iterator(final long first, final long count) {
164             List<PrivilegeTO> list = application.getPrivileges();
165             list.sort(comparator);
166             return list.subList((int) first, (int) first + (int) count).iterator();
167         }
168 
169         @Override
170         public long size() {
171             return application.getPrivileges().size();
172         }
173 
174         @Override
175         public IModel<PrivilegeTO> model(final PrivilegeTO object) {
176             return new CompoundPropertyModel<>(object);
177         }
178     }
179 
180     @Override
181     public void onEvent(final IEvent<?> event) {
182         super.onEvent(event);
183         if (event.getPayload() instanceof ExitEvent) {
184             final AjaxRequestTarget target = ExitEvent.class.cast(event.getPayload()).getTarget();
185             baseModal.show(false);
186             baseModal.close(target);
187         }
188     }
189 }