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.authprofiles;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.stream.Collectors;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
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.DirectoryPanel;
31  import org.apache.syncope.client.console.rest.AuthProfileRestClient;
32  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
33  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
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.BaseBean;
39  import org.apache.syncope.common.lib.to.AuthProfileTO;
40  import org.apache.syncope.common.lib.types.AMEntitlement;
41  import org.apache.syncope.common.lib.wa.GoogleMfaAuthAccount;
42  import org.apache.wicket.PageReference;
43  import org.apache.wicket.ajax.AjaxRequestTarget;
44  import org.apache.wicket.event.Broadcast;
45  import org.apache.wicket.event.IEvent;
46  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
47  import org.apache.wicket.model.CompoundPropertyModel;
48  import org.apache.wicket.model.IModel;
49  import org.springframework.beans.BeanWrapper;
50  import org.springframework.beans.PropertyAccessorFactory;
51  
52  public abstract class AuthProfileItemDirectoryPanel<I extends BaseBean>
53          extends DirectoryPanel<I, I, AuthProfileItemDirectoryPanel<I>.AuthProfileItemProvider, AuthProfileRestClient> {
54  
55      private static final long serialVersionUID = 7640851594812655896L;
56  
57      protected final BaseModal<AuthProfileTO> authProfileModal;
58  
59      protected final AuthProfileTO authProfile;
60  
61      public AuthProfileItemDirectoryPanel(
62              final String id,
63              final AuthProfileRestClient restClient,
64              final BaseModal<AuthProfileTO> authProfileModal,
65              final AuthProfileTO authProfile,
66              final PageReference pageRef) {
67  
68          super(id, restClient, pageRef, false);
69  
70          this.authProfileModal = authProfileModal;
71          this.authProfile = authProfile;
72  
73          setOutputMarkupId(true);
74  
75          enableUtilityButton();
76          setFooterVisibility(false);
77  
78          addNewItemPanelBuilder(new AuthProfileItemWizardBuilder(pageRef), true);
79  
80          disableCheckBoxes();
81          initResultTable();
82      }
83  
84      protected abstract List<I> getItems();
85  
86      protected abstract I defaultItem();
87  
88      protected abstract String sortProperty();
89  
90      @Override
91      public void onEvent(final IEvent<?> event) {
92          if (event.getPayload() instanceof ExitEvent) {
93              AjaxRequestTarget target = ExitEvent.class.cast(event.getPayload()).getTarget();
94              authProfileModal.close(target);
95          } else if (event.getPayload() instanceof AjaxWizard.EditItemActionEvent) {
96              @SuppressWarnings("unchecked")
97              AjaxWizard.EditItemActionEvent<?> payload = (AjaxWizard.EditItemActionEvent<?>) event.getPayload();
98              payload.getTarget().ifPresent(actionTogglePanel::close);
99          }
100         super.onEvent(event);
101     }
102 
103     @Override
104     protected AuthProfileItemProvider dataProvider() {
105         return new AuthProfileItemProvider(sortProperty(), rows);
106     }
107 
108     @Override
109     protected Collection<ActionLink.ActionType> getBatches() {
110         return List.of();
111     }
112 
113     @Override
114     public ActionsPanel<I> getActions(final IModel<I> model) {
115         ActionsPanel<I> panel = super.getActions(model);
116 
117         panel.add(new ActionLink<>() {
118 
119             private static final long serialVersionUID = -3722207913631435501L;
120 
121             @Override
122             public void onClick(final AjaxRequestTarget target, final I ignore) {
123                 send(AuthProfileItemDirectoryPanel.this, Broadcast.EXACT,
124                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
125             }
126         }, ActionLink.ActionType.EDIT, AMEntitlement.AUTH_PROFILE_UPDATE);
127 
128         panel.add(new ActionLink<>() {
129 
130             private static final long serialVersionUID = -3722207913631435501L;
131 
132             @Override
133             public void onClick(final AjaxRequestTarget target, final I ignore) {
134                 try {
135                     getItems().remove(model.getObject());
136                     restClient.update(authProfile);
137 
138                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
139                     target.add(container);
140                 } catch (Exception e) {
141                     LOG.error("While deleting {} from {}", model.getObject(), authProfile.getKey(), e);
142                     SyncopeConsoleSession.get().onException(e);
143                 }
144                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
145             }
146         }, ActionLink.ActionType.DELETE, AMEntitlement.AUTH_PROFILE_UPDATE, true);
147 
148         return panel;
149     }
150 
151     protected class AuthProfileItemProvider extends DirectoryDataProvider<I> {
152 
153         private static final long serialVersionUID = 4725679400450513556L;
154 
155         private final SortableDataProviderComparator<I> comparator;
156 
157         AuthProfileItemProvider(final String sort, final int paginatorRows) {
158             super(paginatorRows);
159 
160             setSort(sort, SortOrder.ASCENDING);
161             comparator = new SortableDataProviderComparator<>(this);
162         }
163 
164         @Override
165         public Iterator<I> iterator(final long first, final long count) {
166             List<I> list = getItems();
167             list.sort(comparator);
168             return list.subList((int) first, (int) first + (int) count).iterator();
169         }
170 
171         @Override
172         public long size() {
173             return getItems().size();
174         }
175 
176         @Override
177         public IModel<I> model(final I object) {
178             return new CompoundPropertyModel<>(object);
179         }
180     }
181 
182     protected class AuthProfileItemWizardBuilder extends AuthProfileWizardBuilder<I> {
183 
184         private static final long serialVersionUID = -7174537333960225216L;
185 
186         protected AuthProfileItemWizardBuilder(final PageReference pageRef) {
187             super(defaultItem(), new StepModel<>(), pageRef);
188         }
189 
190         @Override
191         protected Serializable onApplyInternal(final I modelObject) {
192             if (modelObject instanceof GoogleMfaAuthAccount) {
193                 BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(modelObject);
194                 @SuppressWarnings("unchecked")
195                 List<Serializable> values = (List<Serializable>) wrapper.getPropertyValue("scratchCodes");
196                 if (values != null) {
197                     List<Integer> converted = values.stream().map(value -> {
198                         if (value instanceof Integer) {
199                             return Integer.class.cast(value);
200                         }
201                         if (value instanceof String) {
202                             try {
203                                 return Integer.valueOf((String) value);
204                             } catch (NumberFormatException e) {
205                                 LOG.error("Could not convert to Integer: {}", value, e);
206                             }
207                         }
208                         return null;
209                     }).filter(Objects::nonNull).collect(Collectors.toList());
210                     wrapper.setPropertyValue("scratchCodes", converted);
211                 }
212             }
213 
214             getItems().remove(model.getInitialModelObject());
215             getItems().add(modelObject);
216             restClient.update(authProfile);
217 
218             return modelObject;
219         }
220     }
221 }