4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Login.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: Login.java
5  * Description: Backbean for login page
6  */
7 
8 /**
9  * @file Login.java
10  *
11  * @brief Backbean for login page
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
21 import java.io.Serializable;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.faces.bean.ManagedBean;
28 import javax.faces.bean.SessionScoped;
29 
30 /**
31  * Backbean for login page
32  *
33  * @brief Backbean for login page
34  * @author idytrych
35  */
36 @ManagedBean
37 @SessionScoped
38 public class Login implements Serializable {
39 
40  /**
41  * Value of login field in login form
42  */
43  private String login = "";
44  /**
45  * Value of password field in login form
46  */
47  private String password = "";
48  /**
49  * Indicator, whether bad credentials has been entered
50  */
51  private boolean badCredentials = false;
52 
53  /**
54  * Action listener for login button
55  *
56  * @return Returns page outcome (identificator of next page or null to stay here)
57  */
58  public String btnLoginAction() {
59  User user = null;
60  AppBean.getInstance();
61  PersistM persistMan = AppBean.getPersistenceManager();
62  if (login == null) { // null is a same as not set
63  login = "";
64  }
65  if (password == null) {
66  password = "";
67  }
68  if (login.isEmpty() || password.isEmpty()) { // if login or password is not set
69  badCredentials = true;
70  return null;
71  } else { // if credentials are set
72 
73  // compute MD5 of password
74  MessageDigest md5;
75  String hash = "";
76  try {
77  md5 = MessageDigest.getInstance("MD5");
78  md5.update(password.getBytes());
79  hash = MessageProcessor.getHexString(md5.digest());
80  } catch (NoSuchAlgorithmException ex) {
81  badCredentials = true;
83  String msg = "NoSuchAlgorithmException for computing MD5.";
84  Logger.getLogger(SelectAT.class.getName()).log(Level.SEVERE, msg, ex);
85  }
86  return null;
87  }
88 
89  // query database for user (find by login and password)
90  Object[] params = new Object[4];
91  params[0] = "login";
92  params[1] = login;
93  params[2] = "password";
94  params[3] = hash; // use MD5 of sent password
95  List uList = persistMan.queryDB("User.findByCredentials", params);
96  if (uList != null && !uList.isEmpty()) { // if user was found
97  user = (User) uList.get(0);
98  }
99 
100  if (user == null) { // user wasn't found, possible insecure login
101  // query database for user (find by login and password)
102  params[3] = password;
103  uList = persistMan.queryDB("User.findByCredentials", params);
104  if (uList != null && !uList.isEmpty()) { // if user was found
105  user = (User) uList.get(0);
106  }
107  }
108 
109  if (user == null) { // user wasn't found - bad credentials
110  badCredentials = true;
111  return null;
112  } else {
113  WebSession session = SessionManager.getSession();
114  session.setLoggedUser(user);
115  }
116  } // if credentials are set
117  badCredentials = false;
118 
119  // go to index page
120  Menu menuBean = (Menu) SessionManager.getBeanByName("menu");
121  menuBean.setActualPage("index");
122  return "index";
123  } // btnLoginAction()
124 
125  /**
126  * Gets value of login field in login form
127  *
128  * @return Returns value of login field in login form
129  */
130  public String getLogin() {
131  return login;
132  }
133 
134  /**
135  * Sets value of login field in login form
136  *
137  * @param login Value of login field in login form
138  */
139  public void setLogin(String login) {
140  this.login = login;
141  }
142 
143  /**
144  * Gets value of password field in login form
145  *
146  * @return Returns Value of password field in login form
147  */
148  public String getPassword() {
149  return password;
150  }
151 
152  /**
153  * Sets value of password field in login form
154  *
155  * @param password Value of password field in login form
156  */
157  public void setPassword(String password) {
158  this.password = password;
159  }
160 
161  /**
162  * Gets message for user, if bad credentials has been entered
163  *
164  * @return If bad credentials has been entered, returns message for user, empty string otherwise
165  */
166  public String getCredentialsError() {
167  if (badCredentials) {
168  return MessageProvider.getMessage("badCredentials");
169  } else {
170  return "";
171  }
172  }
173 
174  /**
175  * Gets indicator, whether bad credentials has been entered
176  *
177  * @return If bad credentials has been entered, returns true, false otherwise
178  */
179  public boolean getBadCredentials() {
180  return badCredentials;
181  }
182 
183  /**
184  * Sets, whether bad credentials has been entered
185  *
186  * @param badCredentials If bad credentials has been entered, then true, false otherwise
187  */
188  public void setBadCredentials(boolean badCredentials) {
189  this.badCredentials = badCredentials;
190  }
191 
192  /**
193  * Creates a new instance of Login bean
194  */
195  public Login() {
196  }
197 
198 } // public class Login
Persistence manager (database manipulator)
Definition: PersistM.java:35
void setPassword(String password)
Definition: Login.java:157
Class for manipulating with session.
Class for handling session variables in the web.
Definition: WebSession.java:37
Backbean for template.xhtml, class for menu handling.
Definition: Menu.java:28
Singleton for storing global variables.
Definition: AppBean.java:47
void setBadCredentials(boolean badCredentials)
Definition: Login.java:188
Static class which parses and process XML with messages.
Class representing user.
Definition: User.java:51
Backbean for login page.
Definition: Login.java:38