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.Validator.InvalidValueException;
47 import com.vaadin.v7.ui.CustomField;
48 import com.vaadin.v7.ui.Label;
49 import com.vaadin.v7.ui.PasswordField;
50 import com.vaadin.v7.ui.VerticalLayout;
51
52
53
54
55
56
57
58
59 public class PasswordFields extends CustomField<String> {
60
61 private PasswordField passwordField;
62 private PasswordField verificationField;
63 private PasswordField currentPasswordField;
64 private Provider<Context> contextProvider;
65 private PasswordFieldDefinition definition;
66 private VerticalLayout layout;
67
68 public PasswordFields(Provider<Context> contextProvider, PasswordFieldDefinition definition) {
69 this.contextProvider = contextProvider;
70 this.definition = definition;
71
72 passwordField = new PasswordField();
73 passwordField.setNullRepresentation("");
74 passwordField.setWidth("100%");
75 if (this.definition.isVerification()) {
76 verificationField = new PasswordField();
77 verificationField.setNullRepresentation("");
78 verificationField.setWidth("100%");
79 }
80 if (this.definition.isVerificationCurrentPassword()) {
81 currentPasswordField = new PasswordField();
82 currentPasswordField.setNullRepresentation("");
83 currentPasswordField.setWidth("100%");
84 }
85
86 getContent();
87 }
88
89
90
91
92
93
94
95
96
97
98 @Deprecated
99 public PasswordFields(boolean verification, String verificationMessage, String verificationErrorMessage) {
100 this(MgnlContext::getInstance, new PasswordFieldDefinition());
101 }
102
103 @Override
104 protected Component initContent() {
105
106 layout = new VerticalLayout();
107 if (definition.isVerificationCurrentPassword()) {
108 layout.addComponent(new Label(definition.getVerificationCurrentPasswordMessage()));
109 layout.addComponent(currentPasswordField);
110 }
111
112 if (definition.isVerification() || definition.isVerificationCurrentPassword()) {
113 layout.addComponent(new Label(definition.getPasswordMessage()));
114 }
115 layout.addComponent(passwordField);
116 if (definition.isVerification()) {
117 layout.addComponent(new Label(definition.getVerificationMessage()));
118 layout.addComponent(verificationField);
119 }
120 return layout;
121 }
122
123 public VerticalLayout getVerticalLayout() {
124 return this.layout;
125 }
126
127
128
129
130 @Override
131 public void validate() throws InvalidValueException {
132 super.validate();
133
134 boolean validationFieldHasValue = verificationField != null && StringUtils.isNotBlank(verificationField.getValue());
135 if (StringUtils.isNotBlank(passwordField.getValue()) || validationFieldHasValue) {
136 if (definition.isVerificationCurrentPassword()) {
137 if (StringUtils.isBlank(currentPasswordField.getValue()) || !SecurityUtil.matchBCrypted(currentPasswordField.getValue(), contextProvider.get().getUser().getPassword())) {
138 throw new InvalidValueException(definition.getVerificationCurrentPasswordErrorMessage());
139 }
140 }
141 if (definition.isVerification()) {
142 if (StringUtils.isBlank(passwordField.getValue()) || StringUtils.isBlank(verificationField.getValue())) {
143 throw new InvalidValueException(definition.getVerificationErrorMessage());
144 }
145 if (!passwordField.getValue().equals(verificationField.getValue())) {
146 throw new InvalidValueException(definition.getVerificationErrorMessage());
147 }
148 }
149 setInvalidCommitted(true);
150 setValue(passwordField.getValue(), true);
151 }
152 }
153
154 @Override
155 public Class<String> getType() {
156 return String.class;
157 }
158
159 @Override
160 public void setReadOnly(boolean readOnly) {
161 super.setReadOnly(readOnly);
162 passwordField.setReadOnly(readOnly);
163 if (verificationField != null) {
164 verificationField.setReadOnly(readOnly);
165 }
166 }
167
168 @Override
169 public boolean isEmpty() {
170
171 String currentFieldValue = StringUtils.firstNonBlank(passwordField.getValue(), getInternalValue());
172 return StringUtils.isBlank(currentFieldValue);
173 }
174 }