]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/validators/password/UserPasswordValidator.java
Continued with updating personal data:
[addressbook-war.git] / src / java / org / mxchange / addressbook / validators / password / UserPasswordValidator.java
1 /*
2  * Copyright (C) 2016 quix0r
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.validators.password;
18
19 import javax.faces.application.FacesMessage;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.validator.Validator;
23 import javax.faces.validator.ValidatorException;
24 import javax.inject.Inject;
25 import org.mxchange.addressbook.beans.login.UserLoginWebSessionController;
26 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
27 import org.mxchange.jusercore.container.login.LoginContainer;
28 import org.mxchange.jusercore.container.login.UserLoginContainer;
29 import org.mxchange.jusercore.model.user.UserUtils;
30
31 /**
32  * A validator for validating passwords (if they match with stored)
33  * <p>
34  * @author Roland Haeder
35  */
36 public class UserPasswordValidator extends BaseStringValidator implements Validator {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 48_581_795_687_317L;
42
43         /**
44          * User login controller
45          */
46         @Inject
47         private UserLoginWebSessionController loginController;
48
49         @Override
50         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
51                 // Trace message
52                 //this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
53
54                 // The required field
55                 String[] requiredFileds = {"currentPassword"}; //NOI18N
56
57                 // Pre-validation (example: not null, not a string, empty string ...)
58                 super.preValidate(context, component, value, requiredFileds, false);
59
60                 // value is known to be an entered password, so instance login container
61                 LoginContainer container = new UserLoginContainer(this.loginController.getLoggedInUser(), (String) value);
62
63                 // Test it here
64                 if (!UserUtils.ifPasswordMatches(container, this.loginController.getLoggedInUser())) {
65                         // Password mismatches
66                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password mismatching.", "The password the user has entered does not match the stored password.")); //NOI18N
67                 }
68
69                 // Trace message
70                 //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
71         }
72 }