package org.ngbw.web.actions; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Date; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.ngbw.sdk.UserAuthenticationException; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.common.util.StringUtils; import org.ngbw.sdk.common.util.ValidationResult; import org.ngbw.sdk.core.shared.UserRole; import org.ngbw.sdk.database.Country; import org.ngbw.sdk.database.Group; import org.ngbw.sdk.database.User; import org.ngbw.sdk.phoenix.CipresPhoenixUserAccountManager; import org.ngbw.sdk.tool.CipresNotifier; import org.ngbw.sdk.tool.DateUtils; import org.ngbw.web.controllers.FolderController; /** * Struts action class to process all user profile-related requests in the NGBW * web application. * * @author Jeremy Carver * @author Tony Chen */ @SuppressWarnings("serial") public class ProfileManager extends SessionManager { private static final Logger logger = Logger.getLogger(ProfileManager.class); private boolean dontSendToCloudFlag = false; private String dontSendToCloudPref = "dont_send_to_cloud"; private String dontSendToCloud = null; private static Map carriedOverErrors = new HashMap(); static { carriedOverErrors.put(CreateTask.INVALID_TASKID, CreateTask.JAVASCRIPT_DISABLED); } private String carriedOverError = null; public ProfileManager() { logger.info("carriedOverError = " + carriedOverError); if (carriedOverError == null) { carriedOverError = ServletActionContext.getRequest().getParameter("carriedOverError"); } logger.info("carriedOverError = " + carriedOverError + " and error message = " + carriedOverErrors.get(carriedOverError)); if (carriedOverError != null && carriedOverErrors.get(carriedOverError) != null) { super.addActionError(carriedOverErrors.get(carriedOverError)); } } @Override public String execute () { try { final String WILL_EXPIRE = "Alert: Your current subscription will expire in %d days."; final String HAS_EXPIRED = "Alert: Your current subscription has expired %d days ago."; // Check user's subscription expiration date, // and show a message to the user if it has expired (OR) will expire in less than 30 days. logger.debug("Checking user's SU subscrition expiration date ..."); final User user = super.getWorkbenchSession().getUser(); final Date expires = Workbench.getInstance().getSuAllocationExpireTime(user); final long remaining = Workbench.getInstance().getSuAllocationRemaining(user); final long dayCount = (expires == null) ? 0 : DateUtils.daysDidff(expires, new Date(), false); logger.debug(String.format( "User[%d - %s] SU subscription expires on [%s]; Days left [%d].", user.getUserId(), user.getUsername(), DateUtils.formatDate("yyyy/MM/dd HH:mm:ss", expires), dayCount)); if (dayCount <= 0) { super.reportUserError(String.format(HAS_EXPIRED, dayCount)); } else if (dayCount > 0 && dayCount <= 30) { if (remaining > 0L) super.reportUserError(String.format(WILL_EXPIRE, dayCount)); } } catch ( Throwable t ) { logger.error(t.getMessage(), t); } return SUCCESS; } @Override public String input () { populateProfileForm(); return INPUT; } public String updatePassword () { if (validatePasswordInput()) { try { if (Workbench.getInstance().hasInvalidChars(getNewPassword(), false)) { super.addActionError( "Your password contains one or more unsupported characters. " + "Supported characters are " + NgbwSupport.ALPHANUMERIC_MSG + " and " + NgbwSupport.SPECIAL_CHARS_MSG); return INPUT; } else if (getController().editPassword(getCurrentPassword(), getNewPassword())) { super.getController().getWorkbenchSession().clearSessionAttribute(NgbwSupport.REQ_PASSWORD_CHANGE); logger.debug("Attribute [%s] removed from Session."); addActionMessage("Password successfully updated."); if (Workbench.getInstance().isPhoenixApiModuleEnabled(Boolean.FALSE)) { try { User user = getAuthenticatedUser(); CipresPhoenixUserAccountManager syncManager = new CipresPhoenixUserAccountManager(user, getNewPassword()); syncManager.sync(Boolean.TRUE); } catch ( Throwable t ) { logger.error(t.getMessage(), t); } } return SUCCESS; } else { return INPUT; } } catch (UserAuthenticationException error) { addFieldError("currentPassword", error.getMessage()); return INPUT; } } else { return INPUT; } } public String updatePersonalInformation () { if (validatePersonalInformation()) { String email = (getEmail() == null)? null : getEmail().trim(); if (StringUtils.isNullOrEmpty(email, true)) { reportUserError("Email is required."); return INPUT; } else if (!email.equals(getConfirmEmail())) { reportUserError("Sorry, the email addresses you entered aren't identical. Please try again."); return INPUT; } else //if (!StringUtils.isValidEmailFormat(email)) { if (!email.contains("@")) { reportUserError("Sorry, the email address you entered is invalid"); return INPUT; } String strArr[] = email.split("@"); if (strArr.length < 2) { reportUserError("Sorry, the email address you entered is invalid"); return INPUT; } } ValidationResult result = getController() .editUser( getEmail(), getFirstName(), getLastName(), getInstitution(), getCountry(), getAccount()); if (!result.isValid()) { for (String error : result.getErrors()) { reportUserError(error); } return INPUT; } addActionMessage("Personal information successfully updated."); return SUCCESS; } else { return INPUT; } } public String dontSendToCloud () { String tf = (dontSendToCloudFlag? "true":"false"); getController().setUserPreference(dontSendToCloudPref, tf); addActionMessage("Your preferences (" + dontSendToCloudPref + " == " + tf + ") have been saved sucessfully."); return SUCCESS; } public String getDontGoToCloud() { String value = getController().getUserPreference(dontSendToCloudPref); //boolean dontSendToCloud = false; /* try { if (value != null && !value.trim().isEmpty()) dontSendToCloud = Boolean.parseBoolean(value); } catch (Exception e) { logger.error(e.toString()); }*/ return value; } public void setDontGoToCloud(String dontSendToCloud) { this.dontSendToCloud = dontSendToCloud; } public boolean getDontSendToCloudFlag() { String value = getController().getUserPreference(dontSendToCloudPref); //boolean dontSendToCloud = false; try { if (value != null && !value.trim().isEmpty()) dontSendToCloudFlag = Boolean.parseBoolean(value); } catch (Exception e) { logger.error(e.toString()); } return dontSendToCloudFlag; } public void setDontSendToCloudFlag(boolean dontSendToCloudFlag) { this.dontSendToCloudFlag = dontSendToCloudFlag; } public UserRole getAuthenticatedUserRole () { FolderController controller = getFolderController(); return (controller == null)? null : controller.getAuthenticatedUserRole(); } public Set getAuthenticatedUserGroups () { FolderController controller = getFolderController(); return (controller == null)? null : controller.getAuthenticatedUserGroups(); } public String getCarriedOverError() { return carriedOverError; } public void setcarriedOverError(String carriedOverError) { this.carriedOverError = carriedOverError; } protected boolean validatePasswordInput () { String currentPassword = getCurrentPassword(); if (StringUtils.isNullOrEmpty(currentPassword, true)) { addFieldError("currentPassword", "Current Password is required."); } if (StringUtils.isNullOrEmpty(getNewPassword(), true)) { addFieldError("newPassword", "New Password is required."); } else if (!getNewPassword().equals(getConfirmNewPassword())) { addFieldError( "confirmNewPassword", "Sorry, the passwords you entered aren't identical. Please try again."); } return !hasFieldErrors(); } protected boolean validatePersonalInformation () { String firstName = getFirstName(); if (StringUtils.isNullOrEmpty(firstName, true)) { addFieldError("firstName", "First Name is required."); } String lastName = getLastName(); if (StringUtils.isNullOrEmpty(lastName, true)) { addFieldError("lastName", "Last Name is required."); } String email = getEmail(); String oldEmail = getAuthenticatedUser().getEmail(); if (StringUtils.isNullOrEmpty(email, true)) { addFieldError("email", "Email is required."); } else if (email.equals(oldEmail)) { String confirmEmail = getConfirmEmail(); if (confirmEmail != null && confirmEmail.trim().equals("") == false && email.equals(confirmEmail) == false) { addFieldError("confirmEmail", "Sorry, the email addresses you entered " + "aren't identical. Please try again."); } } else if (!email.equals(getConfirmEmail())) { addFieldError("confirmEmail", "Sorry, the email addresses you entered " + "aren't identical. Please try again."); } String institution = getInstitution(); if (StringUtils.isNullOrEmpty(institution, true)) { addFieldError("institution", "Institution is required."); } String ctry = getCountry(); logger.info("return value from getCountry() = " + ctry); if (ctry == null) { final User user = super.getAuthenticatedUser(); if (null != user) { ctry = user.getCountry(); } } logger.info("return value from user.getCountry() = " + ctry); Country country = Country.getCountry(ctry); // The Country code is in the db. if (null != country) { setCountry(country.getCountryCode()); } else { String countryCode = null; try { countryCode = Workbench.getInstance().getCountryCodeFromIP(super.getClientIp()); } catch ( Throwable t ) { countryCode = null; logger.error(t.getMessage(), t); } // Cannot find the Country from the user's IP. if (null == countryCode) { addFieldError("country", "Country is required."); final User user = super.getAuthenticatedUser(); if (null != user) { CipresNotifier.emailSysAdmin( "User Country Code Error", String.format( "User [%d, %s]\n\n" + "Unable to find country code of IP %s.", user.getUserId(), user.getUsername(), super.getClientIp()), false); } } // The Country code is NOT in the db. else if (!Workbench.getInstance().isValidCountryCode(countryCode)) { addFieldError("country", "Country is required."); } else { setCountry(countryCode); } } return !hasFieldErrors(); } private void populateProfileForm () { User user = getAuthenticatedUser(); if (user == null) { return; } else { setFirstName(user.getFirstName()); setLastName(user.getLastName()); setEmail(user.getEmail()); setInstitution(user.getInstitution()); setCountry(user.getCountry()); setAccount(user.getAccount("teragrid")); } } }