Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package web_projekt;
- import java.util.List;
- import javax.ejb.EJB;
- import javax.faces.application.FacesMessage;
- import javax.faces.bean.ManagedBean;
- import javax.faces.context.FacesContext;
- import javax.servlet.http.HttpSession;
- import projekt.dao.UserDAO;
- import projekt.entities.User;
- @ManagedBean
- public class LoginBB {
- private static final String PAGE_MAIN = "taskList?faces-redirect=true";
- private static final String PAGE_LOGIN = "login";
- private static final String PAGE_STAY = null;
- private String login;
- private String password;
- @EJB
- UserDAO userDAO;
- 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;
- }
- public boolean validateData() {
- boolean result = true;
- FacesContext ctx = FacesContext.getCurrentInstance();
- 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 hasło", "null"));
- }
- if (!ctx.getMessageList().isEmpty()) {
- result = false;
- }
- return result;
- }
- public String doLogin() {
- FacesContext ctx = FacesContext.getCurrentInstance();
- User user = null;
- if (!validateData()) {
- return PAGE_STAY;
- }
- if (login.length() < 1) {
- ctx.addMessage(null, new FacesMessage(
- FacesMessage.SEVERITY_ERROR, "NIE MA LOGINU", null
- ));
- return PAGE_STAY;
- }
- if (password.length() < 1) {
- ctx.addMessage(null, new FacesMessage(
- FacesMessage.SEVERITY_ERROR, "NIE MA HASŁA", null
- ));
- return PAGE_STAY;
- }
- user = getUserFromDB(login, password);
- if (user == null) {
- ctx.addMessage(null, new FacesMessage(
- FacesMessage.SEVERITY_ERROR, "Niepoprawne dane logowania.", null
- ));
- return PAGE_STAY;
- }
- HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);
- session.setAttribute("userSession", user);
- return PAGE_MAIN;
- }
- public User getUser() {
- HttpSession session = (HttpSession)
- FacesContext.getCurrentInstance().getExternalContext().getSession(true);
- return (User) session.getAttribute("userSession");
- }
- public String doLogout() {
- HttpSession session = (HttpSession)
- FacesContext.getCurrentInstance().getExternalContext().getSession(true);
- session.invalidate();
- return PAGE_LOGIN;
- }
- public User getUserFromDB(String login, String password) {
- List<User> users = userDAO.getFullList();
- User user = null;
- for (User list_user : users) {
- if ((list_user.getLoginUser().equals(login))
- && list_user.getPasswordUser().equals(password)) {
- user = list_user;
- }
- }
- return user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement