-
Notifications
You must be signed in to change notification settings - Fork 2
/
SubjectForm.java
143 lines (122 loc) · 4.3 KB
/
SubjectForm.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.application.manager.form;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.FormValidator;
import org.appng.api.MessageParam;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.auth.PasswordPolicy;
import org.appng.api.auth.PasswordPolicy.ValidationResult;
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.api.model.UserType;
import org.appng.application.manager.MessageConstants;
import org.appng.core.domain.SubjectImpl;
import org.appng.xml.platform.FieldDef;
import org.appng.xml.platform.Validation;
/**
* Bindclass used for creating/updating a {@link SubjectImpl}.
*
* @author Matthias Müller
*/
public class SubjectForm implements FormValidator, Serializable {
private SubjectImpl subject;
private List<Integer> groupIds = new ArrayList<Integer>();
private String password;
private String passwordConfirmation;
public SubjectForm() {
this(new SubjectImpl());
}
public SubjectForm(SubjectImpl subject) {
this.subject = subject;
}
public void validate(Site site, Application application, Environment environment, Options options, Request request,
FieldProcessor fp) {
if (isLocalUser()) {
if (StringUtils.isBlank(getSubject().getEmail())) {
String message = request.getMessage(MessageConstants.SUBJECT_ENTER_EMAIL);
fp.addErrorMessage(fp.getField("subject.email"), message);
}
String action = options.getOptionValue("action", "id");
FieldDef passwordField = fp.getField("password");
if ("create".equals(action)) {
if (null == password) {
String message = request.getMessage(MessageConstants.SUBJECT_ENTER_PASSWORD);
fp.addErrorMessage(passwordField, message);
}
if (null == passwordConfirmation) {
String message = request.getMessage(MessageConstants.SUBJECT_ENTER_PASSWORD_CONFIRMATION);
fp.addErrorMessage(fp.getField("passwordConfirmation"), message);
}
}
PasswordPolicy passwordPolicy = site.getPasswordPolicy();
if (!StringUtils.isEmpty(password)) {
ValidationResult validationResult = passwordPolicy.validatePassword(getSubject().getAuthName(), null,
password.toCharArray());
if (!StringUtils.equals(password, passwordConfirmation)) {
String message = request.getMessage(MessageConstants.SUBJECT_PASSWORDS_NO_MATCH);
passwordField.setValidation(new Validation());
fp.addErrorMessage(fp.getField("passwordConfirmation"), message);
} else if (!validationResult.isValid()) {
passwordField.setValidation(new Validation());
for (MessageParam m : validationResult.getMessages()) {
String errMssg = request.getMessage(m.getMessageKey(), m.getMessageArgs());
fp.addErrorMessage(passwordField, errMssg);
}
}
}
}
}
@Valid
public SubjectImpl getSubject() {
return subject;
}
public void setSubject(SubjectImpl subject) {
this.subject = subject;
}
public List<Integer> getGroupIds() {
return groupIds;
}
public void setGroupIds(List<Integer> groupIds) {
this.groupIds = groupIds;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
if (!"".equals(password)) {
this.password = password;
}
}
public String getPasswordConfirmation() {
return passwordConfirmation;
}
public void setPasswordConfirmation(String passwordConfirmation) {
if (!"".equals(passwordConfirmation)) {
this.passwordConfirmation = passwordConfirmation;
}
}
public boolean isLocalUser() {
return UserType.LOCAL_USER.equals(getSubject().getUserType());
}
}