PasswordResetView.java 3.63 KB
package fi.insomnia.bortal.view;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.beans.UserBeanLocal;
import fi.insomnia.bortal.model.User;
import fi.insomnia.bortal.utilities.PasswordFunctions;

@ManagedBean(name = "pwdResetView")
@SessionScoped
public class PasswordResetView extends GenericView {

    private Integer id;
    private String hash;
    private static final Logger logger = LoggerFactory.getLogger(PasswordResetView.class);

    private String password;
    private String confirm;
    private User user;

    private String mailuser;

    @EJB
    private UserBeanLocal userbean;

    public boolean hashMatch() {
        if (id != null && id > 0 && hash != null && hash.length() > 5) {
            user = userbean.findPasswordResetUser(id, hash);
        }
        return user != null;
    }

    public String change() {
        boolean changed = false;
        String ret = null;
        if (password.length() < 5) {
            addFaceMessage("userview.passwordTooShort");
        } else if (!password.equals(confirm)) {
            addFaceMessage("userview.passwordsDontMatch");
        } else if (user != null) {
            changed = userbean.resetPassword(user, password, hash);
        }
        if (changed)
            ret = "passwordChanged";
        return ret;
    }

    public String sendMail() {
        User userObj = userbean.getUser(mailuser);
        if (userObj != null) {
            String hashStr = PasswordFunctions.generateRandomString(25);

            ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
            StringBuilder path = new StringBuilder()
                    .append("http://")
                    .append(extcontext.getRequestServerName());

            if (extcontext.getRequestServerPort() != 80) {
                path.append(":").append(extcontext.getRequestServerPort());
            }

            path.append("/")
                    .append(FacesContext.getCurrentInstance().getExternalContext().getContextName())
                    .append("/auth/resetPassword.jsf?id=")
                    .append(userObj.getId()).append("&hash=")
                    .append(hashStr);
            logger.debug("billpath {}", path);
            userbean.initPasswordReset(userObj, hashStr, path.toString());

            return "resetmailSent";
        }
        mailuser = null;
        this.addFaceMessage("passwordreset.usernotfound");
        return null;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserBeanLocal getUserbean() {
        return userbean;
    }

    public void setUserbean(UserBeanLocal userbean) {
        this.userbean = userbean;
    }

    public void setMailuser(String mailuser) {
        this.mailuser = mailuser;
    }

    public String getMailuser() {
        return mailuser;
    }

}