use of mom.trd.opentheso.bdd.account.Account 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;
}
use of mom.trd.opentheso.bdd.account.Account in project opentheso by miledrousset.
the class LDAPAuthenticator method login.
/* (non-Javadoc)
* @see fr.persee.aldo.auth.Authenticator#login(java.lang.String, java.lang.String)
*/
public Account login(String login, String password) {
Account acc = null;
try {
String dn = dnFromUser(login);
if (dn == null) {
// TODO gerer exception
return null;
}
env.put(Context.SECURITY_PRINCIPAL, dn);
env.put(Context.SECURITY_CREDENTIALS, password);
InitialDirContext context = new InitialDirContext(env);
SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes(new String[] { authBean.getFirstnameLdap(), authBean.getNameLdap(), authBean.getMailLdap() });
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answers;
answers = context.search(authBean.getScope(), "(" + authBean.getUidLdap() + "=" + login + ")", ctrls);
SearchResult result;
result = answers.next();
// System.out.println();
String firstname = result.getAttributes().get(authBean.getFirstnameLdap()).get().toString();
String name = result.getAttributes().get(authBean.getNameLdap()).get().toString();
String mail = result.getAttributes().get(authBean.getMailLdap()).get().toString();
// User
User user = new User();
user.setUser(login);
user.setFirstname(firstname);
user.setLastname(name);
user.setMail(mail);
user.setUid(login);
// Account
acc = new Account();
acc.setBaseId(authBean.getBaseId());
acc.setUser(user);
} catch (NamingException e) {
}
return acc;
}
Aggregations