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.status;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.panels.AbstractModalPanel;
26  import org.apache.syncope.client.console.panels.ListViewPanel;
27  import org.apache.syncope.client.console.rest.UserRestClient;
28  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
29  import org.apache.syncope.client.console.wizards.any.StatusPanel;
30  import org.apache.syncope.client.ui.commons.Constants;
31  import org.apache.syncope.client.ui.commons.status.StatusBean;
32  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
33  import org.apache.syncope.client.ui.commons.wizards.any.PasswordPanel;
34  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
35  import org.apache.syncope.common.lib.request.PasswordPatch;
36  import org.apache.syncope.common.lib.request.UserUR;
37  import org.apache.syncope.common.lib.to.UserTO;
38  import org.apache.wicket.PageReference;
39  import org.apache.wicket.ajax.AjaxRequestTarget;
40  import org.apache.wicket.model.IModel;
41  import org.apache.wicket.model.util.ListModel;
42  import org.apache.wicket.spring.injection.annot.SpringBean;
43  
44  public class ChangePasswordModal extends AbstractModalPanel<AnyWrapper<UserTO>> {
45  
46      private static final long serialVersionUID = 6257389301592059194L;
47  
48      @SpringBean
49      protected UserRestClient userRestClient;
50  
51      protected final IModel<List<StatusBean>> statusModel;
52  
53      protected final UserWrapper wrapper;
54  
55      public ChangePasswordModal(
56              final BaseModal<AnyWrapper<UserTO>> baseModal,
57              final UserWrapper wrapper,
58              final PageReference pageRefer) {
59  
60          super(baseModal, pageRefer);
61          this.wrapper = wrapper;
62  
63          PasswordPanel passwordPanel = new PasswordPanel("passwordPanel", wrapper, false, false);
64          passwordPanel.setOutputMarkupId(true);
65          add(passwordPanel);
66  
67          statusModel = new ListModel<>(new ArrayList<>());
68          StatusPanel statusPanel = new StatusPanel("status", wrapper.getInnerObject(), statusModel, pageRefer);
69          statusPanel.setCheckAvailability(ListViewPanel.CheckAvailability.AVAILABLE);
70          add(statusPanel.setRenderBodyOnly(true));
71      }
72  
73      @Override
74      public void onSubmit(final AjaxRequestTarget target) {
75          UserTO inner = wrapper.getInnerObject();
76  
77          try {
78              if (StringUtils.isBlank(inner.getPassword()) || statusModel.getObject().isEmpty()) {
79                  SyncopeConsoleSession.get().error(getString(Constants.OPERATION_ERROR));
80              } else {
81                  List<String> resources = new ArrayList<>();
82                  boolean isOnSyncope = false;
83                  for (StatusBean sb : statusModel.getObject()) {
84                      if (sb.getResource().equals(Constants.SYNCOPE)) {
85                          isOnSyncope = true;
86                      } else {
87                          resources.add(sb.getResource());
88                      }
89                  }
90  
91                  UserUR req = new UserUR.Builder(inner.getKey()).
92                          password(new PasswordPatch.Builder().
93                                  value(inner.getPassword()).onSyncope(isOnSyncope).resources(resources).build()).
94                          build();
95  
96                  userRestClient.update(inner.getETagValue(), req);
97                  SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
98                  modal.show(false);
99                  modal.close(target);
100             }
101         } catch (Exception e) {
102             LOG.error("While updating password for user {}", inner, e);
103             SyncopeConsoleSession.get().onException(e);
104         }
105         super.onSubmit(target);
106     }
107 }