SQLite Database Authorization and ACL

查看原文

SQLite 数据库也可以使用简单的 ACL,方法是通过 sqlite 内建的 authorizer. SQLite 连接对象可以设置 authorizer 控制 sql 的操作是否可以通过: https://www.sqlite.org/c3ref/set_authorizer.html

简单来说,这个 authorizer 方法可以接受几个设定的参数,你自己需要写哪些操作可以通过,哪些不可以。例如:

import sqlite3

db = sqlite3.connect('/tmp/auth-demo.db')

def authorizer(action, arg1, arg2, db_name, trigger_name):
    if action == SQLITE_DELETE and arg1 == 'users':
        return SQLITE_DENY  # 1
    elif action == SQLITE_READ and arg1 == 'users' and arg2 == 'password':
        return SQLITE_IGNORE  # 2
    return SQLITE_OK  # 0

db.set_authorizer(authorizer)

cursor = db.execute('SELECT * FROM users;')

for username, password in cursor.fetchall():
    print(username, password)  # Password will be None (NULL)