Search in sources :

Example 1 with DataIntegrityException

use of com.autentia.tnt.dao.DataIntegrityException in project TNTConcept by autentia.

the class UserDAO method searchByLogin.

/**
 * Get a user given her login.
 * @return the user
 * @param login login of user
 * @throws com.autentia.tnt.dao.DataNotFoundException if user does not exist
 * @throws com.autentia.tnt.dao.DataIntegrityException if user is duplicated (bad thing)
 * @throws com.autentia.tnt.dao.DataAccException if something fails accesind DB
 */
public User searchByLogin(String login) throws DataAccException, DataNotFoundException, DataIntegrityException {
    UserSearch search = new UserSearch();
    search.setLogin(login);
    List<User> found = super.search(User.class, search, null);
    if (found.size() == 0) {
        throw new DataNotFoundException(User.class, search);
    } else if (found.size() > 1) {
        throw new DataIntegrityException(User.class, "duplicate login '" + login + "'");
    } else {
        return found.get(0);
    }
}
Also used : DataNotFoundException(com.autentia.tnt.dao.DataNotFoundException) User(com.autentia.tnt.businessobject.User) DataIntegrityException(com.autentia.tnt.dao.DataIntegrityException) UserSearch(com.autentia.tnt.dao.search.UserSearch)

Example 2 with DataIntegrityException

use of com.autentia.tnt.dao.DataIntegrityException in project TNTConcept by autentia.

the class AuthenticationManager method jdbcSearchByLogin.

/**
 * <p>
 * Get user details given her login. This method accesses tables using JDBC. Thus, is should only be used if we need to
 * log into the single-user console to migrate tables (in that case, we cannot rely on Hibernate to get the users until
 * we have really finished migrating tables and data).
 * </p>
 *
 * @return a MiniUser object (minimal representation of a user)
 * @param login user login
 * @throws com.autentia.tnt.dao.DataNotFoundException if user does not exist
 * @throws com.autentia.tnt.dao.DataIntegrityException if user is duplicated in DB
 * @throws com.autentia.tnt.dao.DataAccException if something fails accessing DB
 */
