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