core/orm/user.py

30 lines
941 B
Python
Raw Normal View History

from typing import List
2021-07-29 19:42:45 +00:00
from sqlalchemy import Column, Integer, String, ForeignKey #, relationship
from orm import Permission
2021-08-05 16:49:08 +00:00
from orm.base import Base, local_session
class User(Base):
2021-08-05 16:49:08 +00:00
__tablename__ = 'user'
2021-08-05 16:49:08 +00:00
email: str = Column(String, nullable=False)
username: str = Column(String, nullable=False, comment="Name")
password: str = Column(String, nullable=True, comment="Password")
2021-08-05 16:49:08 +00:00
role_id: list = Column(ForeignKey("role.id"), nullable=True, comment="Role")
# roles = relationship("Role") TODO: one to many, see schema.graphql
oauth_id: str = Column(String, nullable=True)
2021-08-05 16:49:08 +00:00
@classmethod
def get_permission(cls, user_id):
with local_session() as session:
perms: List[Permission] = session.query(Permission).join(User, User.role_id == Permission.role_id).filter(
User.id == user_id).all()
return {f"{p.operation_id}-{p.resource_id}" for p in perms}
if __name__ == '__main__':
2021-08-05 16:49:08 +00:00
print(User.get_permission(user_id=1))