Selaa lähdekoodia

Implement is_admin(), is_owner().

tags/v0.1^2
Ben Kurtovic 12 vuotta sitten
vanhempi
commit
992faba70e
1 muutettua tiedostoa jossa 41 lisäystä ja 0 poistoa
  1. +41
    -0
      earwigbot/config/permissions.py

+ 41
- 0
earwigbot/config/permissions.py Näytä tiedosto

@@ -26,6 +26,9 @@ from threading import Lock
__all__ = ["PermissionsDB"] __all__ = ["PermissionsDB"]


class PermissionsDB(object): class PermissionsDB(object):
ADMIN = 1
OWNER = 2

def __init__(self, dbfile): def __init__(self, dbfile):
self._dbfile = dbfile self._dbfile = dbfile
self._db_access_lock = Lock() self._db_access_lock = Lock()
@@ -46,6 +49,15 @@ class PermissionsDB(object):
user_rank)""" user_rank)"""
conn.execute(query) conn.execute(query)


def _is_rank(self, user, rank):
"""Return True if the given user has the given rank, else False."""
try:
for rule in self._data[rank]:
if user == rule:
return True
except KeyError:
return False

def load(self): def load(self):
"""Load permissions from an existing database, or create a new one.""" """Load permissions from an existing database, or create a new one."""
query = "SELECT user_nick, user_ident, user_host, user_rank FROM users" query = "SELECT user_nick, user_ident, user_host, user_rank FROM users"
@@ -60,9 +72,38 @@ class PermissionsDB(object):
except sqlite.OperationalError: except sqlite.OperationalError:
self._create(conn) self._create(conn)


def is_admin(self, nick="*", ident="*", host="*"):
"""Return ``True`` if the given user is a bot admin, else ``False``."""
return self._is_rank(_User(nick, ident, host), rank=self.ADMIN)

def is_owner(self, nick="*", ident="*", host="*"):
"""Return ``True`` if the given user is a bot owner, else ``False``."""
return self._is_rank(_User(nick, ident, host), rank=self.OWNER)


class _User(object): class _User(object):
"""A class that represents an IRC user for the purpose of testing rules."""
def __init__(self, nick, ident, host): def __init__(self, nick, ident, host):
self.nick = nick self.nick = nick
self.ident = ident self.ident = ident
self.host = host self.host = host

def __repr__(self):
"""Return the canonical string representation of the User."""
res = "_User(nick={0!r}, ident={1!r}, host={2!r})"
return res.format(self.nick, self.ident, self.host)

def __str__(self):
"""Return a nice string representation of the User."""
return "{0}!{1}@{2}".format(self.nick, self.ident, self.host)

def __eq__(self, user):
if self.nick == user.nick or (self.nick == "*" or user.nick == "*"):
if self.ident == user.ident or (self.ident == "*" or
user.ident == "*"):
if self.host == user.host or (self.host == "*" or
user.host == "*"):
return True
return False

def __ne__(self, user):
return not self == user

Ladataan…
Peruuta
Tallenna