1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }