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 General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU 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.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.user.UserWebSessionController;
30 import org.mxchange.jusercore.container.login.LoginContainer;
31 import org.mxchange.jusercore.container.login.UserLoginContainer;
32 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
33 import org.mxchange.jusercore.events.login.UserLoginEvent;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
36 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
37 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
38 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.User;
40 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44 * A web bean for user registration
46 * @author Roland Haeder
48 @Named ("loginController")
50 public class UserLoginWebSessionBean implements UserLoginWebSessionController {
55 private static final long serialVersionUID = 47_828_986_719_691_592L;
60 private String currentPassword;
63 * Logged-in user instance
65 private User loggedInUser;
68 * Remote register session bean
70 private UserLoginSessionBeanRemote loginBean;
73 * Event fired when user has logged in
77 private Event<UserLoggedInEvent> loginEvent;
80 * Template type for pages that might be displayed in guest area and login
81 * area. Default is guest area.
83 private String templateType = "guest"; //NOI18N
89 private UserWebSessionController userController;
92 * Flag whether the user has logged-in, set only from inside
94 private boolean userLoggedIn;
99 public UserLoginWebSessionBean () {
101 // Get initial context
102 Context context = new InitialContext();
105 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("ejb/stateless-addressbook-login"); //NOI18N
106 } catch (final NamingException ex) {
108 throw new FaceletException(ex);
113 public String doLogin () {
115 User user = this.userController.createUserInstance();
117 // Create login container
118 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
122 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
124 // All fine here so set it here
125 this.setLoggedInUser(confirmedUser);
127 // Set template to "login"
128 this.setTemplateType("login"); //NOI18N
130 // Fire event away. Keep this last before return statement.
131 this.loginEvent.fire(new UserLoginEvent(confirmedUser));
134 return "login"; //NOI18N
135 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
137 throw new FaceletException(ex);
142 public String getCurrentPassword () {
143 return currentPassword;
147 public void setCurrentPassword (final String currentPassword) {
148 this.currentPassword = currentPassword;
152 public User getLoggedInUser () {
153 return this.loggedInUser;
157 public void setLoggedInUser (final User loggedInUser) {
158 this.loggedInUser = loggedInUser;
162 public String getTemplateType () {
163 return this.templateType;
167 public void setTemplateType (final String templateType) {
168 this.templateType = templateType;
172 public boolean isGuest () {
173 return (!this.isUserLoggedIn());
177 public boolean isInvisible () {
179 if (!this.isUserLoggedIn()) {
181 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
184 // Check logged-in first, then invisibility
185 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
189 public boolean isUserLoggedIn () {
191 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
194 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
197 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
200 return this.userLoggedIn;