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 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.
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.
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/>.
17 package org.mxchange.jjobs.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.inject.Any;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
36 import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
37 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
38 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
39 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
40 import org.mxchange.jusercore.exceptions.UserNotFoundException;
41 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
42 import org.mxchange.jusercore.model.user.LoginUser;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
45 import org.mxchange.jusercore.model.user.UserUtils;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
50 * A user bean (controller)
52 * @author Roland Haeder<roland@mxchange.org>
54 @Named ("adminUserController")
56 public class JobsAdminUserWebRequestBean implements JobsAdminUserWebRequestController {
61 private static final long serialVersionUID = 542_145_347_916L;
64 * An event fired when the administrator has added a new user
68 private Event<AdminAddedUserEvent> addedUserEvent;
71 * Regular contact controller
74 private JobsContactWebSessionController contactController;
79 private final UserSessionBeanRemote userBean;
82 * A list of all user profiles
84 private List<User> userList;
89 private String userName;
92 * User password (unencrypted from web form)
94 private String userPassword;
97 * User password repeated (unencrypted from web form)
99 private String userPasswordRepeat;
102 <<<<<<< HEAD:src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebSessionBean.java
105 private Integer zipCode;
108 * Regular user controller
111 private JobsUserWebSessionController userController;
114 * Default constructor
116 public JobsAdminUserWebRequestBean () {
119 // Get initial context
120 Context context = new InitialContext();
123 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
124 } catch (final NamingException e) {
126 throw new FaceletException(e);
131 public void addUser () {
132 // Create new user instance
133 User localUser = new LoginUser();
135 // Set user name, CONFIRMED and INVISIBLE
136 localUser.setUserName(this.getUserName());
137 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
138 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
140 // Create contact instance
141 Contact contact = this.contactController.createContactInstance();
143 // Set contact in user
144 localUser.setUserContact(contact);
146 // Init variable for password
147 String password = null;
149 // Is the user name or email address used already?
150 // @TODO Add password length check
151 if (this.userController.isUserNameRegistered(localUser)) {
152 // User name is already used
153 throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
154 } else if (this.contactController.isEmailAddressRegistered(localUser.getUserContact())) {
155 // Email address is already used
156 throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
157 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
158 // Empty password entered, then generate one
159 password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
160 } else if (!this.isSamePasswordEntered()) {
161 // Both passwords don't match
162 throw new FaceletException(new UserPasswordRepeatMismatchException(localUser));
164 // Both match, so get it from this bean
165 password = this.getUserPassword();
168 // The password should not be null and at least 5 characters long
169 assert (password != null) : "password is null"; //NOI18N
170 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
172 // Encrypt password and set it
173 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
175 // Init updated user instance
176 User updatedUser = null;
179 // Now, that all is set, call EJB
180 updatedUser = this.userBean.addUser(localUser);
181 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
183 throw new FaceletException(ex);
187 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
189 // Add user to local list
190 this.userList.add(updatedUser);
195 // Clear contact instance
196 this.contactController.clear();
200 public List<User> allUsers () {
202 return Collections.unmodifiableList(this.userList);
206 public String getUserName () {
207 return this.userName;
211 public void setUserName (final String userName) {
212 this.userName = userName;
216 public String getUserPassword () {
217 return this.userPassword;
221 public void setUserPassword (final String userPassword) {
222 this.userPassword = userPassword;
226 public String getUserPasswordRepeat () {
227 return this.userPasswordRepeat;
231 public void setUserPasswordRepeat (final String userPasswordRepeat) {
232 this.userPasswordRepeat = userPasswordRepeat;
236 public boolean hasUsers () {
237 return (!this.allUsers().isEmpty());
241 * Post-initialization of this class
244 public void init () {
245 // Initialize user list
246 this.userList = this.userBean.allUsers();
250 public User lookupUserById (final Long userId) throws UserNotFoundException {
251 // Parameter must be valid
252 if (null == userId) {
254 throw new NullPointerException("userId is null"); //NOI18N
255 } else if (userId < 1) {
257 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
263 // Try to lookup it in visible user list
264 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
266 User next = iterator.next();
268 // Is the user id found?
269 if (Objects.equals(next.getUserId(), userId)) {
270 // Copy to other variable
278 // Not visible for the current user
279 throw new UserNotFoundException(userId);
289 private void clear () {
291 this.setUserName(null);
292 this.setUserPassword(null);
293 this.setUserPasswordRepeat(null);
297 * Checks if same password is entered and that they are not empty.
299 * @return Whether the same password was entered
301 private boolean isSamePasswordEntered () {
302 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));