core/orm/user.py

76 lines
2.7 KiB
Python
Raw Normal View History

from typing import List
2021-08-24 09:32:20 +00:00
from datetime import datetime
2021-08-20 08:08:32 +00:00
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Boolean, DateTime, JSON as JSONType
2021-08-17 09:14:26 +00:00
from sqlalchemy.orm import relationship
from orm import Permission
2021-08-05 16:49:08 +00:00
from orm.base import Base, local_session
2021-08-20 08:08:32 +00:00
from orm.rbac import Role
2021-08-20 08:08:32 +00:00
class UserNotifications(Base):
__tablename__ = 'user_notifications'
2021-08-19 15:33:39 +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-08-23 08:02:45 +00:00
class UserRatings(Base):
__tablename__ = "user_ratings"
id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True)
user_id = Column(ForeignKey('user.id'), primary_key = True)
value = Column(Integer)
2021-08-20 08:08:32 +00:00
UserRoles = Table("user_roles",
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')),
2021-08-20 09:27:19 +00:00
Column('role_id', Integer, ForeignKey('role.id'))
2021-08-20 08:08:32 +00:00
)
2021-08-17 09:14:26 +00:00
2021-08-26 21:14:20 +00:00
UserTopics = Table("user_topics",
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')),
Column('topic_id', Integer, ForeignKey('topic.id'))
)
class User(Base):
2021-08-20 08:08:32 +00:00
__tablename__ = "user"
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")
bio: str = Column(String, nullable=True, comment="Bio")
userpic: str = Column(String, nullable=True, comment="Userpic")
viewname: str = Column(String, nullable=True, comment="Display name")
rating: int = Column(Integer, nullable=True, comment="Rating")
2021-08-20 15:10:15 +00:00
slug: str = Column(String, unique=True, comment="User's slug")
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-08-19 15:33:39 +00:00
links: JSONType = Column(JSONType, nullable=True, comment="Links")
oauth: str = Column(String, nullable=True)
2021-08-20 08:08:32 +00:00
notifications = relationship(lambda: UserNotifications)
2021-08-23 08:02:45 +00:00
ratings = relationship(UserRatings, foreign_keys=UserRatings.user_id)
2021-08-20 08:08:32 +00:00
roles = relationship(lambda: Role, secondary=UserRoles)
2021-08-26 21:14:20 +00:00
topics = relationship(lambda: Topic, secondary=UserTopics)
2021-08-17 09:14:26 +00:00
2021-08-05 16:49:08 +00:00
@classmethod
def get_permission(cls, user_id):
2021-08-17 09:14:26 +00:00
scope = {}
2021-08-05 16:49:08 +00:00
with local_session() as session:
2021-08-17 09:14:26 +00:00
user = session.query(User).filter(User.id == user_id).first()
for role in user.roles:
for p in role.permissions:
2021-08-18 16:53:55 +00:00
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-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))