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.policies;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.client.console.SyncopeConsoleSession;
24  import org.apache.syncope.client.console.panels.AbstractModalPanel;
25  import org.apache.syncope.client.console.rest.AuthModuleRestClient;
26  import org.apache.syncope.client.console.rest.PolicyRestClient;
27  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
28  import org.apache.syncope.client.ui.commons.Constants;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPalettePanel;
31  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
32  import org.apache.syncope.common.lib.policy.AuthPolicyTO;
33  import org.apache.syncope.common.lib.to.AuthModuleTO;
34  import org.apache.syncope.common.lib.types.PolicyType;
35  import org.apache.wicket.PageReference;
36  import org.apache.wicket.ajax.AjaxRequestTarget;
37  import org.apache.wicket.model.IModel;
38  import org.apache.wicket.model.LoadableDetachableModel;
39  import org.apache.wicket.model.PropertyModel;
40  import org.apache.wicket.spring.injection.annot.SpringBean;
41  
42  public class AuthPolicyModalPanel extends AbstractModalPanel<AuthPolicyTO> {
43  
44      private static final long serialVersionUID = -7210166323800567306L;
45  
46      @SpringBean
47      protected PolicyRestClient policyRestClient;
48  
49      @SpringBean
50      protected AuthModuleRestClient authModuleRestClient;
51  
52      protected final IModel<List<String>> allAuthModules = new LoadableDetachableModel<>() {
53  
54          private static final long serialVersionUID = -2012833443695917883L;
55  
56          @Override
57          protected List<String> load() {
58              return authModuleRestClient.list().stream().map(AuthModuleTO::getKey).sorted().collect(Collectors.toList());
59          }
60      };
61  
62      private final IModel<AuthPolicyTO> model;
63  
64      public AuthPolicyModalPanel(
65              final BaseModal<AuthPolicyTO> modal,
66              final IModel<AuthPolicyTO> model,
67              final PageReference pageRef) {
68  
69          super(modal, pageRef);
70          this.model = model;
71  
72          add(new AjaxCheckBoxPanel(
73                  "tryAll",
74                  "tryAll",
75                  new PropertyModel<>(model.getObject().getConf(), "tryAll"),
76                  false));
77  
78          add(new AjaxPalettePanel.Builder<String>().setName("authModules").build(
79                  "authModules",
80                  new PropertyModel<>(model.getObject().getConf(), "authModules"),
81                  allAuthModules));
82      }
83  
84      @Override
85      public void onSubmit(final AjaxRequestTarget target) {
86          try {
87              policyRestClient.update(PolicyType.AUTH, model.getObject());
88  
89              SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
90              modal.close(target);
91          } catch (Exception e) {
92              LOG.error("While updating Auth Policy {}", model.getObject().getKey(), e);
93              SyncopeConsoleSession.get().onException(e);
94          }
95          ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
96      }
97  }