]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/login/PizzaUserLoginWebSessionBean.java
Rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / login / PizzaUserLoginWebSessionBean.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 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.
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 Affero General Public License for more details.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.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.jusercore.container.login.LoginContainer;
30 import org.mxchange.jusercore.container.login.UserLoginContainer;
31 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
32 import org.mxchange.jusercore.events.login.UserLoginEvent;
33 import org.mxchange.jusercore.exceptions.UserNotFoundException;
34 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
35 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
36 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
37 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserUtils;
40 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
43
44 /**
45  * A web bean for user registration
46  * <p>
47  * @author Roland Haeder<roland@mxchange.org>
48  */
49 @Named ("loginController")
50 @SessionScoped
51 public class PizzaUserLoginWebSessionBean implements PizzaUserLoginWebSessionController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 47_828_986_719_691_592L;
57
58         /**
59          * Current password
60          */
61         private String currentPassword;
62
63         /**
64          * Logged-in user instance
65          */
66         private User loggedInUser;
67
68         /**
69          * Remote register session bean
70          */
71         private UserLoginSessionBeanRemote loginBean;
72
73         /**
74          * Event fired when user has logged in
75          */
76         @Inject
77         @Any
78         private Event<UserLoggedInEvent> loginEvent;
79
80         /**
81          * Template type for pages that might be displayed in guest area and login
82          * area. Default is guest area.
83          */
84         private String templateType = "guest"; //NOI18N
85
86         /**
87          * User controller
88          */
89         @Inject
90         private PizzaUserWebSessionController userController;
91
92         /**
93          * Flag whether the user has logged-in, set only from inside
94          */
95         private boolean userLoggedIn;
96
97         /**
98          * Default constructor
99          */
100         public PizzaUserLoginWebSessionBean () {
101                 try {
102                         // Get initial context
103                         Context context = new InitialContext();
104
105                         // Try to lookup
106                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jrecruiter-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
107                 } catch (final NamingException ex) {
108                         // Continue to throw
109                         throw new FaceletException(ex);
110                 }
111         }
112
113         @Override
114         public String doLogin () {
115                 // Get user instance
116                 User user = this.userController.createUserInstance();
117
118                 // Create login container
119                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
120
121                 try {
122                         // Call bean
123                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
124
125                         // All fine here so set it here
126                         this.setLoggedInUser(confirmedUser);
127
128                         // Set template to "login"
129                         this.setTemplateType("login"); //NOI18N
130
131                         // Fire event away. Keep this last before return statement.
132                         this.loginEvent.fire(new UserLoginEvent(confirmedUser));
133
134                         // All fine
135                         return "login"; //NOI18N
136                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
137                         // Throw again
138                         throw new FaceletException(ex);
139                 }
140         }
141
142         @Override
143         public String getCurrentPassword () {
144                 return this.currentPassword;
145         }
146
147         @Override
148         public void setCurrentPassword (final String currentPassword) {
149                 this.currentPassword = currentPassword;
150         }
151
152         @Override
153         public User getLoggedInUser () {
154                 return this.loggedInUser;
155         }
156
157         @Override
158         public void setLoggedInUser (final User loggedInUser) {
159                 this.loggedInUser = loggedInUser;
160         }
161
162         @Override
163         public String getTemplateType () {
164                 return this.templateType;
165         }
166
167         @Override
168         public void setTemplateType (final String templateType) {
169                 this.templateType = templateType;
170         }
171
172         @Override
173         public boolean ifCurrentPasswordMatches () {
174                 // The current password must be set and not empty
175                 if (this.getCurrentPassword() == null) {
176                         // Is not set
177                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
178                 } else if (this.getCurrentPassword().isEmpty()) {
179                         // Is set empty
180                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
181                 }
182
183                 // Create "container"
184                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
185
186                 // Now check if it matches
187                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
188         }
189
190         @Override
191         public boolean isGuest () {
192                 return (!this.isUserLoggedIn());
193         }
194
195         @Override
196         public boolean isInvisible () {
197                 // Check on login
198                 if (!this.isUserLoggedIn()) {
199                         // Not logged in!
200                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
201                 }
202
203                 // Check logged-in first, then invisibility
204                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
205         }
206
207         @Override
208         public boolean isUserLoggedIn () {
209                 // Trace message
210                 // NOISY-DEBUG System.out.println(MessageFormat.format("RecruiterUserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
211
212                 // Compare instance
213                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
214
215                 // Trace message
216                 // NOISY-DEBUG System.out.println(MessageFormat.format("RecruiterUserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
217
218                 // Return it
219                 return this.userLoggedIn;
220         }
221 }