]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/login/UserLoginWebSessionBean.java
renamed packages (addressbook import) + added event for logging in
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / login / UserLoginWebSessionBean.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 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.
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 General Public License for more details.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.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.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;
42
43 /**
44  * A web bean for user registration
45  * <p>
46  * @author Roland Haeder
47  */
48 @Named ("loginController")
49 @SessionScoped
50 public class UserLoginWebSessionBean implements UserLoginWebSessionController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 47_828_986_719_691_592L;
56
57         /**
58          * Logged-in user instance
59          */
60         private User loggedInUser;
61
62         /**
63          * Reemote register session bean
64          */
65         private UserLoginSessionBeanRemote loginBean;
66
67         /**
68          * Event fired when user has logged in
69          */
70         @Inject
71         @Any
72         private Event<UserLoggedInEvent> loginEvent;
73
74         /**
75          * Template type for pages that might be displayed in guest area and login
76          * area. Default is guest area.
77          */
78         private String templateType = "guest"; //NOI18N
79
80         /**
81          * User controller
82          */
83         @Inject
84         private UserWebSessionController userController;
85
86         /**
87          * Flag whether the user has logged-in, set only from inside
88          */
89         private boolean userLoggedIn;
90
91         /**
92          * Default constructor
93          */
94         public UserLoginWebSessionBean () {
95                 try {
96                         // Get initial context
97                         Context context = new InitialContext();
98
99                         // Try to lookup
100                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("ejb/stateless-login"); //NOI18N
101                 } catch (final NamingException ex) {
102                         // Continue to throw
103                         throw new FaceletException(ex);
104                 }
105         }
106
107         @Override
108         public String doLogin () {
109                 // Get user instance
110                 User user = this.userController.createUserInstance();
111
112                 // Create login container
113                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
114
115                 try {
116                         // Call bean
117                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
118
119                         // All fine here so set it here
120                         this.setLoggedInUser(confirmedUser);
121
122                         // Set template to "login"
123                         this.setTemplateType("login"); //NOI18N
124
125                         // Fire event away. Keep this last before return statement.
126                         this.loginEvent.fire(new UserLoginEvent(confirmedUser));
127
128                         // All fine
129                         return "login"; //NOI18N
130                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
131                         // Throw again
132                         throw new FaceletException(ex);
133                 }
134         }
135
136         @Override
137         public User getLoggedInUser () {
138                 return this.loggedInUser;
139         }
140
141         @Override
142         public void setLoggedInUser (final User loggedInUser) {
143                 this.loggedInUser = loggedInUser;
144         }
145
146         @Override
147         public String getTemplateType () {
148                 return this.templateType;
149         }
150
151         @Override
152         public void setTemplateType (final String templateType) {
153                 this.templateType = templateType;
154         }
155
156         @Override
157         public boolean isGuest () {
158                 return (!this.isUserLoggedIn());
159         }
160
161         @Override
162         public boolean isInvisible () {
163                 // Check on login
164                 if (!this.isUserLoggedIn()) {
165                         // Not logged in!
166                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
167                 }
168
169                 // Check logged-in first, then invisibility
170                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
171         }
172
173         @Override
174         public boolean isUserLoggedIn () {
175                 // Trace message
176                 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
177
178                 // Compare instance
179                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
180
181                 // Trace message
182                 // NOISY: System.out.println(MessageFormat.format("UserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
183
184                 // Return it
185                 return this.userLoggedIn;
186         }
187 }