Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jsfcourse.security;
- import java.util.List;
- import javax.ejb.EJB;
- import javax.faces.application.FacesMessage;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.SessionScoped;
- import javax.faces.context.FacesContext;
- import javax.servlet.http.HttpSession;
- import com.jsf.dao.LoginDAO;
- import com.jsf.entities.User;
- @ManagedBean
- @SessionScoped
- public class LoginBB {
- private static final String PAGE_MAIN = "/pages/admin";
- private static final String PAGE_LOGIN = "/pages/login";
- private static final String PAGE_STAY_AT_THE_SAME = null;
- private String login;
- private String password;
- public String getLogin() {
- return login;
- }
- public void setLogin(String login) {
- this.login = login;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- //Dependency injection
- // - no setter method needed in this case
- @EJB
- LoginDAO loginDAO;
- public boolean validateData() {
- boolean result = true;
- FacesContext ctx = FacesContext.getCurrentInstance();
- // check if not empty
- if (login == null || login.length() == 0) {
- ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
- "podaj login", "null"));
- }
- if (password == null || password.length() == 0) {
- ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
- "podaj haslo", "null"));
- }
- if (ctx.getMessageList().isEmpty()) {
- result = true;
- } else {
- result = false;
- }
- return result;
- }
- public String doLogin() {
- FacesContext ctx = FacesContext.getCurrentInstance();
- User user = null;
- // 1. check parameters and stay if errors
- if (!validateData()) {
- return PAGE_STAY_AT_THE_SAME;
- }
- // 2. verify login and password - get User from "database"
- user = getUserFromDatabase(login, password);
- // 3. if bad login or password - stay with error info
- if (user == null) {
- ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
- "Niepoprawny login lub haslo", null));
- return PAGE_STAY_AT_THE_SAME;
- }
- // 4. if login ok - save User object in session
- HttpSession session = (HttpSession) ctx.getExternalContext()
- .getSession(true);
- session.setAttribute("user", user);
- // and enter the system
- return PAGE_MAIN;
- }
- public User getUser() {
- HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
- .getExternalContext().getSession(true);
- return (User) session.getAttribute("user");
- }
- public String doLogout(){
- HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
- .getExternalContext().getSession(true);
- //Invalidate session
- // - all objects within session will be destroyed
- // - new session will be created (with new ID)
- session.invalidate();
- return PAGE_LOGIN;
- }
- // simulate finding user in database
- private User getUserFromDatabase(String login, String password) {
- User u = null;
- List<User> list = loginDAO.getUser(login, password);
- if ( list.size() != 0) {
- u = new User();
- u = list.get(0);
- }
- return u;
- }
- }
Add Comment
Please, Sign In to add comment