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.jjobs.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.jjobs.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;
58 * Logged-in user instance
60 private User loggedInUser;
63 * Reemote register session bean
65 private UserLoginSessionBeanRemote loginBean;
68 * Event fired when user has logged in
72 private Event<UserLoggedInEvent> loginEvent;
75 * Template type for pages that might be displayed in guest area and login
76 * area. Default is guest area.
78 private String templateType = "guest"; //NOI18N
84 private UserWebSessionController userController;
87 * Flag whether the user has logged-in, set only from inside
89 private boolean userLoggedIn;
94 public UserLoginWebSessionBean () {
96 // Get initial context
97 Context context = new InitialContext();
100 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("ejb/stateless-login"); //NOI18N
101 } catch (final NamingException ex) {
103 throw new FaceletException(ex);
108 public String doLogin () {
110 User user = this.userController.createUserInstance();
112 // Create login container
113 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
117 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
119 // All fine here so set it here
120 this.setLoggedInUser(confirmedUser);
122 // Set template to "login"
123 this.setTemplateType("login"); //NOI18N
125 // Fire event away. Keep this last before return statement.
126 this.loginEvent.fire(new UserLoginEvent(confirmedUser));
129 return "login"; //NOI18N
130 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
132 throw new FaceletException(ex);
137 public User getLoggedInUser () {
138 return this.loggedInUser;
142 public void setLoggedInUser (final User loggedInUser) {
143 this.loggedInUser = loggedInUser;
147 public String getTemplateType () {
148 return this.templateType;
152 public void setTemplateType (final String templateType) {
153 this.templateType = templateType;
157 public boolean isGuest () {
158 return (!this.isUserLoggedIn());
162 public boolean isInvisible () {
164 if (!this.isUserLoggedIn()) {
166 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
169 // Check logged-in first, then invisibility
170 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
174 public boolean isUserLoggedIn () {
176 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
179 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
182 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
185 return this.userLoggedIn;