2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.login;
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;
50 * A web bean for user registration
52 * @author Roland Haeder<roland@mxchange.org>
54 @Named ("loginController")
56 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
59 * Path name for guest base template
61 private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; //NOI18N
64 * Path name for logged-in user base template
66 private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; //NOI18N
71 private static final long serialVersionUID = 47_828_986_719_691_592L;
74 * Template type for pages that might be displayed in guest area and login
77 private String baseTemplatePathName;
82 private String currentPassword;
85 * Logged-in user instance
87 private User loggedInUser;
90 * Remote register session bean
92 private UserLoginSessionBeanRemote loginBean;
95 * Event fired when user has logged in
99 private Event<UserLoggedInEvent> loginEvent;
105 private AddressbookUserWebSessionController userController;
108 * Flag whether the user has logged-in, set only from inside
110 private boolean userLoggedIn;
113 * Event fired when user has logged in
117 private Event<UserLoggedInEvent> userLoginEvent;
120 * Event fired when user has logged out
124 private Event<ObserveableUserLogoutEvent> userLogoutEvent;
127 * Default constructor
129 public AddressbookUserLoginWebSessionBean () {
131 // Get initial context
132 Context context = new InitialContext();
135 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
137 // Defaul template is guest
138 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
139 } catch (final NamingException ex) {
141 throw new FaceletException(ex);
146 public String doAdminLogout () {
147 // Is a user logged-in?
148 if (this.isUserLoggedIn()) {
150 return this.doUserLogout();
153 // Invalidate session
154 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
156 // Set template type to guest
157 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
160 return "index?faces-redirect=true"; //NOI18N
164 public String doUserLogin () {
166 User user = this.userController.createUserLogin();
168 // Create login container
169 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
173 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
175 // All fine here so set it here
176 this.setLoggedInUser(confirmedUser);
178 // Set template to "login"
179 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
181 // Fire event away. Keep this last before return statement.
182 this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
188 return "login"; //NOI18N
189 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
191 throw new FaceletException(ex);
196 public String doUserLogout () {
197 // Is loggedInUser set?
198 if (this.getLoggedInUser() == null) {
200 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
201 } else if (this.getLoggedInUser().getUserId() == null) {
203 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
204 } else if (this.getLoggedInUser().getUserId() < 1) {
206 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
210 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
212 // Invalidate session
213 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
215 // Unset any user instances
216 this.setLoggedInUser(null);
217 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
220 return "index"; //NOI18N
224 public String getBaseTemplatePathName () {
225 return this.baseTemplatePathName;
229 public void setBaseTemplatePathName (final String baseTemplatePathName) {
230 this.baseTemplatePathName = baseTemplatePathName;
234 public String getCurrentPassword () {
235 return this.currentPassword;
239 public void setCurrentPassword (final String currentPassword) {
240 this.currentPassword = currentPassword;
244 public User getLoggedInUser () {
245 return this.loggedInUser;
249 public void setLoggedInUser (final User loggedInUser) {
250 this.loggedInUser = loggedInUser;
254 public boolean ifCurrentPasswordMatches () {
255 // The current password must be set and not empty
256 if (this.getCurrentPassword() == null) {
258 throw new NullPointerException("this.currentPassword is null"); //NOI18N
259 } else if (this.getCurrentPassword().isEmpty()) {
261 throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
264 // Create "container"
265 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
267 // Now check if it matches
268 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
272 public boolean ifUserMustChangePassword () {
273 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
277 public boolean isInvisible () {
279 if (!this.isUserLoggedIn()) {
281 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
284 // Check logged-in first, then invisibility
285 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
289 public boolean isUserLoggedIn () {
290 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
293 return this.userLoggedIn;
299 private void clear () {
301 this.setCurrentPassword(null);