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.addressbook.beans.user;
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.event.Observes;
25 import javax.enterprise.inject.Any;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController;
33 import org.mxchange.addressbook.beans.helper.AddressbookAdminWebRequestController;
34 import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jusercore.container.login.UserLoginContainer;
38 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
39 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
40 import org.mxchange.jusercore.events.user.add.AdminUserAddedEvent;
41 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
42 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
43 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
44 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
45 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
46 import org.mxchange.jusercore.model.user.LoginUser;
47 import org.mxchange.jusercore.model.user.User;
48 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
49 import org.mxchange.jusercore.model.user.UserUtils;
50 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
51 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
54 * A user bean (controller)
56 * @author Roland Haeder<roland@mxchange.org>
58 @Named ("adminUserController")
60 public class AddressbookAdminUserWebRequestBean implements AddressbookAdminUserWebRequestController {
65 private static final long serialVersionUID = 542_145_347_916L;
68 * An event fired when the administrator has added a new user
72 private Event<AdminAddedUserEvent> addedUserEvent;
75 * Admin helper instance
78 private AddressbookAdminWebRequestController adminHelper;
83 private final ContactSessionBeanRemote contactBean;
86 * Regular contact controller
89 private AddressbookContactWebSessionController contactController;
92 * An event fired when the administrator has updated a new user
96 private Event<AdminUpdatedUserDataEvent> updatedUserDataEvent;
101 private final UserSessionBeanRemote userBean;
104 * Regular user controller
107 private AddressbookUserWebSessionController userController;
110 * Login bean (controller)
113 private AddressbookUserLoginWebSessionController userLoginController;
118 private String userName;
121 * User password (unencrypted from web form)
123 private String userPassword;
126 * User password repeated (unencrypted from web form)
128 private String userPasswordRepeat;
131 * Default constructor
133 public AddressbookAdminUserWebRequestBean () {
136 // Get initial context
137 Context context = new InitialContext();
140 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
143 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
144 } catch (final NamingException e) {
146 throw new FaceletException(e);
151 public String addUser () {
152 // Create new user instance
153 User user = new LoginUser();
155 // As the form cannot validate the data (required="true"), check it here
156 if (this.getUserName() == null) {
158 throw new NullPointerException("userName is null"); //NOI18N
159 } else if (this.getUserName().isEmpty()) {
161 throw new IllegalArgumentException("userName is null"); //NOI18N
162 } else if (this.adminHelper.getContact() == null) {
163 // No contact instance set, so test required fields: gender, first name and family name
164 if (this.contactController.getGender() == null) {
166 throw new NullPointerException("contactController.gender is null"); //NOI18N
167 } else if (this.contactController.getFirstName() == null) {
169 throw new NullPointerException("contactController.firstName is null"); //NOI18N //NOI18N
170 } else if (this.contactController.getFirstName().isEmpty()) {
172 throw new IllegalArgumentException("contactController.firstName is empty");
173 } else if (this.contactController.getFamilyName() == null) {
175 throw new NullPointerException("contactController.familyName is null"); //NOI18N
176 } else if (this.contactController.getFamilyName().isEmpty()) {
178 throw new IllegalArgumentException("contactController.familyName is empty"); //NOI18N //NOI18N
179 } else if (this.contactController.getEmailAddress() == null) {
181 throw new NullPointerException("contactController.emailAddress is null");
182 } else if (this.contactController.getEmailAddress().isEmpty()) {
184 throw new IllegalArgumentException("contactController.emailAddress is empty"); //NOI18N //NOI18N
185 } else if (this.contactController.getEmailAddressRepeat() == null) {
187 throw new NullPointerException("contactController.emailAddressRepeat is null");
188 } else if (this.contactController.getEmailAddressRepeat().isEmpty()) {
190 throw new IllegalArgumentException("contactController.emailAddressRepeat is empty"); //NOI18N //NOI18N
191 } else if (!Objects.equals(this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat())) {
192 // Is not same email address
193 throw new IllegalArgumentException("Both entered email addresses don't match.");
197 // Set user name, CONFIRMED and INVISIBLE
198 user.setUserName(this.getUserName());
199 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
200 user.setUserProfileMode(ProfileMode.INVISIBLE);
205 // Is a contact instance in helper set?
206 if (this.adminHelper.getContact() instanceof Contact) {
207 // Then use it for contact linking
208 contact = this.adminHelper.getContact();
210 // Create contact instance
211 contact = this.contactController.createContactInstance();
214 // Set contact in user
215 user.setUserContact(contact);
217 // Init variable for password
218 String password = null;
220 // Is the user name or email address used already?
221 // @TODO Add password length check
222 if (this.userController.isUserNameRegistered(user)) {
223 // User name is already used
224 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
225 } else if ((this.adminHelper.getContact() == null) && (this.contactController.isEmailAddressRegistered(user.getUserContact()))) {
226 // Email address is already used
227 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
228 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
229 // Empty password entered, then generate one
230 password = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
231 } else if (!this.isSamePasswordEntered()) {
232 // Both passwords don't match
233 throw new FaceletException(new UserPasswordRepeatMismatchException(user));
235 // Both match, so get it from this bean
236 password = this.getUserPassword();
239 // The password should not be null and at least 5 characters long
240 assert (password != null) : "password is null"; //NOI18N
241 assert (password.length() >= AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
243 // Encrypt password and set it
244 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
246 // Init updated user instance
247 User updatedUser = null;
250 // Now, that all is set, call EJB
251 if (this.adminHelper.getContact() instanceof Contact) {
252 // Link contact with this user
253 updatedUser = this.userBean.linkUser(user);
255 // Remove contact instance
256 this.adminHelper.setContact(null);
259 updatedUser = this.userBean.addUser(user);
261 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
263 throw new FaceletException(ex);
267 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
272 // Return to user list (for now)
273 return "admin_list_user"; //NOI18N
277 public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
279 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminUserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
281 // event should not be null
284 throw new NullPointerException("event is null"); //NOI18N
285 } else if (event.getRegisteredUser() == null) {
287 throw new NullPointerException("event.user is null"); //NOI18N
288 } else if (event.getRegisteredUser().getUserId() == null) {
290 throw new NullPointerException("event.user.userId is null"); //NOI18N
291 } else if (event.getRegisteredUser().getUserId() < 1) {
293 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
297 User registeredUser = event.getRegisteredUser();
300 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
306 //* NOISY-DEBUG: */ System.out.println("AdminUserWebBean:afterRegistration: EXIT!"); //NOI18N
310 public String editUserData () {
312 User user = this.adminHelper.getUser();
314 // Null password means not setting it
315 String encryptedPassword = null;
317 // Check if user instance is in helper and valid
320 throw new NullPointerException("adminHelper.user is null"); //NOI18N
321 } else if (user.getUserId() == null) {
323 throw new NullPointerException("adminHelper.user.userId is null"); //NOI18N //NOI18N
324 } else if (user.getUserId() < 1) {
326 throw new IllegalStateException(MessageFormat.format("adminHelper.user.userId={0} is invalid", user.getUserId())); //NOI18N //NOI18N
327 } else if (this.getUserName() == null) {
328 // Not all required fields are set
329 throw new NullPointerException("this.userName is null"); //NOI18N
330 } else if (this.getUserName().isEmpty()) {
331 // Not all required fields are set
332 throw new IllegalArgumentException("this.userName is empty"); //NOI18N
333 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
334 // Not same password entered
335 this.setUserPassword(null);
336 this.setUserPasswordRepeat(null);
339 throw new FaceletException("Not same password entered"); //NOI18N
340 } else if (this.userBean.ifUserNameExists(this.getUserName())) {
341 // User name already exists
342 throw new FaceletException(new UserNameAlreadyRegisteredException(this.getUserName()));
343 } else if (this.isSamePasswordEntered()) {
344 // Same password entered, create container
345 if (UserUtils.ifPasswordMatches(new UserLoginContainer(user, this.getUserPassword()))) {
346 // Same password entered
347 throw new FaceletException("Same password as stored entered."); //NOI18N
351 encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
355 user.setUserName(this.getUserName());
357 // Is a password set?
358 if (encryptedPassword != null) {
360 user.setUserEncryptedPassword(encryptedPassword);
363 // Call EJB for updating user data
364 User updatedUser = this.userBean.updateUserData(user);
367 this.updatedUserDataEvent.fire(new AdminUserDataUpdatedEvent(updatedUser));
369 // Return to user list (for now)
370 return "admin_list_user"; //NOI18N
374 public String getUserName () {
375 return this.userName;
379 public void setUserName (final String userName) {
380 this.userName = userName;
384 public String getUserPassword () {
385 return this.userPassword;
389 public void setUserPassword (final String userPassword) {
390 this.userPassword = userPassword;
394 public String getUserPasswordRepeat () {
395 return this.userPasswordRepeat;
399 public void setUserPasswordRepeat (final String userPasswordRepeat) {
400 this.userPasswordRepeat = userPasswordRepeat;
404 * Post-initialization of this class
407 public void init () {
413 private void clear () {
416 this.setUserName(null);
417 this.setUserPassword(null);
418 this.setUserPasswordRepeat(null);
422 * Checks if same password is entered and that they are not empty.
424 * @return Whether the same password was entered
426 private boolean isSamePasswordEntered () {
427 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));