Search in sources :

Example 1 with Group

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;
}
Also used : Account(mom.trd.opentheso.bdd.account.Account) Group(mom.trd.opentheso.bdd.account.Group) User(mom.trd.opentheso.bdd.account.User) AuthenticationException(mom.trd.opentheso.bdd.auth.exceptions.AuthenticationException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Vector(java.util.Vector) InvalidAuthBeanException(mom.trd.opentheso.bdd.auth.exceptions.InvalidAuthBeanException) SQLException(java.sql.SQLException) AuthenticatorConnectionException(mom.trd.opentheso.bdd.auth.exceptions.AuthenticatorConnectionException) AuthenticationException(mom.trd.opentheso.bdd.auth.exceptions.AuthenticationException)

Aggregations

ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Vector (java.util.Vector)1 Account (mom.trd.opentheso.bdd.account.Account)1 Group (mom.trd.opentheso.bdd.account.Group)1 User (mom.trd.opentheso.bdd.account.User)1 AuthenticationException (mom.trd.opentheso.bdd.auth.exceptions.AuthenticationException)1 AuthenticatorConnectionException (mom.trd.opentheso.bdd.auth.exceptions.AuthenticatorConnectionException)1 InvalidAuthBeanException (mom.trd.opentheso.bdd.auth.exceptions.InvalidAuthBeanException)1