]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/validator/password/JobsUserPasswordValidator.java
renamed package
[jjobs-war.git] / src / java / org / mxchange / jjobs / validator / password / JobsUserPasswordValidator.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.validator.password;
18
19 import java.text.MessageFormat;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.validator.FacesValidator;
24 import javax.faces.validator.Validator;
25 import javax.faces.validator.ValidatorException;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
30 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
31 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
32 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
33 import org.mxchange.jusercore.container.login.LoginContainer;
34 import org.mxchange.jusercore.container.login.UserLoginContainer;
35 import org.mxchange.jusercore.model.user.UserUtils;
36
37 /**
38  * A validator for validating passwords (if they match with stored)
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  */
42 @FacesValidator (value = "UserPasswordValidator")
43 public class JobsUserPasswordValidator extends BaseStringValidator implements Validator {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 48_581_795_687_317L;
49
50         /**
51          * Logger instance
52          */
53         @Log
54         private LoggerBeanLocal loggerBeanLocal;
55
56         /**
57          * User login controller
58          */
59         private JobsUserLoginWebSessionController userLoginController;
60
61         /**
62          * Default constructor
63          */
64         public JobsUserPasswordValidator () {
65                 // Try to get it
66                 try {
67                         // Get initial context
68                         Context context = new InitialContext();
69
70                         // Lookup logger
71                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
72                 } catch (final NamingException ex) {
73                         // Continue to throw it
74                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
75                 }
76         }
77
78         @Override
79         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
80                 // Trace message
81                 this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
82
83                 // The required field
84                 String[] requiredFields = {"currentPassword"}; //NOI18N
85
86                 // Pre-validation (example: not null, not a string, empty string ...)
87                 super.preValidate(context, component, value, requiredFields, false);
88
89                 // value is known to be an entered password, so instance login container
90                 LoginContainer container = new UserLoginContainer(this.userLoginController.getLoggedInUser(), (String) value);
91
92                 // Test it here
93                 if (!UserUtils.ifPasswordMatches(container, this.userLoginController.getLoggedInUser())) {
94                         // Password mismatches
95                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password mismatching.", "The password the user has entered does not match the stored password.")); //NOI18N
96                 }
97
98                 // Trace message
99                 this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N
100         }
101
102 }