Subversion Repositories WebE

Rev

Rev 30 | Go to most recent revision | Show entire file | Regard 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
-
 
29
  /**
-
 
30
   *
-
 
31
   */
27
    public LoginAction()
32
  public LoginAction()
28
    {
33
  {
29
    }
34
  }
30
35
31
    public String doLogin()
36
  public String doLogin()
32
    {
37
  {
33
38
34
        // If password or user name are empty, the login fails.
39
    // If password or user name are empty, the login fails.
35
        if ("".equals(getUserName()) || "".equals(getPw())
40
    if ("".equals(this.getUserName()) || "".equals(this.getPw())
36
                || getUserName() == null || getPw() == null)
41
        || this.getUserName() == null || this.getPw() == null)
37
        {
42
    {
38
            addFieldError("userName", "Falscher Username oder Passwort");
43
      this.addFieldError("userName", "Falscher Username oder Passwort");
39
            return ERROR;
44
      return Action.ERROR;
40
        }
45
    }
41
46
42
        String verifiedUser = verifyUser(getUserName(), getPw());
47
    String verifiedUser = this.verifyUser(this.getUserName(), this.getPw());
43
        if (verifiedUser.equals("failed"))
48
    if (verifiedUser.equals("failed"))
44
        {
49
    {
45
            addFieldError("userName", "Falscher Username oder Passwort");
50
      this.addFieldError("userName", "Falscher Username oder Passwort");
46
            return ERROR;
51
      return Action.ERROR;
47
        }
52
    }
48
        else
53
    else
49
        {
54
    {
50
55
51
            // Put user name, password into session
56
      // Put user name, password into session
52
            session.put("username", getUserName());
57
      this.session.put("username", this.getUserName());
53
            session.put("pw", getPw());
58
      this.session.put("pw", this.getPw());
54
            return verifiedUser;
59
      return verifiedUser;
55
        }
60
    }
56
    }
61
  }
57
62
58
    /**
63
  /**
59
     * Logout ausführen. Zerstört die Daten in der Session
64
   * Logout ausf�hren. Zerst�rt die Daten in der Session
-
 
65
   *
60
     * @return String
66
   * @return String
61
     */
67
   */
62
    public String doLogout()
68
  public String doLogout()
63
    {
69
  {
64
        //Kill Session content
70
    // Kill Session content
65
        ActionContext.getContext().getSession().clear();
71
    ActionContext.getContext().getSession().clear();
66
        return SUCCESS;
72
    return Action.SUCCESS;
67
    }
73
  }
68
74
69
    /**
75
  /**
70
     * Verify user credentials
76
   * Verify user credentials
71
     *
77
   *
Line 76... Line 82...
76
     * @return
82
   * @return
77
     */
83
   */
78
    public String verifyUser(String username, String password)
84
  public String verifyUser(String username, String password)
79
    {
85
  {
80
        // DB Query
86
    // DB Query
81
        User u = userDAO.searchUsername(username);
87
    User u = this.userDAO.searchUsername(username);
82
88
83
        // User does not exist
89
    // User does not exist
84
        if (u == null)
90
    if (u == null)
-
 
91
    {
85
            return ERROR;
92
      return Action.ERROR;
-
 
93
    }
86
94
87
        // User password does not match
95
    // User password does not match
88
        if (!u.getPassword().equals(password))
96
    if (!u.getPassword().equals(password))
-
 
97
    {
89
            return ERROR;
98
      return Action.ERROR;
-
 
99
    }
90
100
91
        // User credentials are fine, check for admin rights
101
    // User credentials are fine, check for admin rights
92
        if (u.isAdmin())
102
    if (u.isAdmin())
93
        {
103
    {
94
            return "admin";
104
      return "admin";
Line 99... Line 109...
99
        }
109
    }
100
    }
110
  }
101
111
102
    public String getUserName()
112
  public String getUserName()
103
    {
113
  {
104
        return userName;
114
    return this.userName;
105
    }
115
  }
106
116
107
    public void setUserName(String userName)
117
  public void setUserName(String userName)
108
    {
118
  {
109
        this.userName = userName;
119
    this.userName = userName;
110
    }
120
  }
111
121
112
    public String getPw()
122
  public String getPw()
113
    {
123
  {
114
        return pw;
124
    return this.pw;
115
    }
125
  }
116
126
117
    public void setPw(String pw)
127
  public void setPw(String pw)
118
    {
128
  {
119
        this.pw = pw;
129
    this.pw = pw;
120
    }
130
  }
121
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
}