]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/login/UserLoginWebBean.java
Continued:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / login / UserLoginWebBean.java
1 /*
2  * Copyright (C) 2015 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.addressbook.beans.login;
18
19 import java.util.Objects;
20 import javax.enterprise.context.SessionScoped;
21 import javax.faces.view.facelets.FaceletException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.addressbook.beans.user.UserWebController;
28 import org.mxchange.jusercore.container.login.LoginContainer;
29 import org.mxchange.jusercore.container.login.UserLoginContainer;
30 import org.mxchange.jusercore.exceptions.UserNotFoundException;
31 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
32 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
33 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
34 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
37
38 /**
39  * A web bean for user registration
40  * <p>
41  * @author Roland Haeder
42  */
43 @Named ("loginController")
44 @SessionScoped
45 public class UserLoginWebBean implements UserLoginWebController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 47_828_986_719_691_592L;
51
52         /**
53          * Logged-in user instance
54          */
55         private User loggedInUser;
56
57         /**
58          * Reemote register session bean
59          */
60         private UserLoginSessionBeanRemote loginBean;
61
62         /**
63          * User controller
64          */
65         @Inject
66         private UserWebController userController;
67
68         /**
69          * Default constructor
70          */
71         public UserLoginWebBean () {
72                 try {
73                         // Get initial context
74                         Context context = new InitialContext();
75
76                         // Try to lookup
77                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("ejb/stateless-login"); //NOI18N
78                 } catch (final NamingException ex) {
79                         // Continue to throw
80                         throw new FaceletException(ex);
81                 }
82         }
83
84         @Override
85         public String doLogin () {
86                 // Get user instance
87                 User user = this.userController.createUserInstance();
88
89                 // Create login container
90                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
91
92                 try {
93                         // Call bean
94                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
95
96                         // All fine here so set it here
97                         this.setLoggedInUser(confirmedUser);
98
99                         // All fine
100                         return "login"; //NOI18N
101                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
102                         // Throw again
103                         throw new FaceletException(ex);
104                 }
105         }
106
107         @Override
108         public User getLoggedInUser () {
109                 return this.loggedInUser;
110         }
111
112         @Override
113         public void setLoggedInUser (final User loggedInUser) {
114                 this.loggedInUser = loggedInUser;
115         }
116
117         @Override
118         public boolean isUserLoggedIn () {
119                 // Compare instance
120                 return ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
121         }
122 }