DefaultLockOutProvider.java 6.34 KB
Newer Older
1
/*
2
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
15 16 17
 */
package org.jivesoftware.openfire.lockout;

18 19 20 21 22 23 24
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

25 26
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.StringUtils;
27 28
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
29 30

/**
31
 * The DefaultLockOutProvider works with the ofUserFlag table to maintain a list of disabled/locked out
32 33
 * accounts., and as the name implies, is the default LockOutProvider implementation.
 *
34 35 36 37
 * @author Daniel Henninger
 */
public class DefaultLockOutProvider implements LockOutProvider {

38 39
	private static final Logger Log = LoggerFactory.getLogger(DefaultLockOutProvider.class);

40 41
    private static final String FLAG_ID = "lockout";
    private static final String DELETE_FLAG =
42
            "DELETE FROM ofUserFlag WHERE username=? AND name='"+FLAG_ID+"'";
43
    private static final String ADD_FLAG =
44
            "INSERT INTO ofUserFlag VALUES(?,'"+FLAG_ID+"',?,?)";
45
    private static final String RETRIEVE_FLAG =
46
            "SELECT name,startTime,endTime FROM ofUserFlag WHERE username=? AND name='"+FLAG_ID+"'";
47 48 49 50 51 52 53 54 55

    /**
     * Constructs a new DefaultLockOutProvider
     */
    public DefaultLockOutProvider() {

    }

    /**
56
     * Default provider retrieves disabled status from ofUserFlag table.
57 58
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#getDisabledStatus(String)
     */
59
    @Override
60
    public LockOutFlag getDisabledStatus(String username) {
61 62 63 64 65 66 67 68 69 70
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        LockOutFlag ret = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(RETRIEVE_FLAG);
            pstmt.setString(1, username);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
71
                return null;
72 73 74 75 76 77 78 79 80 81 82 83 84
            }
            Date startTime = null;
            if (rs.getString(2) != null) {
                startTime = new Date(Long.parseLong(rs.getString(2).trim()));
            }
            Date endTime = null;
            if (rs.getString(3) != null) {
                endTime = new Date(Long.parseLong(rs.getString(3).trim()));
            }

            ret = new LockOutFlag(username, startTime, endTime);
        }
        catch (Exception e) {
85 86
            Log.error("Error loading lockout information from DB", e);
            return null;
87 88 89 90 91 92 93 94 95
        }
        finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }

        return ret;
    }

    /**
96
     * Default provider deletes existing flag, if it exists, and adds new described flag in ofUserFlag table.
97 98
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#setDisabledStatus(LockOutFlag)
     */
99
    @Override
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    public void setDisabledStatus(LockOutFlag flag) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_FLAG);
            pstmt.setString(1, flag.getUsername());
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            // Nothing to do.
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_FLAG);
            pstmt.setString(1, flag.getUsername());
            if (flag.getStartTime() != null) {
                pstmt.setString(2, StringUtils.dateToMillis(flag.getStartTime()));
            }
            else {
                pstmt.setNull(2, Types.VARCHAR);
            }
125 126
            if (flag.getEndTime() != null) {
                pstmt.setString(3, StringUtils.dateToMillis(flag.getEndTime()));
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            }
            else {
                pstmt.setNull(3, Types.VARCHAR);
            }
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            // Nothing to do.
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    /**
142
     * Default provider deletes existing flag from ofUserFlag table.
143 144
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#unsetDisabledStatus(String)
     */
145
    @Override
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    public void unsetDisabledStatus(String username) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_FLAG);
            pstmt.setString(1, username);
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            // Nothing to do.
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    /**
     * Default provider allows editing of disabled status.
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#isReadOnly()
     */
167
    @Override
168 169 170 171
    public boolean isReadOnly() {
        return false;
    }

172 173 174 175
    /**
     * Default provider allows delayed start to disabled status.
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#isDelayedStartSupported()
     */
176
    @Override
177 178 179 180 181 182 183 184
    public boolean isDelayedStartSupported() {
        return true;
    }

    /**
     * Default provider allows timeout of disabled status.
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#isTimeoutSupported()
     */
185
    @Override
186 187 188 189
    public boolean isTimeoutSupported() {
        return true;
    }

190 191 192 193
    /**
     * Default provider should be cached.
     * @see org.jivesoftware.openfire.lockout.LockOutProvider#shouldNotBeCached()
     */
194
    @Override
195 196 197 198
    public boolean shouldNotBeCached() {
        return false;
    }

199
}