private Principal jdbcSearchByLogin(String login) throws DataAccException, DataNotFoundException, DataIntegrityException {
    Session ses = HibernateUtil.getSessionFactory().openSession();
    Connection con = ses.connection();
    Principal ret;
    try {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            stmt = con.prepareStatement("select u.id,u.login,u.password,u.active,u.name,r.id roleId " + "from User u,Role r " + "where u.roleId=r.id " + "and u.login=?");
            stmt.setString(1, login);
            rs = stmt.executeQuery();
            if (rs.next()) {
                int roleId = rs.getInt("roleId");
                ret = new // In console mode we won't need departmentId so
                Principal(// In console mode we won't need departmentId so
                rs.getInt("id"), // In console mode we won't need departmentId so
                0, // 0.4 the field didn't exist
                rs.getString("login"), rs.getString("password"), rs.getBoolean("active"), rs.getString("name"), roleId, (roleId == administratorRoleId) ? new GrantedAuthority[] { Permission.Action_Console } : new GrantedAuthority[] {});
                if (rs.next()) {
                    throw new DataIntegrityException(User.class, "Duplicated user login: " + login);
                }
            } else {
                throw new DataNotFoundException(User.class, login);
            }
        } catch (SQLException e) {
            throw new DataAccException("Error loading user: " + login, e);
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException e) {
                    log.error("jdbcSearchByLogin - Error al liberar el resultset", e);
                }
            ;
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    log.error("jdbcSearchByLogin - Error al liberar el statement", e);
                }
            ;
        }
    } finally {
        ses.close();
    }
    return ret;
}
Also used : DataNotFoundException(com.autentia.tnt.dao.DataNotFoundException) DataAccException(com.autentia.tnt.dao.DataAccException) SQLException(java.sql.SQLException) DataIntegrityException(com.autentia.tnt.dao.DataIntegrityException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Session(org.hibernate.Session)

Example 3 with DataIntegrityException

use of com.autentia.tnt.dao.DataIntegrityException in project TNTConcept by autentia.

the class AuthenticationManager method loadUserByUsername.

/**
 * Load a User for ACEGI given its user name
 *
 * @param username user name
 * @throws org.acegisecurity.userdetails.UsernameNotFoundException
 * @throws org.springframework.dao.DataAccessException
 * @return the user object description as specified by ACEGI
 */
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    try {
        Version db = Version.getDatabaseVersion();
        Version code = Version.getApplicationVersion();
        if (db.compareTo(code, Version.MINOR) == 0) {
            log.info("loadUserByUsername - getting user " + username + " using Hibernate");
            User user = userDAO.searchByLogin(username);
            GrantedAuthority[] auths = userRolesService.getAuthorities(user);
            if (log.isDebugEnabled()) {
                StringBuilder sb = new StringBuilder();
                for (GrantedAuthority auth : auths) {
                    sb.append(auth);
                    sb.append(" ");
                }
                log.debug("loadUserByUsername - user roles: " + sb);
            }
            final Principal principal = new Principal(user, auths);
            // setting user preferred Locale
            final SettingSearch s = new SettingSearch();
            s.setName(SettingPath.GENERAL_PREFERRED_LOCALE);
            s.setOwnerId(user.getId());
            final List<Setting> vals = settings.search(s, null);
            final Setting val = (vals != null && vals.size() > 0) ? vals.get(0) : null;
            if (val != null) {
                final Locale local = new Locale(val.getValue());
                principal.setLocale(local);
            }
            return principal;
        } else {
            log.info("loadUserByUsername - getting user " + username + " using JDBC");
            return jdbcSearchByLogin(username);
        }
    } catch (SecException e) {
        log.warn("loadUserByUsername - exception", e);
        throw new DataRetrievalFailureException("Error getting roles for user: " + username, e);
    } catch (DataIntegrityException e) {
        log.warn("loadUserByUsername - exception", e);
        throw new DataIntegrityViolationException("Inconsistent user name: " + username, e);
    } catch (DataNotFoundException e) {
        log.warn("loadUserByUsername - exception", e);
        throw new UsernameNotFoundException("User not found: " + username, e);
    } catch (DataAccException e) {
        log.warn("loadUserByUsername - exception", e);
        throw new DataRetrievalFailureException("Error getting user: " + username, e);
    } catch (SQLException e) {
        log.warn("loadUserByUsername - exception", e);
        throw new DataRetrievalFailureException("Error getting user: " + username, e);
    }
}
Also used : Locale(java.util.Locale) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) DataNotFoundException(com.autentia.tnt.dao.DataNotFoundException) User(com.autentia.tnt.businessobject.User) DataAccException(com.autentia.tnt.dao.DataAccException) SecException(com.autentia.tnt.manager.security.exception.SecException) SQLException(java.sql.SQLException) DataIntegrityException(com.autentia.tnt.dao.DataIntegrityException) GrantedAuthority(org.acegisecurity.GrantedAuthority) Setting(com.autentia.tnt.businessobject.Setting) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Version(com.autentia.tnt.version.Version) SettingSearch(com.autentia.tnt.dao.search.SettingSearch) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException)

Aggregations

DataIntegrityException (com.autentia.tnt.dao.DataIntegrityException)3 DataNotFoundException (com.autentia.tnt.dao.DataNotFoundException)3 User (com.autentia.tnt.businessobject.User)2 DataAccException (com.autentia.tnt.dao.DataAccException)2 SQLException (java.sql.SQLException)2 Setting (com.autentia.tnt.businessobject.Setting)1 SettingSearch (com.autentia.tnt.dao.search.SettingSearch)1 UserSearch (com.autentia.tnt.dao.search.UserSearch)1 SecException (com.autentia.tnt.manager.security.exception.SecException)1 Version (com.autentia.tnt.version.Version)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Locale (java.util.Locale)1 GrantedAuthority (org.acegisecurity.GrantedAuthority)1 UsernameNotFoundException (org.acegisecurity.userdetails.UsernameNotFoundException)1 Session (org.hibernate.Session)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)1