]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/login/AddressbookUserLoginWebSessionBean.java
Continued a bit: (please cherry-pick)
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / login / AddressbookUserLoginWebSessionBean.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.addressbook.beans.login;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.enterprise.context.SessionScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.context.FacesContext;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.addressbook.beans.BaseAddressbookController;
32 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
33 import org.mxchange.jusercore.container.login.LoginContainer;
34 import org.mxchange.jusercore.container.login.UserLoginContainer;
35 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
36 import org.mxchange.jusercore.events.login.UserLoginEvent;
37 import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
38 import org.mxchange.jusercore.events.logout.UserLogoutEvent;
39 import org.mxchange.jusercore.exceptions.UserNotFoundException;
40 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
41 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
42 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
43 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.UserUtils;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
48
49 /**
50  * A web bean for user registration
51  * <p>
52  * @author Roland Haeder<roland@mxchange.org>
53  */
54 @Named ("loginController")
55 @SessionScoped
56 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
57
58         /**
59          * Path name for guest base template
60          */
61         private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; //NOI18N
62
63         /**
64          * Path name for logged-in user base template
65          */
66         private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; //NOI18N
67
68         /**
69          * Serial number
70          */
71         private static final long serialVersionUID = 47_828_986_719_691_592L;
72
73         /**
74          * Template type for pages that might be displayed in guest area and login
75          * area.
76          */
77         private String baseTemplatePathName;
78
79         /**
80          * Current password
81          */
82         private String currentPassword;
83
84         /**
85          * Logged-in user instance
86          */
87         private User loggedInUser;
88
89         /**
90          * Remote register session bean
91          */
92         private UserLoginSessionBeanRemote loginBean;
93
94         /**
95          * Event fired when user has logged in
96          */
97         @Inject
98         @Any
99         private Event<UserLoggedInEvent> loginEvent;
100
101         /**
102          * User controller
103          */
104         @Inject
105         private AddressbookUserWebSessionController userController;
106
107         /**
108          * Flag whether the user has logged-in, set only from inside
109          */
110         private boolean userLoggedIn;
111
112         /**
113          * Event fired when user has logged in
114          */
115         @Inject
116         @Any
117         private Event<UserLoggedInEvent> userLoginEvent;
118
119         /**
120          * Event fired when user has logged out
121          */
122         @Inject
123         @Any
124         private Event<ObserveableUserLogoutEvent> userLogoutEvent;
125
126         /**
127          * Default constructor
128          */
129         public AddressbookUserLoginWebSessionBean () {
130                 try {
131                         // Get initial context
132                         Context context = new InitialContext();
133
134                         // Try to lookup
135                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
136
137                         // Defaul template is guest
138                         this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
139                 } catch (final NamingException ex) {
140                         // Continue to throw
141                         throw new FaceletException(ex);
142                 }
143         }
144
145         @Override
146         public String doAdminLogout () {
147                 // Is a user logged-in?
148                 if (this.isUserLoggedIn()) {
149                         // Call other logout
150                         return this.doUserLogout();
151                 }
152
153                 // Invalidate session
154                 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
155
156                 // Set template type to guest
157                 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
158
159                 // Redirect to index
160                 return "index?faces-redirect=true"; //NOI18N
161         }
162
163         @Override
164         public String doUserLogin () {
165                 // Get user instance
166                 User user = this.userController.createUserLogin();
167
168                 // Create login container
169                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
170
171                 try {
172                         // Call bean
173                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
174
175                         // All fine here so set it here
176                         this.setLoggedInUser(confirmedUser);
177
178                         // Set template to "login"
179                         this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
180
181                         // Fire event away. Keep this last before return statement.
182                         this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
183
184                         // Clear this bean
185                         this.clear();
186
187                         // All fine
188                         return "login"; //NOI18N
189                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException ex) {
190                         // Show JSF message
191                         this.showFacesMessage("form_user_login:userName", ex); //NOI18N
192                         return ""; //NOI18N
193                 } catch (final UserPasswordMismatchException ex) {
194                         // Show JSF message
195                         this.showFacesMessage("form_user_login:userPassword", ex); //NOI18N
196                         return ""; //NOI18N
197                 }
198         }
199
200         @Override
201         public String doUserLogout () {
202                 // Is loggedInUser set?
203                 if (this.getLoggedInUser() == null) {
204                         // Throw NPE
205                         throw new NullPointerException("this.loggedInUser is null"); //NOI18N
206                 } else if (this.getLoggedInUser().getUserId() == null) {
207                         // Throw again
208                         throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
209                 } else if (this.getLoggedInUser().getUserId() < 1) {
210                         // Invalid user id
211                         throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
212                 }
213
214                 // Fire event
215                 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
216
217                 // Invalidate session
218                 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
219
220                 // Unset any user instances
221                 this.setLoggedInUser(null);
222                 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
223
224                 // Redirect to index
225                 return "index"; //NOI18N
226         }
227
228         @Override
229         public String getBaseTemplatePathName () {
230                 return this.baseTemplatePathName;
231         }
232
233         @Override
234         public void setBaseTemplatePathName (final String baseTemplatePathName) {
235                 this.baseTemplatePathName = baseTemplatePathName;
236         }
237
238         @Override
239         public String getCurrentPassword () {
240                 return this.currentPassword;
241         }
242
243         @Override
244         public void setCurrentPassword (final String currentPassword) {
245                 this.currentPassword = currentPassword;
246         }
247
248         @Override
249         public User getLoggedInUser () {
250                 return this.loggedInUser;
251         }
252
253         @Override
254         public void setLoggedInUser (final User loggedInUser) {
255                 this.loggedInUser = loggedInUser;
256         }
257
258         @Override
259         public boolean ifCurrentPasswordMatches () {
260                 // The current password must be set and not empty
261                 if (this.getCurrentPassword() == null) {
262                         // Is not set
263                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
264                 } else if (this.getCurrentPassword().isEmpty()) {
265                         // Is set empty
266                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
267                 }
268
269                 // Create "container"
270                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
271
272                 // Now check if it matches
273                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
274         }
275
276         @Override
277         public boolean ifUserMustChangePassword () {
278                 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
279         }
280
281         @Override
282         public boolean isInvisible () {
283                 // Check on login
284                 if (!this.isUserLoggedIn()) {
285                         // Not logged in!
286                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
287                 }
288
289                 // Check logged-in first, then invisibility
290                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
291         }
292
293         @Override
294         public boolean isUserLoggedIn () {
295                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
296
297                 // Return it
298                 return this.userLoggedIn;
299         }
300
301         /**
302          * Clears this bean
303          */
304         private void clear () {
305                 // Clear all fields
306                 this.setCurrentPassword(null);
307         }
308
309 }