2021-08-24 09:32:20 +00:00
|
|
|
from datetime import datetime
|
2022-07-21 11:58:50 +00:00
|
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, DateTime, JSON as JSONType
|
|
|
|
from sqlalchemy.orm import relationship
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import Base, local_session
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.rbac import Role
|
|
|
|
from storages.roles import RoleStorage
|
2021-11-24 12:56:09 +00:00
|
|
|
|
2021-08-20 08:08:32 +00:00
|
|
|
class UserNotifications(Base):
|
|
|
|
__tablename__ = 'user_notifications'
|
2021-08-19 15:33:39 +00:00
|
|
|
|
2021-08-19 10:02:28 +00:00
|
|
|
id: int = Column(Integer, primary_key = True)
|
2021-08-19 15:33:39 +00:00
|
|
|
user_id: int = Column(Integer, ForeignKey("user.id"))
|
2021-08-20 08:08:32 +00:00
|
|
|
kind: str = Column(String, ForeignKey("notification.kind"))
|
|
|
|
values: JSONType = Column(JSONType, nullable = True) # [ <var1>, .. ]
|
|
|
|
|
2021-10-15 10:00:26 +00:00
|
|
|
class UserRating(Base):
|
|
|
|
__tablename__ = "user_rating"
|
2021-08-23 08:02:45 +00:00
|
|
|
|
|
|
|
id = None
|
2022-01-14 12:19:57 +00:00
|
|
|
rater = Column(ForeignKey('user.slug'), primary_key = True)
|
|
|
|
user = Column(ForeignKey('user.slug'), primary_key = True)
|
2021-08-23 08:02:45 +00:00
|
|
|
value = Column(Integer)
|
2021-08-20 08:08:32 +00:00
|
|
|
|
2021-11-24 09:09:47 +00:00
|
|
|
class UserRole(Base):
|
|
|
|
__tablename__ = "user_role"
|
|
|
|
|
|
|
|
id = None
|
|
|
|
user_id = Column(ForeignKey('user.id'), primary_key = True)
|
|
|
|
role_id = Column(ForeignKey('role.id'), primary_key = True)
|
2021-08-17 09:14:26 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
class AuthorFollower(Base):
|
|
|
|
__tablename__ = "author_follower"
|
2022-01-30 08:35:49 +00:00
|
|
|
|
|
|
|
id = None
|
2022-07-21 11:58:50 +00:00
|
|
|
follower = Column(ForeignKey('user.slug'), primary_key = True)
|
2022-01-30 08:35:49 +00:00
|
|
|
author = Column(ForeignKey('user.slug'), primary_key = True)
|
|
|
|
createdAt = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
|
|
|
|
2021-06-28 09:08:09 +00:00
|
|
|
class User(Base):
|
2021-08-20 08:08:32 +00:00
|
|
|
__tablename__ = "user"
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-19 10:02:28 +00:00
|
|
|
email: str = Column(String, unique=True, nullable=False, comment="Email")
|
|
|
|
username: str = Column(String, nullable=False, comment="Login")
|
2021-08-05 16:49:08 +00:00
|
|
|
password: str = Column(String, nullable=True, comment="Password")
|
2021-08-19 10:02:28 +00:00
|
|
|
bio: str = Column(String, nullable=True, comment="Bio")
|
|
|
|
userpic: str = Column(String, nullable=True, comment="Userpic")
|
2021-10-03 16:19:48 +00:00
|
|
|
name: str = Column(String, nullable=True, comment="Display name")
|
2021-08-20 15:10:15 +00:00
|
|
|
slug: str = Column(String, unique=True, comment="User's slug")
|
2021-08-19 10:02:28 +00:00
|
|
|
muted: bool = Column(Boolean, default=False)
|
|
|
|
emailConfirmed: bool = Column(Boolean, default=False)
|
2021-08-24 09:32:20 +00:00
|
|
|
createdAt: DateTime = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
|
|
|
wasOnlineAt: DateTime = Column(DateTime, nullable=False, default = datetime.now, comment="Was online at")
|
2021-12-08 10:29:04 +00:00
|
|
|
deletedAt: DateTime = Column(DateTime, nullable=True, comment="Deleted at")
|
2021-08-19 15:33:39 +00:00
|
|
|
links: JSONType = Column(JSONType, nullable=True, comment="Links")
|
2021-08-19 10:02:28 +00:00
|
|
|
oauth: str = Column(String, nullable=True)
|
2021-08-20 08:08:32 +00:00
|
|
|
notifications = relationship(lambda: UserNotifications)
|
2021-12-14 16:37:49 +00:00
|
|
|
ratings = relationship(UserRating, foreign_keys=UserRating.user)
|
2021-11-24 09:09:47 +00:00
|
|
|
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
|
2022-07-07 13:55:13 +00:00
|
|
|
oid: str = Column(String, nullable = True)
|
2021-12-10 13:52:55 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_table():
|
|
|
|
with local_session() as session:
|
2022-07-14 13:41:53 +00:00
|
|
|
default = session.query(User).filter(User.slug == "discours").first()
|
2021-12-10 13:52:55 +00:00
|
|
|
if not default:
|
|
|
|
default = User.create(
|
|
|
|
id = 0,
|
2022-07-14 13:41:53 +00:00
|
|
|
email = "welcome@discours.io",
|
|
|
|
username = "welcome@discours.io",
|
|
|
|
slug = "discours",
|
|
|
|
userpic = 'https://discours.io/images/logo-mini.svg',
|
2021-12-10 13:52:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
User.default_user = default
|
2021-08-17 09:14:26 +00:00
|
|
|
|
2021-11-24 15:53:01 +00:00
|
|
|
async def get_permission(self):
|
2021-08-17 09:14:26 +00:00
|
|
|
scope = {}
|
2021-11-24 15:53:01 +00:00
|
|
|
for user_role in self.roles:
|
|
|
|
role = await RoleStorage.get_role(user_role.id)
|
|
|
|
for p in role.permissions:
|
|
|
|
if not p.resource_id in scope:
|
|
|
|
scope[p.resource_id] = set()
|
|
|
|
scope[p.resource_id].add(p.operation_id)
|
2021-08-17 09:14:26 +00:00
|
|
|
return scope
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
|
2021-08-20 08:08:32 +00:00
|
|
|
if __name__ == "__main__":
|
2021-08-05 16:49:08 +00:00
|
|
|
print(User.get_permission(user_id=1))
|