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.Objects;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jjobs.beans.BaseJobsController;
33 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
34 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
35 import org.mxchange.jusercore.container.login.UserLoginContainer;
36 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
37 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
38 import org.mxchange.jusercore.events.user.add.AdminUserAddedEvent;
39 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
40 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
41 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
42 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
43 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
44 import org.mxchange.jusercore.model.user.LoginUser;
45 import org.mxchange.jusercore.model.user.User;
46 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
47 import org.mxchange.jusercore.model.user.UserUtils;
48 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
49 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
50 import org.mxchange.jjobs.beans.helper.JobsWebRequestController;
53 * A user bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("adminUserController")
59 public class JobsAdminUserWebRequestBean extends BaseJobsController implements JobsAdminUserWebRequestController {
64 private static final long serialVersionUID = 542_145_347_916L;
67 * An event fired when the administrator has added a new user
71 private Event<AdminAddedUserEvent> addedUserEvent;
74 * Bean helper instance
77 private JobsWebRequestController beanHelper;
80 * Regular contact controller
83 private JobsContactWebSessionController contactController;
86 * An event fired when the administrator has updated a new user
90 private Event<AdminUpdatedUserDataEvent> updatedUserDataEvent;
95 private final UserSessionBeanRemote userBean;
98 * Regular user controller
101 private JobsUserWebSessionController userController;
106 private String userLockReason;
109 * Flag whether user must change password after login
111 private Boolean userMustChangePassword;
116 private String userName;
119 * User password (unencrypted from web form)
121 private String userPassword;
124 * User password repeated (unencrypted from web form)
126 private String userPasswordRepeat;
129 * Default constructor
131 public JobsAdminUserWebRequestBean () {
134 // Get initial context
135 Context context = new InitialContext();
138 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
139 } catch (final NamingException e) {
141 throw new FaceletException(e);
146 public String addUser () {
147 // As the form cannot validate the data (required="true"), check it here
148 if (this.getUserName() == null) {
150 throw new NullPointerException("userName is null"); //NOI18N
151 } else if (this.getUserName().isEmpty()) {
153 throw new IllegalArgumentException("userName is null"); //NOI18N
154 } else if (this.beanHelper.getContact() == null) {
155 // No contact instance set, so test required fields: gender, first name and family name
156 if (this.contactController.getGender() == null) {
158 throw new NullPointerException("contactController.gender is null"); //NOI18N
159 } else if (this.contactController.getFirstName() == null) {
161 throw new NullPointerException("contactController.firstName is null"); //NOI18N //NOI18N
162 } else if (this.contactController.getFirstName().isEmpty()) {
164 throw new IllegalArgumentException("contactController.firstName is empty");
165 } else if (this.contactController.getFamilyName() == null) {
167 throw new NullPointerException("contactController.familyName is null"); //NOI18N
168 } else if (this.contactController.getFamilyName().isEmpty()) {
170 throw new IllegalArgumentException("contactController.familyName is empty"); //NOI18N //NOI18N
171 } else if (this.contactController.getEmailAddress() == null) {
173 throw new NullPointerException("contactController.emailAddress is null");
174 } else if (this.contactController.getEmailAddress().isEmpty()) {
176 throw new IllegalArgumentException("contactController.emailAddress is empty"); //NOI18N //NOI18N
177 } else if (this.contactController.getEmailAddressRepeat() == null) {
179 throw new NullPointerException("contactController.emailAddressRepeat is null");
180 } else if (this.contactController.getEmailAddressRepeat().isEmpty()) {
182 throw new IllegalArgumentException("contactController.emailAddressRepeat is empty"); //NOI18N //NOI18N
183 } else if (!Objects.equals(this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat())) {
184 // Is not same email address
185 throw new IllegalArgumentException("Both entered email addresses don't match.");
189 // Create new user instance
190 User user = new LoginUser();
192 // Set user name, CONFIRMED and INVISIBLE
193 user.setUserName(this.getUserName());
194 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
195 user.setUserProfileMode(ProfileMode.INVISIBLE);
200 // Is a contact instance in helper set?
201 if (this.beanHelper.getContact() instanceof Contact) {
202 // Then use it for contact linking
203 contact = this.beanHelper.getContact();
205 // Create contact instance
206 contact = this.contactController.createContactInstance();
209 // Set contact in user
210 user.setUserContact(contact);
212 // Init variable for password
213 String password = null;
215 // Is the user name or email address used already?
216 // @TODO Add password length check
217 if (this.userController.isUserNameRegistered(user)) {
218 // User name is already used
219 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
220 } else if ((this.beanHelper.getContact() == null) && (this.contactController.isEmailAddressRegistered(user.getUserContact()))) {
221 // Email address is already used
222 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
223 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
224 // Empty password entered, then generate one
225 password = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
226 } else if (!this.isSamePasswordEntered()) {
227 // Both passwords don't match
228 throw new FaceletException(new UserPasswordRepeatMismatchException(user));
230 // Both match, so get it from this bean
231 password = this.getUserPassword();
234 // The password should not be null and at least 5 characters long
235 assert (password != null) : "password is null"; //NOI18N
236 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
238 // Encrypt password and set it
239 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
241 // Init updated user instance
242 User updatedUser = null;
245 // Now, that all is set, call EJB
246 if (this.beanHelper.getContact() instanceof Contact) {
247 // Link contact with this user
248 updatedUser = this.userBean.linkUser(user);
250 // Remove contact instance
251 this.beanHelper.setContact(null);
254 updatedUser = this.userBean.addUser(user);
256 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
258 throw new FaceletException(ex);
262 this.beanHelper.setContact(null);
265 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
267 // Return to user list (for now)
268 return "admin_list_user"; //NOI18N
272 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
274 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
276 // event should not be null
279 throw new NullPointerException("event is null"); //NOI18N
280 } else if (event.getRegisteredUser() == null) {
282 throw new NullPointerException("event.user is null"); //NOI18N
283 } else if (event.getRegisteredUser().getUserId() == null) {
285 throw new NullPointerException("event.user.userId is null"); //NOI18N
286 } else if (event.getRegisteredUser().getUserId() < 1) {
288 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
292 User registeredUser = event.getRegisteredUser();
295 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
301 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
305 public String editUserData () {
307 User user = this.beanHelper.getUser();
309 // Null password means not setting it
310 String encryptedPassword = null;
312 // Check if user instance is in helper and valid
315 throw new NullPointerException("beanHelper.user is null"); //NOI18N
316 } else if (user.getUserId() == null) {
318 throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N //NOI18N
319 } else if (user.getUserId() < 1) {
321 throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", user.getUserId())); //NOI18N
322 } else if (this.getUserName() == null) {
323 // Not all required fields are set
324 throw new NullPointerException("this.userName is null"); //NOI18N
325 } else if (this.getUserName().isEmpty()) {
326 // Not all required fields are set
327 throw new IllegalArgumentException("this.userName is empty"); //NOI18N
328 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
329 // Not same password entered
330 this.setUserPassword(null);
331 this.setUserPasswordRepeat(null);
334 throw new FaceletException("Not same password entered"); //NOI18N
335 } else if (this.userBean.ifUserNameExists(this.getUserName())) {
336 // User name already exists
337 throw new FaceletException(new UserNameAlreadyRegisteredException(this.getUserName()));
338 } else if (this.isSamePasswordEntered()) {
339 // Same password entered, create container
340 if (UserUtils.ifPasswordMatches(new UserLoginContainer(user, this.getUserPassword()))) {
341 // Same password entered
342 throw new FaceletException("Same password as stored entered."); //NOI18N
346 encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
349 // Set user name and flag
350 user.setUserName(this.getUserName());
351 user.setUserMustChangePassword(this.getUserMustChangePassword());
353 // Is a password set?
354 if (encryptedPassword != null) {
356 user.setUserEncryptedPassword(encryptedPassword);
359 // Call EJB for updating user data
360 User updatedUser = this.userBean.updateUserData(user);
363 this.updatedUserDataEvent.fire(new AdminUserDataUpdatedEvent(updatedUser));
365 // Return to user list (for now)
366 return "admin_list_user"; //NOI18N
370 public String getUserLockReason () {
371 return this.userLockReason;
375 public void setUserLockReason (final String userLockReason) {
376 this.userLockReason = userLockReason;
380 public Boolean getUserMustChangePassword () {
381 return this.userMustChangePassword;
385 public void setUserMustChangePassword (final Boolean userMustChangePassword) {
386 this.userMustChangePassword = userMustChangePassword;
390 public String getUserName () {
391 return this.userName;
395 public void setUserName (final String userName) {
396 this.userName = userName;
400 public String getUserPassword () {
401 return this.userPassword;
405 public void setUserPassword (final String userPassword) {
406 this.userPassword = userPassword;
410 public String getUserPasswordRepeat () {
411 return this.userPasswordRepeat;
415 public void setUserPasswordRepeat (final String userPasswordRepeat) {
416 this.userPasswordRepeat = userPasswordRepeat;
422 private void clear () {
425 this.setUserName(null);
426 this.setUserPassword(null);
427 this.setUserPasswordRepeat(null);
428 this.setUserMustChangePassword(null);
432 * Checks if same password is entered and that they are not empty.
434 * @return Whether the same password was entered
436 private boolean isSamePasswordEntered () {
437 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));