1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.form.field;
35
36 import info.magnolia.cms.security.SecurityUtil;
37 import info.magnolia.context.Context;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.ui.form.field.definition.PasswordFieldDefinition;
40
41 import javax.inject.Provider;
42
43 import org.apache.commons.lang3.StringUtils;
44
45 import com.vaadin.ui.Component;
46 import com.vaadin.v7.data.Property;
47 import com.vaadin.v7.data.Validator.InvalidValueException;
48 import com.vaadin.v7.ui.CustomField;
49 import com.vaadin.v7.ui.Label;
50 import com.vaadin.v7.ui.PasswordField;
51 import com.vaadin.v7.ui.VerticalLayout;
52
53
54
55
56
57
58
59
60 public class PasswordFields extends CustomField<String> {
61
62 private PasswordField passwordField;
63 private PasswordField verificationField;
64 private PasswordField currentPasswordField;
65 private Provider<Context> contextProvider;
66 private PasswordFieldDefinition definition;
67 private VerticalLayout layout;
68
69 public PasswordFields(Provider<Context> contextProvider, PasswordFieldDefinition definition) {
70 this.contextProvider = contextProvider;
71 this.definition = definition;
72
73 passwordField = new PasswordField();
74 passwordField.setNullRepresentation("");
75 passwordField.setWidth("100%");
76 passwordField.addValueChangeListener(event -> setValue((String) event.getProperty().getValue(), true));
77 if (this.definition.isVerification()) {
78 verificationField = new PasswordField();
79 verificationField.setNullRepresentation("");
80 verificationField.setWidth("100%");
81 verificationField.addValueChangeListener(event -> setValue((String) event.getProperty().getValue(), true));
82 }
83 if (this.definition.isVerificationCurrentPassword()) {
84 currentPasswordField = new PasswordField();
85 currentPasswordField.setNullRepresentation("");
86 currentPasswordField.setWidth("100%");
87 }
88
89 getContent();
90 }
91
92
93
94
95
96
97
98
99
100
101 @Deprecated
102 public PasswordFields(boolean verification, String verificationMessage, String verificationErrorMessage) {
103 this(MgnlContext::getInstance, new PasswordFieldDefinition());
104 }
105
106 @Override
107 protected Component initContent() {
108
109 layout = new VerticalLayout();
110 if (definition.isVerificationCurrentPassword()) {
111 layout.addComponent(new Label(definition.getVerificationCurrentPasswordMessage()));
112 layout.addComponent(currentPasswordField);
113 }
114
115 if (definition.isVerification() || definition.isVerificationCurrentPassword()) {
116 layout.addComponent(new Label(definition.getPasswordMessage()));
117 }
118 layout.addComponent(passwordField);
119 if (definition.isVerification()) {
120 layout.addComponent(new Label(definition.getVerificationMessage()));
121 layout.addComponent(verificationField);
122 }
123 return layout;
124 }
125
126 public VerticalLayout getVerticalLayout() {
127 return this.layout;
128 }
129
130
131
132
133 @Override
134 public void validate() throws InvalidValueException {
135 super.validate();
136 if (StringUtils.isNotBlank(passwordField.getValue())) {
137 if (definition.isVerificationCurrentPassword()) {
138 if (StringUtils.isBlank(currentPasswordField.getValue()) || !SecurityUtil.matchBCrypted(currentPasswordField.getValue(), contextProvider.get().getUser().getPassword())) {
139 throw new InvalidValueException(definition.getVerificationCurrentPasswordErrorMessage());
140 }
141 }
142 if (definition.isVerification()) {
143 if (StringUtils.isBlank(passwordField.getValue()) || StringUtils.isBlank(verificationField.getValue())) {
144 throw new InvalidValueException(definition.getVerificationErrorMessage());
145 }
146 if (!passwordField.getValue().equals(verificationField.getValue())) {
147 throw new InvalidValueException(definition.getVerificationErrorMessage());
148 }
149 }
150 }
151 }
152
153 @Override
154 public boolean isValid() {
155 if (super.isValid()) {
156 try {
157 this.validate();
158 return true;
159 } catch (InvalidValueException ive) {
160 return false;
161 }
162 } else {
163 return false;
164 }
165 }
166
167 @Override
168 public Class<String> getType() {
169 return String.class;
170 }
171
172 @Override
173 protected void setInternalValue(String newValue) {
174 super.setInternalValue(newValue);
175 }
176
177 @Override
178 public void setReadOnly(boolean readOnly) {
179 super.setReadOnly(readOnly);
180 passwordField.setReadOnly(readOnly);
181 if (verificationField != null) {
182 verificationField.setReadOnly(readOnly);
183 }
184 }
185
186 @Override
187 public String getValue() {
188 return super.getValue();
189 }
190
191 @Override
192 public void setValue(String newValue) throws ReadOnlyException {
193 super.setValue(newValue);
194 }
195
196 @Override
197 @SuppressWarnings("rawtypes")
198 public void setPropertyDataSource(Property newDataSource) {
199 newDataSource.setValue("");
200 passwordField.setPropertyDataSource(newDataSource);
201 if (definition.isVerification() && newDataSource.getValue() != null) {
202 verificationField.setValue(String.valueOf(newDataSource.getValue()));
203 }
204 super.setPropertyDataSource(newDataSource);
205 }
206
207 @Override
208 public Property<?> getPropertyDataSource() {
209 return passwordField.getPropertyDataSource();
210 }
211
212 @Override
213 public boolean isEmpty() {
214
215 return StringUtils.isBlank(getInternalValue());
216 }
217 }