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