From f9ee7bfdc0965619587016fa17863b2a38570335 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 29 Nov 2024 23:23:44 -0500 Subject: [PATCH] Initial implementation of user permissions --- lib/nntp/tiny/user.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/nntp/tiny/user.py b/lib/nntp/tiny/user.py index 7d4c671..e626be2 100644 --- a/lib/nntp/tiny/user.py +++ b/lib/nntp/tiny/user.py @@ -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 +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): name = 'server_user' key = 'id' columns = 'id', 'active', 'username', 'password', 'fullname', 'mail', 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 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