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.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import org.apache.syncope.client.console.SyncopeConsoleSession;
27  import org.apache.syncope.client.console.commons.AMConstants;
28  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
29  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
30  import org.apache.syncope.client.console.panels.SAML2SPEntityDirectoryPanel.SAML2SPEntityProvider;
31  import org.apache.syncope.client.console.rest.SAML2SPEntityRestClient;
32  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
34  import org.apache.syncope.client.console.wizards.SAML2SPEntityWizardBuilder;
35  import org.apache.syncope.client.ui.commons.Constants;
36  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
37  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
38  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
39  import org.apache.syncope.common.lib.types.AMEntitlement;
40  import org.apache.wicket.PageReference;
41  import org.apache.wicket.ajax.AjaxRequestTarget;
42  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
43  import org.apache.wicket.event.Broadcast;
44  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
45  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
46  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
47  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
48  import org.apache.wicket.markup.ComponentTag;
49  import org.apache.wicket.markup.html.link.ExternalLink;
50  import org.apache.wicket.markup.repeater.Item;
51  import org.apache.wicket.model.CompoundPropertyModel;
52  import org.apache.wicket.model.IModel;
53  import org.apache.wicket.model.Model;
54  import org.apache.wicket.model.StringResourceModel;
55  
56  public class SAML2SPEntityDirectoryPanel extends DirectoryPanel<
57          SAML2SPEntityTO, SAML2SPEntityTO, SAML2SPEntityProvider, SAML2SPEntityRestClient> {
58  
59      private static final long serialVersionUID = -3890622383545171017L;
60  
61      private final String waPrefix;
62  
63      public SAML2SPEntityDirectoryPanel(
64              final String id,
65              final SAML2SPEntityRestClient restClient,
66              final String waPrefix,
67              final PageReference pageRef) {
68  
69          super(id, restClient, pageRef);
70          this.waPrefix = waPrefix;
71  
72          disableCheckBoxes();
73  
74          modal.size(Modal.Size.Large);
75          modal.addSubmitButton();
76  
77          modal.setWindowClosedCallback(target -> {
78              updateResultTable(target);
79              modal.show(false);
80          });
81  
82          addNewItemPanelBuilder(new SAML2SPEntityWizardBuilder(new SAML2SPEntityTO(), restClient, pageRef), true);
83  
84          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, AMEntitlement.SAML2_SP_ENTITY_SET);
85  
86          initResultTable();
87      }
88  
89      @Override
90      protected List<IColumn<SAML2SPEntityTO, String>> getColumns() {
91          List<IColumn<SAML2SPEntityTO, String>> columns = new ArrayList<>();
92  
93          columns.add(new PropertyColumn<>(
94                  new StringResourceModel(Constants.KEY_FIELD_NAME, this),
95                  Constants.KEY_FIELD_NAME, Constants.KEY_FIELD_NAME));
96  
97          columns.add(new AbstractColumn<>(Model.of("URL")) {
98  
99              private static final long serialVersionUID = -7226955670801277153L;
100 
101             @Override
102             public void populateItem(
103                     final Item<ICellPopulator<SAML2SPEntityTO>> cellItem,
104                     final String componentId,
105                     final IModel<SAML2SPEntityTO> rowModel) {
106 
107                 String metadataURL = waPrefix + "/sp/" + rowModel.getObject().getKey() + "/metadata";
108                 cellItem.add(new ExternalLink(
109                         componentId,
110                         Model.of(metadataURL),
111                         Model.of(metadataURL)) {
112 
113                     private static final long serialVersionUID = -1919646533527005367L;
114 
115                     @Override
116                     protected void onComponentTag(final ComponentTag tag) {
117                         super.onComponentTag(tag);
118 
119                         tag.setName("a");
120                         if (metadataURL.startsWith("http")) {
121                             tag.put("href", getDefaultModelObject().toString());
122                             tag.put("target", "_blank");
123                         }
124                     }
125                 });
126             }
127         });
128 
129         return columns;
130     }
131 
132     @Override
133     protected ActionsPanel<SAML2SPEntityTO> getActions(final IModel<SAML2SPEntityTO> model) {
134         ActionsPanel<SAML2SPEntityTO> panel = super.getActions(model);
135 
136         panel.add(new ActionLink<>() {
137 
138             private static final long serialVersionUID = -3722207913631435501L;
139 
140             @Override
141             public void onClick(final AjaxRequestTarget target, final SAML2SPEntityTO ignore) {
142                 send(SAML2SPEntityDirectoryPanel.this, Broadcast.EXACT,
143                         new AjaxWizard.EditItemActionEvent<>(
144                                 restClient.get(model.getObject().getKey()), target));
145             }
146         }, ActionLink.ActionType.EDIT, AMEntitlement.SAML2_SP_ENTITY_SET);
147 
148         panel.add(new ActionLink<>() {
149 
150             private static final long serialVersionUID = -3722207913631435501L;
151 
152             @Override
153             public void onClick(final AjaxRequestTarget target, final SAML2SPEntityTO ignore) {
154                 try {
155                     restClient.delete(model.getObject().getKey());
156 
157                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
158                     target.add(container);
159                 } catch (Exception e) {
160                     LOG.error("While deleting {}", model.getObject().getKey(), e);
161                     SyncopeConsoleSession.get().onException(e);
162                 }
163                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
164             }
165         }, ActionLink.ActionType.DELETE, AMEntitlement.AUTH_MODULE_DELETE, true);
166 
167         return panel;
168     }
169 
170     @Override
171     protected Collection<ActionLink.ActionType> getBatches() {
172         return List.of();
173     }
174 
175     @Override
176     protected SAML2SPEntityProvider dataProvider() {
177         return new SAML2SPEntityProvider(rows);
178     }
179 
180     @Override
181     protected String paginatorRowsKey() {
182         return AMConstants.PREF_SAML2_SP_ENTITY_PAGINATOR_ROWS;
183     }
184 
185     protected final class SAML2SPEntityProvider extends DirectoryDataProvider<SAML2SPEntityTO> {
186 
187         private static final long serialVersionUID = 5282134321828253058L;
188 
189         private final SortableDataProviderComparator<SAML2SPEntityTO> comparator;
190 
191         public SAML2SPEntityProvider(final int paginatorRows) {
192             super(paginatorRows);
193             comparator = new SortableDataProviderComparator<>(this);
194         }
195 
196         @Override
197         public Iterator<? extends SAML2SPEntityTO> iterator(final long first, final long count) {
198             List<SAML2SPEntityTO> sps = restClient.list();
199             sps.sort(comparator);
200             return sps.subList((int) first, (int) first + (int) count).iterator();
201         }
202 
203         @Override
204         public long size() {
205             return restClient.list().size();
206         }
207 
208         @Override
209         public IModel<SAML2SPEntityTO> model(final SAML2SPEntityTO metadata) {
210             return new CompoundPropertyModel<>(metadata);
211         }
212     }
213 }