1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 15:00:01 +00:00

Add helper functions to models.User

This commit is contained in:
TheAMM 2017-05-18 15:28:51 +03:00
parent 6b4d487314
commit 6bfb65172c

View file

@ -343,6 +343,16 @@ class User(db.Model):
def __repr__(self): def __repr__(self):
return '<User %r>' % self.username return '<User %r>' % self.username
def validate_authorization(self, password):
''' Returns a boolean for whether the user can be logged in '''
checks = [
# Password must match
password == self.password_hash,
# Reject inactive and banned users
self.status == UserStatusType.ACTIVE
]
return all(checks)
@classmethod @classmethod
def by_id(cls, id): def by_id(cls, id):
return cls.query.get(id) return cls.query.get(id)
@ -357,6 +367,10 @@ class User(db.Model):
user = cls.query.filter_by(email=email).first() user = cls.query.filter_by(email=email).first()
return user return user
@classmethod
def by_username_or_email(cls, username_or_email):
return cls.by_username(username_or_email) or cls.by_email(username_or_email)
@property @property
def is_admin(self): def is_admin(self):
return self.level is UserLevelType.ADMIN or self.level is UserLevelType.SUPERADMIN return self.level is UserLevelType.ADMIN or self.level is UserLevelType.SUPERADMIN