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.wizards.any;
20  
21  import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.checkbox.bootstraptoggle.BootstrapToggle;
22  import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.checkbox.bootstraptoggle.BootstrapToggleConfig;
23  import java.util.List;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.console.commons.LinkedAccountPlainAttrProperty;
26  import org.apache.syncope.client.ui.commons.Constants;
27  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
28  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxPasswordFieldPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
31  import org.apache.syncope.client.ui.commons.wizards.any.EntityWrapper;
32  import org.apache.syncope.common.lib.to.LinkedAccountTO;
33  import org.apache.wicket.ajax.AjaxRequestTarget;
34  import org.apache.wicket.extensions.wizard.WizardStep;
35  import org.apache.wicket.markup.ComponentTag;
36  import org.apache.wicket.markup.html.form.CheckBox;
37  import org.apache.wicket.markup.html.form.FormComponent;
38  import org.apache.wicket.markup.html.form.PasswordTextField;
39  import org.apache.wicket.model.IModel;
40  import org.apache.wicket.model.Model;
41  import org.apache.wicket.model.PropertyModel;
42  
43  public class LinkedAccountCredentialsPanel extends WizardStep {
44  
45      private static final long serialVersionUID = 5116461957402341603L;
46  
47      private String usernameValue;
48  
49      private String passwordValue;
50  
51      private final LinkedAccountTO linkedAccountTO;
52  
53      public LinkedAccountCredentialsPanel(
54              final EntityWrapper<LinkedAccountTO> modelObject, final List<String> whichCredentials) {
55          super();
56          setOutputMarkupId(true);
57  
58          linkedAccountTO = modelObject.getInnerObject();
59  
60          boolean isUsernameManagementEnabled = whichCredentials.contains(Constants.USERNAME_FIELD_NAME);
61          AjaxTextFieldPanel usernameField = new AjaxTextFieldPanel(
62                  Constants.USERNAME_FIELD_NAME,
63                  Constants.USERNAME_FIELD_NAME,
64                  new PropertyModel<>(linkedAccountTO, Constants.USERNAME_FIELD_NAME));
65          FieldPanel.class.cast(usernameField).setReadOnly(StringUtils.isBlank(linkedAccountTO.getUsername()));
66          LinkedAccountPlainAttrProperty usernameProperty = new LinkedAccountPlainAttrProperty();
67          usernameProperty.setOverridable(StringUtils.isNotBlank(linkedAccountTO.getUsername()));
68          usernameProperty.setSchema(Constants.USERNAME_FIELD_NAME);
69          usernameProperty.getValues().add(linkedAccountTO.getUsername());
70          usernameField.showExternAction(
71                  checkboxToggle(usernameProperty, usernameField).setEnabled(isUsernameManagementEnabled));
72          add(usernameField.setOutputMarkupId(true));
73          usernameField.setEnabled(isUsernameManagementEnabled);
74  
75          boolean isPasswordManagementEnabled = whichCredentials.contains("password");
76          AjaxPasswordFieldPanel passwordField = new AjaxPasswordFieldPanel(
77                  "password",
78                  "password",
79                  new PropertyModel<>(linkedAccountTO, "password"),
80                  false);
81          passwordField.setMarkupId("password");
82          passwordField.setRequired(true);
83          FieldPanel.class.cast(passwordField).setReadOnly(StringUtils.isBlank(linkedAccountTO.getPassword()));
84          LinkedAccountPlainAttrProperty passwordProperty = new LinkedAccountPlainAttrProperty();
85          passwordProperty.setOverridable(StringUtils.isNotBlank(linkedAccountTO.getPassword()));
86          passwordProperty.setSchema("password");
87          passwordProperty.getValues().add(linkedAccountTO.getPassword());
88          passwordField.showExternAction(
89                  checkboxToggle(passwordProperty, passwordField).setEnabled(isPasswordManagementEnabled));
90          ((PasswordTextField) passwordField.getField()).setResetPassword(false);
91          add(passwordField.setOutputMarkupId(true));
92          passwordField.setEnabled(isPasswordManagementEnabled);
93      }
94  
95      private FormComponent<?> checkboxToggle(
96              final LinkedAccountPlainAttrProperty property, final FieldPanel<?> panel) {
97  
98          final BootstrapToggleConfig config = new BootstrapToggleConfig().
99                  withOnStyle(BootstrapToggleConfig.Style.success).
100                 withOffStyle(BootstrapToggleConfig.Style.danger).
101                 withSize(BootstrapToggleConfig.Size.mini);
102 
103         return new BootstrapToggle("externalAction", new PropertyModel<>(property, "overridable"), config) {
104 
105             private static final long serialVersionUID = -875219845189261873L;
106 
107             @Override
108             protected CheckBox newCheckBox(final String id, final IModel<Boolean> model) {
109                 final CheckBox checkBox = super.newCheckBox(id, model);
110                 checkBox.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
111 
112                     private static final long serialVersionUID = -1107858522700306810L;
113 
114                     @Override
115                     protected void onUpdate(final AjaxRequestTarget target) {
116                         FieldPanel.class.cast(panel).setReadOnly(!model.getObject());
117                         if (model.getObject()) {
118                             if (property.getSchema().equals("password")) {
119                                 linkedAccountTO.setPassword(passwordValue);
120                             } else if (property.getSchema().equals(Constants.USERNAME_FIELD_NAME)) {
121                                 linkedAccountTO.setUsername(usernameValue);
122                             }
123                         } else {
124                             if (property.getSchema().equals("password")) {
125                                 passwordValue = linkedAccountTO.getPassword();
126                                 linkedAccountTO.setPassword(null);
127                             } else if (property.getSchema().equals(Constants.USERNAME_FIELD_NAME)) {
128                                 usernameValue = linkedAccountTO.getUsername();
129                                 linkedAccountTO.setUsername(null);
130                             }
131                         }
132                         target.add(panel);
133                     }
134                 });
135                 return checkBox;
136             }
137 
138             @Override
139             protected IModel<String> getOnLabel() {
140                 return Model.of("Override");
141             }
142 
143             @Override
144             protected IModel<String> getOffLabel() {
145                 return Model.of("Override?");
146             }
147 
148             @Override
149             protected void onComponentTag(final ComponentTag tag) {
150                 super.onComponentTag(tag);
151                 tag.append("class", "overridable", " ");
152             }
153         };
154     }
155 }