Subversion Repositories WebE

Rev

Rev 30 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30 Rev 33
Line 1... Line 1...
1
package ch.ffhs.webE.action;
1
package ch.ffhs.webE.action;
2
2
3
import java.util.Map;
3
import java.util.Map;
4
4
5
import ch.ffhs.webE.dao.UserDAO;
-
 
6
import ch.ffhs.webE.dao.UserDAOImpl;
5
import ch.ffhs.webE.dao.UserDAOImpl;
7
import ch.ffhs.webE.domain.User;
6
import ch.ffhs.webE.domain.User;
8
7
-
 
8
import com.opensymphony.xwork2.Action;
9
import com.opensymphony.xwork2.ActionContext;
9
import com.opensymphony.xwork2.ActionContext;
10
import com.opensymphony.xwork2.ActionSupport;
10
import com.opensymphony.xwork2.ActionSupport;
11
import com.opensymphony.xwork2.ModelDriven;
11
import com.opensymphony.xwork2.ModelDriven;
12
12
13
public class LoginAction extends ActionSupport implements ModelDriven<User>
13
public class LoginAction extends ActionSupport implements ModelDriven<User>
14
{
14
{
15
15
16
    private static final long serialVersionUID = 1799753056277211344L;
16
  private static final long serialVersionUID = 1799753056277211344L;
17
    private User user = new User();
17
  private final User user = new User();
18
    private UserDAO userDAO = new UserDAOImpl();
18
  private final UserDAOImpl userDAO = new UserDAOImpl();
19
19
20
    // Form fields
20
  /* Form fields */
21
    private String userName;
21
  private String userName;
22
    private String pw;
22
  private String pw;
23
23
-
 
24
  /**
24
    // Session Object
25
   * JSP session object
-
 
26
   */
25
    Map<String, Object> session = ActionContext.getContext().getSession();
27
  Map<String, Object> session = ActionContext.getContext().getSession();
26
28
27
    public LoginAction()
-
 
28
    {
29
  /**
29
    }
30
   *
30
31
   */
31
    public String doLogin()
32
  public LoginAction()
32
    {
33
  {
-
 
34
  }
33
35
34
        // If password or user name are empty, the login fails.
-
 
35
        if ("".equals(getUserName()) || "".equals(getPw())
-
 
36
                || getUserName() == null || getPw() == null)
-
 
37
        {
-
 
38
            addFieldError("userName", "Falscher Username oder Passwort");
-
 
39
            return ERROR;
36
  public String doLogin()
40
        }
37
  {
41
38
42
        String verifiedUser = verifyUser(getUserName(), getPw());
39
    // If password or user name are empty, the login fails.
43
        if (verifiedUser.equals("failed"))
40
    if ("".equals(this.getUserName()) || "".equals(this.getPw())
44
        {
-
 
45
            addFieldError("userName", "Falscher Username oder Passwort");
41
        || this.getUserName() == null || this.getPw() == null)
46
            return ERROR;
-
 
47
        }
-
 
48
        else
-
 
49
        {
42
    {
50
-
 
51
            // Put user name, password into session
-
 
52
            session.put("username", getUserName());
43
      this.addFieldError("userName", "Falscher Username oder Passwort");
53
            session.put("pw", getPw());
-
 
54
            return verifiedUser;
44
      return Action.ERROR;
55
        }
-
 
56
    }
45
    }
57
46
58
    /**
-
 
59
     * Logout ausführen. Zerstört die Daten in der Session
47
    String verifiedUser = this.verifyUser(this.getUserName(), this.getPw());
60
     * @return String
-
 
61
     */
-
 
62
    public String doLogout()
48
    if (verifiedUser.equals("failed"))
63
    {
49
    {
64
        //Kill Session content
-
 
65
        ActionContext.getContext().getSession().clear();
50
      this.addFieldError("userName", "Falscher Username oder Passwort");
66
        return SUCCESS;
51
      return Action.ERROR;
67
    }
52
    }
68
-
 
69
    /**
53
    else
70
     * Verify user credentials
-
 
71
     *
-
 
72
     * @param String
-
 
73
     *            username: User name
-
 
74
     * @param String
-
 
75
     *            password: Password (hashed)
-
 
76
     * @return
-
 
77
     */
-
 
78
    public String verifyUser(String username, String password)
-
 
79
    {
54
    {
80
        // DB Query
-
 
81
        User u = userDAO.searchUsername(username);
-
 
82
55
83
        // User does not exist
56
      // Put user name, password into session
-
 
57
      this.session.put("username", this.getUserName());
84
        if (u == null)
58
      this.session.put("pw", this.getPw());
85
            return ERROR;
59
      return verifiedUser;
-
 
60
    }
-
 
61
  }
86
62
-
 
63
  /**
-
 
64
   * Logout ausf�hren. Zerst�rt die Daten in der Session
-
 
65
   *
-
 
66
   * @return String
-
 
67
   */
-
 
68
  public String doLogout()
-
 
69
  {
87
        // User password does not match
70
    // Kill Session content
88
        if (!u.getPassword().equals(password))
71
    ActionContext.getContext().getSession().clear();
89
            return ERROR;
72
    return Action.SUCCESS;
-
 
73
  }
90
74
91
        // User credentials are fine, check for admin rights
-
 
-
 
75
  /**
92
        if (u.isAdmin())
76
   * Verify user credentials
93
        {
77
   *
-
 
78
   * @param String
94
            return "admin";
79
   *          username: User name
95
        }
80
   * @param String
-
 
81
   *          password: Password (hashed)
96
        else
82
   * @return
97
        {
83
   */
98
            return "user";
84
  public String verifyUser(String username, String password)
99
        }
85
  {
100
    }
86
    // DB Query
-
 
87
    User u = this.userDAO.searchUsername(username);
101
88
102
    public String getUserName()
89
    // User does not exist
-
 
90
    if (u == null)
103
    {
91
    {
104
        return userName;
92
      return Action.ERROR;
105
    }
93
    }
106
94
-
 
95
    // User password does not match
107
    public void setUserName(String userName)
96
    if (!u.getPassword().equals(password))
108
    {
97
    {
109
        this.userName = userName;
98
      return Action.ERROR;
110
    }
99
    }
111
100
-
 
101
    // User credentials are fine, check for admin rights
112
    public String getPw()
102
    if (u.isAdmin())
113
    {
103
    {
114
        return pw;
104
      return "admin";
115
    }
105
    }
116
-
 
117
    public void setPw(String pw)
106
    else
118
    {
107
    {
119
        this.pw = pw;
108
      return "user";
120
    }
109
    }
-
 
110
  }
121
111
-
 
112
  public String getUserName()
-
 
113
  {
-
 
114
    return this.userName;
-
 
115
  }
-
 
116
-
 
117
  public void setUserName(String userName)
-
 
118
  {
-
 
119
    this.userName = userName;
-
 
120
  }
-
 
121
-
 
122
  public String getPw()
-
 
123
  {
-
 
124
    return this.pw;
-
 
125
  }
-
 
126
-
 
127
  public void setPw(String pw)
-
 
128
  {
-
 
129
    this.pw = pw;
-
 
130
  }
-
 
131
122
    @Override
132
  @Override
123
    public User getModel()
133
  public User getModel()
124
    {
134
  {
125
        return user;
135
    return this.user;
126
    }
136
  }
127
}
137
}