Package com.agilebooster.view

Source Code of com.agilebooster.view.SessionBean

/*
* (C) Copyright 2014 Agile Booster.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Maxime ESCOURBIAC
*/
package com.agilebooster.view;

import com.agilebooster.data.entity.User;
import com.agilebooster.data.jpa.UserController;
import com.agilebooster.security.PasswordManager;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
* JSF Managed Bean for User Session
*
* @author Maxime ESCOURBIAC
*/
@ManagedBean(name = "sessionBean")
@SessionScoped
public class SessionBean implements Serializable {

    private final UserController userController;
    private User user;

    /**
     * Creates a new instance of SessionBean
     */
    public SessionBean() {
        userController = new UserController();
    }

    /**
     * Connect Method.
     *
     * @param mail User mail.
     * @param password User password.
     */
    public void connect(String mail, String password) {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            User userChecked = userController.getUserByMail(mail);
            if (userChecked == null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection faild", "The user is not existing." + mail));
            } else {
                if (PasswordManager.CheckPassword(password, userChecked.getPassword()) == false) {
                    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection faild", "The password is not correct." + mail));
                } else {
                    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Connection succeed", "Welcome " + userChecked.getForename()));
                    user = userChecked;
                    FacesContext.getCurrentInstance().getExternalContext().redirect("/Agile-Booster/dashboard.xhtml");
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception", ex.getMessage()));
        }
    }

    /**
     * Logout the user.
     */
    public void logout() {
        user = null;
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Agile-Booster/index.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Test if the user is connected.
     */
    public void autologin() {
        if (user != null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Agile-Booster/dashboard.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    /**
     * Test if the user is connected.
     */
    public void checkSession() {
        if (user == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Agile-Booster/index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    //Getter & Setter for Bean properties.
    public User getUser() {
        return user;
    }

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

Related Classes of com.agilebooster.view.SessionBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.