]> 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 | UserPasswordMismatchException ex) {
148                         // Throw again
149                         throw new FaceletException(ex);
150                 }
151         }
152
153         @Override
154         public String getCurrentPassword () {
155                 return this.currentPassword;
156         }
157
158         @Override
159         public void setCurrentPassword (final String currentPassword) {
160                 this.currentPassword = currentPassword;
161         }
162
163         @Override
164         public User getLoggedInUser () {
165                 return this.loggedInUser;
166         }
167
168         @Override
169         public void setLoggedInUser (final User loggedInUser) {
170                 this.loggedInUser = loggedInUser;
171         }
172
173         @Override
174         public String getTemplateType () {
175                 return this.templateType;
176         }
177
178         @Override
179         public void setTemplateType (final String templateType) {
180                 this.templateType = templateType;
181         }
182
183         @Override
184         public boolean ifCurrentPasswordMatches () {
185                 // The current password must be set and not empty
186                 if (this.getCurrentPassword() == null) {
187                         // Is not set
188                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
189                 } else if (this.getCurrentPassword().isEmpty()) {
190                         // Is set empty
191                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
192                 }
193
194                 // Create "container"
195                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
196
197                 // Now check if it matches
198                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
199         }
200
201         @Override
202         public boolean ifUserMustChangePassword () {
203                 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
204         }
205
206         @Override
207         public boolean isInvisible () {
208                 // Check on login
209                 if (!this.isUserLoggedIn()) {
210                         // Not logged in!
211                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
212                 }
213
214                 // Check logged-in first, then invisibility
215                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
216         }
217
218         @Override
219         public boolean isUserLoggedIn () {
220                 // Trace message
221                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
222
223                 // Compare instance
224                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
225
226                 // Trace message
227                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
228
229                 // Return it
230                 return this.userLoggedIn;
231         }
232
233 }