2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.validator.password;
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;
38 * A validator for validating passwords (if they match with stored)
40 * @author Roland Haeder<roland@mxchange.org>
42 @FacesValidator (value = "UserPasswordValidator")
43 public class JobsUserPasswordValidator extends BaseStringValidator implements Validator {
48 private static final long serialVersionUID = 48_581_795_687_317L;
54 private LoggerBeanLocal loggerBeanLocal;
57 * User login controller
59 private JobsUserLoginWebSessionController userLoginController;
64 public JobsUserPasswordValidator () {
67 // Get initial context
68 Context context = new InitialContext();
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
79 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
81 this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
84 String[] requiredFields = {"currentPassword"}; //NOI18N
86 // Pre-validation (example: not null, not a string, empty string ...)
87 super.preValidate(context, component, value, requiredFields, false);
89 // value is known to be an entered password, so instance login container
90 LoginContainer container = new UserLoginContainer(this.userLoginController.getLoggedInUser(), (String) value);
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
99 this.loggerBeanLocal.logTrace("validate: EXIT!"); //NOI18N