Initial implementation of user permissions
This commit is contained in:
parent
9b27899446
commit
f9ee7bfdc0
1 changed files with 37 additions and 2 deletions
|
@ -1,14 +1,49 @@
|
||||||
from nntp.tiny.db import DatabaseTable
|
import enum
|
||||||
|
|
||||||
|
from nntp.tiny.db import Database, DatabaseTable
|
||||||
from nntp.tiny.passwd import compare
|
from nntp.tiny.passwd import compare
|
||||||
|
|
||||||
|
class UserPermission(enum.Flag):
|
||||||
|
NONE = 0
|
||||||
|
READ = 1
|
||||||
|
POST = enum.auto()
|
||||||
|
KILL = enum.auto()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return {
|
||||||
|
1: 'READ',
|
||||||
|
2: 'POST',
|
||||||
|
3: 'KILL'
|
||||||
|
}[self.value]
|
||||||
|
|
||||||
class User(DatabaseTable):
|
class User(DatabaseTable):
|
||||||
name = 'server_user'
|
name = 'server_user'
|
||||||
key = 'id'
|
key = 'id'
|
||||||
columns = 'id', 'active', 'username', 'password', 'fullname', 'mail',
|
columns = 'id', 'active', 'username', 'password', 'fullname', 'mail',
|
||||||
|
|
||||||
def auth(self, password: str):
|
def auth(self, password: str):
|
||||||
if self.active is False or or self.password is None or self.password == '':
|
if self.active is False or self.password is None or self.password == '':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return compare(password, self.password)
|
return compare(password, self.password)
|
||||||
|
|
||||||
|
def permissions(self, db: Database) -> UserPermission:
|
||||||
|
perms = UserPermission.NONE
|
||||||
|
|
||||||
|
sql = """
|
||||||
|
select
|
||||||
|
perm.name
|
||||||
|
from
|
||||||
|
server_permission perm,
|
||||||
|
server_user_permission user_perm
|
||||||
|
where
|
||||||
|
perm.id = user_perm.permission_id
|
||||||
|
and user_perm.user_id = ?
|
||||||
|
"""
|
||||||
|
|
||||||
|
cr = db.execute(sql, self.id)
|
||||||
|
|
||||||
|
for row in cr.each():
|
||||||
|
perms |= UserPermission[row[0]]
|
||||||
|
|
||||||
|
return perms
|
||||||
|
|
Loading…
Add table
Reference in a new issue