Search in sources :

Example 1 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class ControllerUtils method checkAutoLogin.

/**
	 * Checks user credentials / automatic login.
	 * 
	 * @param userSession The UserSession instance associated to the user's session
	 * @return <code>true</code> if auto login was enabled and the user was sucessfuly 
	 * logged in.
	 * @throws DatabaseException
	 */
protected boolean checkAutoLogin(UserSession userSession) {
    String cookieName = SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA);
    Cookie cookie = this.getCookieTemplate(cookieName);
    Cookie hashCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH));
    Cookie autoLoginCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN));
    if (hashCookie != null && cookie != null && !cookie.getValue().equals(SystemGlobals.getValue(ConfigKeys.ANONYMOUS_USER_ID)) && autoLoginCookie != null && "1".equals(autoLoginCookie.getValue())) {
        String uid = cookie.getValue();
        String uidHash = hashCookie.getValue();
        // Load the user-specific security hash from the database
        try {
            UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
            String userHash = userDao.getUserAuthHash(Integer.parseInt(uid));
            if (userHash == null || userHash.trim().length() == 0) {
                return false;
            }
            String securityHash = MD5.crypt(userHash);
            if (securityHash.equals(uidHash)) {
                int userId = Integer.parseInt(uid);
                userSession.setUserId(userId);
                User user = userDao.selectById(userId);
                if (user == null || user.getId() != userId || user.isDeleted()) {
                    userSession.makeAnonymous();
                    return false;
                }
                this.configureUserSession(userSession, user);
                return true;
            }
        } catch (Exception e) {
            throw new DatabaseException(e);
        }
        userSession.makeAnonymous();
    }
    return false;
}
Also used : Cookie(javax.servlet.http.Cookie) User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) DatabaseException(net.jforum.exceptions.DatabaseException) DatabaseException(net.jforum.exceptions.DatabaseException) ForumException(net.jforum.exceptions.ForumException)

Example 2 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class ForumStartup method startDatabase.

/**
	 * Starts the database implementation
	 * @return <code>true</code> if everthing were ok
	 * @throws DatabaseException if something were wrong
	 */
public static boolean startDatabase() {
    try {
        if (DBConnection.createInstance()) {
            DBConnection.getImplementation().init();
            // Check if we're in fact up and running
            Connection conn = DBConnection.getImplementation().getConnection();
            DBConnection.getImplementation().releaseConnection(conn);
        }
    } catch (Exception e) {
        throw new DatabaseException("Error while trying to start the database: " + e, e);
    }
    return true;
}
Also used : Connection(java.sql.Connection) DatabaseException(net.jforum.exceptions.DatabaseException) DatabaseException(net.jforum.exceptions.DatabaseException) RepositoryStartupException(net.jforum.exceptions.RepositoryStartupException)

Example 3 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericConfigDAO method insert.

/**
	 * @see net.jforum.dao.ConfigDAO#insert(net.jforum.entities.Config)
	 */
public void insert(Config config) {
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ConfigModel.insert"));
        p.setString(1, config.getName());
        p.setString(2, config.getValue());
        p.executeUpdate();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 4 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method setModerated.

/**
	 * @see net.jforum.dao.ForumDAO#setModerated(int, boolean)
	 */
public void setModerated(int categoryId, boolean status) {
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setModerated"));
        p.setInt(1, status ? 1 : 0);
        p.setInt(2, categoryId);
        p.executeUpdate();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 5 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericForumDAO method changeForumOrder.

private Forum changeForumOrder(Forum forum, Forum related) {
    int tmpOrder = related.getOrder();
    related.setOrder(forum.getOrder());
    forum.setOrder(tmpOrder);
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
        p.setInt(1, forum.getOrder());
        p.setInt(2, forum.getId());
        p.executeUpdate();
        p.close();
        p = null;
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ForumModel.setOrderById"));
        p.setInt(1, related.getOrder());
        p.setInt(2, related.getId());
        p.executeUpdate();
        return this.selectById(forum.getId());
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Aggregations

DatabaseException (net.jforum.exceptions.DatabaseException)264 PreparedStatement (java.sql.PreparedStatement)255 SQLException (java.sql.SQLException)254 ResultSet (java.sql.ResultSet)138 List (java.util.List)64 ArrayList (java.util.ArrayList)63 Timestamp (java.sql.Timestamp)17 User (net.jforum.entities.User)15 Iterator (java.util.Iterator)12 Post (net.jforum.entities.Post)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Topic (net.jforum.entities.Topic)8 Connection (java.sql.Connection)5 Bookmark (net.jforum.entities.Bookmark)5 KarmaStatus (net.jforum.entities.KarmaStatus)4 SimpleDateFormat (java.text.SimpleDateFormat)3 PrivateMessage (net.jforum.entities.PrivateMessage)3 Ranking (net.jforum.entities.Ranking)3