use of mom.trd.opentheso.bdd.account.Group in project opentheso by miledrousset.
the class DBAuthenticator method login.
/* (non-Javadoc)
* @see fr.persee.aldo.auth.Authenticator#login(java.lang.String, java.lang.String)
*/
public Account login(String login, String password) throws AuthenticationException {
Account acc = null;
try {
// Get statement from newly created SQL connection
Statement stmt = this.conn.createStatement();
// Check if the pair (login, password) is valid
String query = "SELECT * FROM users WHERE login='" + login + "' AND passwd='" + password + "'";
// Execute query and store result
ResultSet rs = stmt.executeQuery(query);
if (!rs.next()) {
// Auth failed
log.error("Authentication failed for user " + login);
return null;
}
// User
User user = new User();
user.setUser(login);
user.setFirstname(rs.getString("firstname"));
user.setLastname(rs.getString("lastname"));
user.setMail(rs.getString("mail"));
// Account
acc = new Account();
acc.setBaseId(authBean.getBaseId());
acc.setUser(user);
// Login/password verified. Get the user groups.
query = "SELECT group_id, default_group FROM user_groups " + "WHERE user_login='" + login + "'";
// Execute query and store result
rs = stmt.executeQuery(query);
Vector<Group> groups = new Vector<Group>(10);
while (rs.next()) {
Group group = new Group();
if (rs.getBoolean("default_group")) {
// Default user group
group.setId(Integer.toString(rs.getInt("group_id")));
user.setUserGroup(group);
} else {
group.setId(Integer.toString(rs.getInt("group_id")));
groups.add(group);
}
}
// Account groups
if (groups.size() > 0) {
acc.setGroups((Group[]) groups.toArray(new Group[groups.size()]));
}
// Free JDBC ressources
rs.close();
stmt.close();
} catch (Exception e) {
log.fatal("Error while authenticating the user " + login, e);
throw new AuthenticationException();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException sqle) {
log.fatal("Error while closing the connection to the database", sqle);
}
}
}
return acc;
}
Aggregations