/*
- * Copyright (C) 2016 - 2018 Free Software Foundation
+ * Copyright (C) 2016 - 2020 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.faces.FacesException;
+import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
-import javax.faces.view.facelets.FaceletException;
import javax.inject.Inject;
import javax.inject.Named;
import org.mxchange.jcontacts.model.contact.Contact;
import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
+import org.mxchange.jjobs.beans.user.list.JobsUserListWebViewController;
import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
-import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
-import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
+import org.mxchange.jusercore.events.user.update.post.AdminPostUserDataUpdatedEvent;
+import org.mxchange.jusercore.events.user.update.post.ObservableAdminPostUserDataUpdatedEvent;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNotFoundException;
*/
@Inject
@Any
- private Event<ObservableAdminUpdatedUserDataEvent> updatedUserDataEvent;
+ private Event<ObservableAdminPostUserDataUpdatedEvent> updatedUserDataEvent;
/**
* User instance
@Any
private Event<ObservableAdminLinkedUserEvent> userLinkedEvent;
+ /**
+ * Regular user controller
+ */
+ @Inject
+ private JobsUserListWebViewController userListController;
+
/**
* User lock reason
*/
* sending it to the EJB. The data set in the controller is being verified,
* e.g. if the user name or email address is not used yet.
* <p>
- * @return Redirect outcome
*/
- public String addUser () {
+ public void addUser () {
// As the form cannot validate the data (required="true"), check it here
if (this.getUserName() == null) {
// Throw NPE
// Is the user name or email address used already?
// @TODO Add password length check
- if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(newUser))) {
+ if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(newUser))) {
// User name is already used
- throw new FaceletException(new UserNameAlreadyRegisteredException(newUser));
+ throw new FacesException(new UserNameAlreadyRegisteredException(newUser));
} else if ((this.getContact() == null) && (this.contactController.isEmailAddressRegistered(newUser.getUserContact()))) {
// Email address is already used
- this.showFacesMessage("admin_add_user:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
+ this.showFacesMessage("admin_add_user:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
// Always clear password
this.setUserPassword(null);
this.setUserPasswordRepeat(null);
// Skip it
- return ""; //NOI18N
+ return;
}
+ // Init variable
+ final User updatedUser;
+
try {
// Now, that all is set, call EJB
if (this.getContact() instanceof Contact) {
// Link contact with this user
- final User updatedUser = this.adminUserBean.linkUser(newUser);
-
- // Fire event
- this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
+ updatedUser = this.adminUserBean.linkUser(newUser);
} else {
// Add new user
- final User updatedUser = this.adminUserBean.addUser(newUser);
-
- // Fire event
- this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
+ updatedUser = this.adminUserBean.addUser(newUser);
}
} catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
// Throw again
- throw new FaceletException(ex);
+ throw new FacesException(ex);
}
+ // Now, that all is set, call EJB
+ if (this.getContact() instanceof Contact) {
+ // Fire event
+ this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
+ } else {
+ // Fire event
+ this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
+ }
// Clear helper
this.setContact(null);
// Clear this bean
this.clear();
-
- // Return to user list (for now)
- return "admin_list_user"; //NOI18N
}
/**
* @param event User registration event
*/
public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
/**
* Deletes given user account
- * <p>
- * @return Redirect outcome
*/
- public String deleteUserData () {
+ public void deleteUserData () {
// Is the user instance valid and CONFIRMED?
if (this.getUser() == null) {
// Throw NPE
this.adminUserBean.deleteUser(this.getUser(), this.getUserDeleteReason());
} catch (final UserNotFoundException ex) {
// Should not happen, so throw again
- throw new FaceletException(ex);
+ throw new FacesException(ex);
}
// Fire event
this.deleteUserEvent.fire(new AdminDeletedUserEvent(this.getUser(), this.getUserDeleteReason()));
-
- // Redirect
- return "admin_list_user"; //NOI18N
}
/**
* Edits currently loaded user's data in database.
- * <p>
- * @return Redirect outcome
*/
- public String editUserData () {
+ public void editUserData () {
// Null password means not setting it
String encryptedPassword = null;
this.setUserPasswordRepeat(null);
// Not same password entered
- this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT"); //NOI18N
- return ""; //NOI18N
+ this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT", FacesMessage.SEVERITY_INFO); //NOI18N
+ return;
} else if ((!Objects.equals(this.getUser().getUserName(), this.getUserName())) && (this.userBean.ifUserNameExists(this.getUserName()))) {
// Clear all fields
this.clear();
// User name already exists
- this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS"); //NOI18N
- return ""; //NOI18N
+ this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS", FacesMessage.SEVERITY_WARN); //NOI18N
+ return;
} else if (this.isSamePasswordEntered()) {
// Same password entered, create container
if ((Objects.equals(this.getUser().getUserMustChangePassword(), this.getUserMustChangePassword())) && (UserLoginUtils.ifPasswordMatches(new UserLoginContainer(this.getUser(), this.getUserPassword())))) {
this.setUserPasswordRepeat(null);
// Same password entered
- this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD"); //NOI18N
- return ""; //NOI18N
+ this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD", FacesMessage.SEVERITY_WARN); //NOI18N
+ return;
}
// Encrypt password
final User updatedUser = this.userBean.updateUserData(this.getUser());
// Fire event
- this.updatedUserDataEvent.fire(new AdminUpdatedUserDataEvent(updatedUser));
-
- // Return to user list (for now)
- return "admin_list_user"; //NOI18N
+ this.updatedUserDataEvent.fire(new AdminPostUserDataUpdatedEvent(updatedUser));
}
/**
throw new FacesException(new UserStatusLockedException(this.getUser()));
} else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
// User account is locked
- throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
+ throw new FacesException(new UserStatusUnconfirmedException(this.getUser()));
} else if (this.getUserLockReason() == null) {
// Throw NPE again
throw new NullPointerException("this.userLockReason is null"); //NOI18N
updatedUser = this.adminUserBean.lockUserAccount(this.getUser(), this.getUserLockReason(), baseUrl);
} catch (final UserStatusLockedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
// Throw again
- throw new FaceletException(ex);
+ throw new FacesException(ex);
}
// Fire event
throw new FacesException(new UserStatusConfirmedException(this.getUser()));
} else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
// User account is locked
- throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
+ throw new FacesException(new UserStatusUnconfirmedException(this.getUser()));
}
// Init updated user instance
updatedUser = this.adminUserBean.unlockUserAccount(this.getUser(), baseUrl);
} catch (final UserStatusConfirmedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
// Throw again
- throw new FaceletException(ex);
+ throw new FacesException(ex);
}
// Fire event
password = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
} else if (!this.isSamePasswordEntered()) {
// Both passwords don't match
- throw new FaceletException(new UserPasswordRepeatMismatchException());
+ throw new FacesException(new UserPasswordRepeatMismatchException());
} else {
// Both match, so get it from this bean
password = this.getUserPassword();
}
// Create new instance
- final User newUser = new LoginUser(this.getUserName(), this.getUserProfileMode(), this.getUserMustChangePassword(), UserLoginUtils.encryptPassword(password), UserAccountStatus.CONFIRMED, userContact);
+ final User newUser = new LoginUser(
+ this.getUserName(),
+ this.getUserProfileMode(),
+ this.getUserMustChangePassword(),
+ UserLoginUtils.encryptPassword(password),
+ UserAccountStatus.CONFIRMED,
+ userContact
+ );
// Get locale from view-root
final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();