latosa/user.py

50 lines
1.5 KiB
Python
Raw Normal View History

2024-03-02 16:15:54 +01:00
from flask import Flask
from flask_bcrypt import Bcrypt
class User:
2024-03-02 23:59:31 +01:00
def __init__(self, app: Flask, uid: str, display_name: str, email:str, password: str, admin: bool = False, groups: list[str] = []):
2024-03-02 16:15:54 +01:00
self.display_name = display_name
self.email = email
2024-03-02 23:59:31 +01:00
self.groups = groups
2024-03-02 16:15:54 +01:00
self.is_active = False
2024-03-02 23:59:31 +01:00
self.is_admin = admin
2024-03-02 16:15:54 +01:00
self.is_anonymous = False
2024-03-02 23:59:31 +01:00
self.is_authenticated = False
self.uid = uid
2024-03-02 16:15:54 +01:00
self.bcrypt = Bcrypt(app)
self.salt = self.get_salt()
self.password_hash = self.bcrypt.generate_password_hash(password + self.salt).decode('utf-8')
def check_password(self, password: str):
return self.bcrypt.check_password_hash(self.password_hash, password + self.salt)
def get_id(self):
return self.uid
def get_display_name(self):
return self.display_name
def get_email(self):
return self.email
def get_salt(self):
return "salt"
def set_active(self, active: bool):
self.is_active = active
def set_authenticated(self, authenticated: bool):
self.is_authenticated = authenticated
def set_anonymous(self, anonymous: bool):
self.is_anonymous = anonymous
def set_admin(self, admin: bool):
self.is_admin = admin
def set_email(self, email: str):
self.email = email
def set_password(self, password: str):
self.password_hash = self.bcrypt.generate_password_hash(password + self.salt).decode('utf-8')