]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/login/JobsUserLoginWebSessionBean.java
Continued a bit: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / login / JobsUserLoginWebSessionBean.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.beans.login;
18
19 import java.util.Objects;
20 import javax.enterprise.context.SessionScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jjobs.beans.BaseJobsController;
30 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
31 import org.mxchange.jusercore.container.login.LoginContainer;
32 import org.mxchange.jusercore.container.login.UserLoginContainer;
33 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
34 import org.mxchange.jusercore.events.login.UserLoginEvent;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
39 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.user.UserUtils;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44
45 /**
46  * A web bean for user registration
47  * <p>
48  * @author Roland Haeder<roland@mxchange.org>
49  */
50 @Named ("loginController")
51 @SessionScoped
52 public class JobsUserLoginWebSessionBean extends BaseJobsController implements JobsUserLoginWebSessionController {
53
54         /**
55          * Path name for guest base template
56          */
57         private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; //NOI18N
58
59         /**
60          * Path name for logged-in user base template
61          */
62         private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; //NOI18N
63
64         /**
65          * Serial number
66          */
67         private static final long serialVersionUID = 47_828_986_719_691_592L;
68
69         /**
70          * Current password
71          */
72         private String currentPassword;
73
74         /**
75          * Logged-in user instance
76          */
77         private User loggedInUser;
78
79         /**
80          * Remote register session bean
81          */
82         private UserLoginSessionBeanRemote loginBean;
83
84         /**
85          * Event fired when user has logged in
86          */
87         @Inject
88         @Any
89         private Event<UserLoggedInEvent> loginEvent;
90
91         /**
92          * Template type for pages that might be displayed in guest area and login
93          * area. Default is guest area.
94          */
95         private String templateType = "guest"; //NOI18N
96
97         /**
98          * User controller
99          */
100         @Inject
101         private JobsUserWebSessionController userController;
102
103         /**
104          * Flag whether the user has logged-in, set only from inside
105          */
106         private boolean userLoggedIn;
107
108         /**
109          * Default constructor
110          */
111         public JobsUserLoginWebSessionBean () {
112                 try {
113                         // Get initial context
114                         Context context = new InitialContext();
115
116                         // Try to lookup
117                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jjobs-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
118                 } catch (final NamingException ex) {
119                         // Continue to throw
120                         throw new FaceletException(ex);
121                 }
122         }
123
124         @Override
125         public String doLogin () {
126                 // Get user instance
127                 User user = this.userController.createUserLogin();
128
129                 // Create login container
130                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
131
132                 try {
133                         // Call bean
134                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
135
136                         // All fine here so set it here
137                         this.setLoggedInUser(confirmedUser);
138
139                         // Set template to "login"
140                         this.setTemplateType("login"); //NOI18N
141
142                         // Fire event away. Keep this last before return statement.
143                         this.loginEvent.fire(new UserLoginEvent(confirmedUser));
144
145                         // All fine
146                         return "login"; //NOI18N
147                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException ex) {
148                         // Show JSF message
149                         this.showFacesMessage("form_user_login:userName", ex); //NOI18N
150                         return ""; //NOI18N
151                 } catch (final UserPasswordMismatchException ex) {
152                         // Show JSF message
153                         this.showFacesMessage("form_user_login:userPassword", ex); //NOI18N
154                         return ""; //NOI18N
155                 }
156         }
157
158         @Override
159         public String getCurrentPassword () {
160                 return this.currentPassword;
161         }
162
163         @Override
164         public void setCurrentPassword (final String currentPassword) {
165                 this.currentPassword = currentPassword;
166         }
167
168         @Override
169         public User getLoggedInUser () {
170                 return this.loggedInUser;
171         }
172
173         @Override
174         public void setLoggedInUser (final User loggedInUser) {
175                 this.loggedInUser = loggedInUser;
176         }
177
178         @Override
179         public String getTemplateType () {
180                 return this.templateType;
181         }
182
183         @Override
184         public void setTemplateType (final String templateType) {
185                 this.templateType = templateType;
186         }
187
188         @Override
189         public boolean ifCurrentPasswordMatches () {
190                 // The current password must be set and not empty
191                 if (this.getCurrentPassword() == null) {
192                         // Is not set
193                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
194                 } else if (this.getCurrentPassword().isEmpty()) {
195                         // Is set empty
196                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
197                 }
198
199                 // Create "container"
200                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
201
202                 // Now check if it matches
203                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
204         }
205
206         @Override
207         public boolean ifUserMustChangePassword () {
208                 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
209         }
210
211         @Override
212         public boolean isInvisible () {
213                 // Check on login
214                 if (!this.isUserLoggedIn()) {
215                         // Not logged in!
216                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
217                 }
218
219                 // Check logged-in first, then invisibility
220                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
221         }
222
223         @Override
224         public boolean isUserLoggedIn () {
225                 // Trace message
226                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
227
228                 // Compare instance
229                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
230
231                 // Trace message
232                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
233
234                 // Return it
235                 return this.userLoggedIn;
236         }
237
238 }