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.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;
49 * A web bean for user registration
51 * @author Roland Haeder<roland@mxchange.org>
53 @Named ("loginController")
55 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
60 private static final long serialVersionUID = 47_828_986_719_691_592L;
65 private String currentPassword;
68 * Logged-in user instance
70 private User loggedInUser;
73 * Remote register session bean
75 private UserLoginSessionBeanRemote loginBean;
78 * Event fired when user has logged in
82 private Event<UserLoggedInEvent> loginEvent;
85 * Template type for pages that might be displayed in guest area and login
86 * area. Default is guest area.
88 private String templateType = "guest"; //NOI18N
94 private AddressbookUserWebSessionController userController;
97 * Flag whether the user has logged-in, set only from inside
99 private boolean userLoggedIn;
102 * Event fired when user has logged in
106 private Event<UserLoggedInEvent> userLoginEvent;
109 * Event fired when user has logged out
113 private Event<ObserveableUserLogoutEvent> userLogoutEvent;
116 * Default constructor
118 public AddressbookUserLoginWebSessionBean () {
120 // Get initial context
121 Context context = new InitialContext();
124 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
125 } catch (final NamingException ex) {
127 throw new FaceletException(ex);
132 public String doLogin () {
134 User user = this.userController.createUserLogin();
136 // Create login container
137 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
141 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
143 // All fine here so set it here
144 this.setLoggedInUser(confirmedUser);
146 // Set template to "login"
147 this.setTemplateType("login"); //NOI18N
149 // Fire event away. Keep this last before return statement.
150 this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
156 return "login"; //NOI18N
157 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
159 throw new FaceletException(ex);
164 public void setBaseTemplatePathName (final String baseTemplatePathName) {
165 this.baseTemplatePathName = baseTemplatePathName;
169 public String doUserLogout () {
171 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
173 // Invalidate session
174 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
176 // Unset any user instances
177 this.setLoggedInUser(null);
180 return "index?faces-redirect=true"; //NOI18N
184 public String getCurrentPassword () {
185 return this.currentPassword;
189 public void setCurrentPassword (final String currentPassword) {
190 this.currentPassword = currentPassword;
194 public User getLoggedInUser () {
195 return this.loggedInUser;
199 public void setLoggedInUser (final User loggedInUser) {
200 this.loggedInUser = loggedInUser;
204 public String getTemplateType () {
205 return this.templateType;
209 public void setTemplateType (final String templateType) {
210 this.templateType = templateType;
214 public boolean ifCurrentPasswordMatches () {
215 // The current password must be set and not empty
216 if (this.getCurrentPassword() == null) {
218 throw new NullPointerException("this.currentPassword is null"); //NOI18N
219 } else if (this.getCurrentPassword().isEmpty()) {
221 throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
224 // Create "container"
225 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
227 // Now check if it matches
228 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
232 public boolean isGuest () {
233 return (!this.isUserLoggedIn());
237 public boolean isInvisible () {
239 if (!this.isUserLoggedIn()) {
241 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
244 // Check logged-in first, then invisibility
245 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
249 public boolean isUserLoggedIn () {
251 // NOISY: System.out.println(MessageFormat.format("AddressbookUserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
254 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
257 // NOISY: System.out.println(MessageFormat.format("AddressbookUserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
260 return this.userLoggedIn;
266 private void clear () {
268 this.setCurrentPassword(null);