Search in sources :

Example 1 with Account

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;
}
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)

Example 2 with Account

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;
}
Also used : Account(mom.trd.opentheso.bdd.account.Account) User(mom.trd.opentheso.bdd.account.User) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

Account (mom.trd.opentheso.bdd.account.Account)2 User (mom.trd.opentheso.bdd.account.User)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Vector (java.util.Vector)1 NamingException (javax.naming.NamingException)1 InitialDirContext (javax.naming.directory.InitialDirContext)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 Group (mom.trd.opentheso.bdd.account.Group)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