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.pizzaapplication.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.jcontacts.contact.Contact;
33 import org.mxchange.jusercore.container.login.UserLoginContainer;
34 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
35 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
36 import org.mxchange.jusercore.events.user.add.AdminUserAddedEvent;
37 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
38 import org.mxchange.jusercore.events.user.update.AdminUserDataUpdatedEvent;
39 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
40 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
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;
48 import org.mxchange.pizzaapplication.beans.BasePizzaController;
49 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
50 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
53 * Administrative user bean (controller)
55 * @author Roland Haeder<roland@mxchange.org>
57 @Named ("adminUserController")
59 public class PizzaAdminUserWebRequestBean extends BasePizzaController implements PizzaAdminUserWebRequestController {
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 PizzaWebRequestController beanHelper;
80 * Regular contact controller
83 private PizzaContactWebSessionController 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 PizzaUserWebSessionController userController;
106 private String userName;
109 * User password (unencrypted from web form)
111 private String userPassword;
114 * User password repeated (unencrypted from web form)
116 private String userPasswordRepeat;
119 * Default constructor
121 public PizzaAdminUserWebRequestBean () {
124 // Get initial context
125 Context context = new InitialContext();
128 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
129 } catch (final NamingException e) {
131 throw new FaceletException(e);
136 public String addUser () {
137 // Create new user instance
138 User user = new LoginUser();
140 // As the form cannot validate the data (required="true"), check it here
141 if (this.getUserName() == null) {
143 throw new NullPointerException("userName is null"); //NOI18N
144 } else if (this.getUserName().isEmpty()) {
146 throw new IllegalArgumentException("userName is null"); //NOI18N
147 } else if (this.beanHelper.getContact() == null) {
148 // No contact instance set, so test required fields: gender, first name and family name
149 if (this.contactController.getGender() == null) {
151 throw new NullPointerException("contactController.gender is null"); //NOI18N
152 } else if (this.contactController.getFirstName() == null) {
154 throw new NullPointerException("contactController.firstName is null"); //NOI18N //NOI18N
155 } else if (this.contactController.getFirstName().isEmpty()) {
157 throw new IllegalArgumentException("contactController.firstName is empty");
158 } else if (this.contactController.getFamilyName() == null) {
160 throw new NullPointerException("contactController.familyName is null"); //NOI18N
161 } else if (this.contactController.getFamilyName().isEmpty()) {
163 throw new IllegalArgumentException("contactController.familyName is empty"); //NOI18N //NOI18N
164 } else if (this.contactController.getEmailAddress() == null) {
166 throw new NullPointerException("contactController.emailAddress is null");
167 } else if (this.contactController.getEmailAddress().isEmpty()) {
169 throw new IllegalArgumentException("contactController.emailAddress is empty"); //NOI18N //NOI18N
170 } else if (this.contactController.getEmailAddressRepeat() == null) {
172 throw new NullPointerException("contactController.emailAddressRepeat is null");
173 } else if (this.contactController.getEmailAddressRepeat().isEmpty()) {
175 throw new IllegalArgumentException("contactController.emailAddressRepeat is empty"); //NOI18N //NOI18N
176 } else if (!Objects.equals(this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat())) {
177 // Is not same email address
178 throw new IllegalArgumentException("Both entered email addresses don't match.");
182 // Set user name, CONFIRMED and INVISIBLE
183 user.setUserName(this.getUserName());
184 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
185 user.setUserProfileMode(ProfileMode.INVISIBLE);
190 // Is a contact instance in helper set?
191 if (this.beanHelper.getContact() instanceof Contact) {
192 // Then use it for contact linking
193 contact = this.beanHelper.getContact();
195 // Create contact instance
196 contact = this.contactController.createContactInstance();
199 // Set contact in user
200 user.setUserContact(contact);
202 // Init variable for password
203 String password = null;
205 // Is the user name or email address used already?
206 // @TODO Add password length check
207 if (this.userController.isUserNameRegistered(user)) {
208 // User name is already used
209 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
210 } else if ((this.beanHelper.getContact() == null) && (this.contactController.isEmailAddressRegistered(user.getUserContact()))) {
211 // Email address is already used
212 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
213 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
214 // Empty password entered, then generate one
215 password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
216 } else if (!this.isSamePasswordEntered()) {
217 // Both passwords don't match
218 throw new FaceletException(new UserPasswordRepeatMismatchException(user));
220 // Both match, so get it from this bean
221 password = this.getUserPassword();
224 // The password should not be null and at least 5 characters long
225 assert (password != null) : "password is null"; //NOI18N
226 assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
228 // Encrypt password and set it
229 user.setUserEncryptedPassword(UserUtils.encryptPassword(password));
231 // Init updated user instance
232 User updatedUser = null;
235 // Now, that all is set, call EJB
236 if (this.beanHelper.getContact() instanceof Contact) {
237 // Link contact with this user
238 updatedUser = this.userBean.linkUser(user);
240 // Remove contact instance
241 this.beanHelper.setContact(null);
244 updatedUser = this.userBean.addUser(user);
246 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
248 throw new FaceletException(ex);
252 this.beanHelper.setContact(null);
255 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
260 // Return to user list (for now)
261 return "admin_list_user"; //NOI18N
265 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
267 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminUserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
269 // event should not be null
272 throw new NullPointerException("event is null"); //NOI18N
273 } else if (event.getRegisteredUser() == null) {
275 throw new NullPointerException("event.user is null"); //NOI18N
276 } else if (event.getRegisteredUser().getUserId() == null) {
278 throw new NullPointerException("event.user.userId is null"); //NOI18N
279 } else if (event.getRegisteredUser().getUserId() < 1) {
281 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
285 User registeredUser = event.getRegisteredUser();
288 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
294 //* NOISY-DEBUG: */ System.out.println("AdminUserWebBean:afterRegistration: EXIT!"); //NOI18N
298 public String editUserData () {
300 User user = this.beanHelper.getUser();
302 // Null password means not setting it
303 String encryptedPassword = null;
305 // Check if user instance is in helper and valid
308 throw new NullPointerException("beanHelper.user is null"); //NOI18N
309 } else if (user.getUserId() == null) {
311 throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N //NOI18N
312 } else if (user.getUserId() < 1) {
314 throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", user.getUserId())); //NOI18N //NOI18N
315 } else if (this.getUserName() == null) {
316 // Not all required fields are set
317 throw new NullPointerException("this.userName is null"); //NOI18N
318 } else if (this.getUserName().isEmpty()) {
319 // Not all required fields are set
320 throw new IllegalArgumentException("this.userName is empty"); //NOI18N
321 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
322 // Not same password entered
323 this.setUserPassword(null);
324 this.setUserPasswordRepeat(null);
327 throw new FaceletException("Not same password entered"); //NOI18N
328 } else if (this.userBean.ifUserNameExists(this.getUserName())) {
329 // User name already exists
330 throw new FaceletException(new UserNameAlreadyRegisteredException(this.getUserName()));
331 } else if (this.isSamePasswordEntered()) {
332 // Same password entered, create container
333 if (UserUtils.ifPasswordMatches(new UserLoginContainer(user, this.getUserPassword()))) {
334 // Same password entered
335 throw new FaceletException("Same password as stored entered."); //NOI18N
339 encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
343 user.setUserName(this.getUserName());
345 // Is a password set?
346 if (encryptedPassword != null) {
348 user.setUserEncryptedPassword(encryptedPassword);
351 // Call EJB for updating user data
352 User updatedUser = this.userBean.updateUserData(user);
355 this.updatedUserDataEvent.fire(new AdminUserDataUpdatedEvent(updatedUser));
357 // Return to user list (for now)
358 return "admin_list_user"; //NOI18N
362 public String getUserName () {
363 return this.userName;
367 public void setUserName (final String userName) {
368 this.userName = userName;
372 public String getUserPassword () {
373 return this.userPassword;
377 public void setUserPassword (final String userPassword) {
378 this.userPassword = userPassword;
382 public String getUserPasswordRepeat () {
383 return this.userPasswordRepeat;
387 public void setUserPasswordRepeat (final String userPasswordRepeat) {
388 this.userPasswordRepeat = userPasswordRepeat;
392 * Post-initialization of this class
395 public void init () {
401 private void clear () {
404 this.setUserName(null);
405 this.setUserPassword(null);
406 this.setUserPasswordRepeat(null);
410 * Checks if same password is entered and that they are not empty.
412 * @return Whether the same password was entered
414 private boolean isSamePasswordEntered () {
415 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));