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