2021-06-28 09:08:09 +00:00
|
|
|
import warnings
|
2022-07-21 11:58:50 +00:00
|
|
|
from sqlalchemy import String, Column, ForeignKey, UniqueConstraint, TypeDecorator
|
|
|
|
from sqlalchemy.orm import relationship
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import Base, REGISTRY, engine, local_session
|
2021-12-08 12:51:30 +00:00
|
|
|
from orm.community import Community
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
|
2021-08-20 09:27:19 +00:00
|
|
|
class ClassType(TypeDecorator):
|
|
|
|
impl = String
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
@property
|
|
|
|
def python_type(self):
|
|
|
|
return NotImplemented
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
def process_literal_param(self, value, dialect):
|
|
|
|
return NotImplemented
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
def process_bind_param(self, value, dialect):
|
|
|
|
return value.__name__ if isinstance(value, type) else str(value)
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
def process_result_value(self, value, dialect):
|
|
|
|
class_ = REGISTRY.get(value)
|
|
|
|
if class_ is None:
|
2021-08-25 21:20:53 +00:00
|
|
|
warnings.warn(f"Can't find class <{value}>,find it yourself!", stacklevel=2)
|
2021-08-17 09:14:26 +00:00
|
|
|
return class_
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
class Role(Base):
|
2021-08-17 09:14:26 +00:00
|
|
|
__tablename__ = 'role'
|
2021-08-20 09:27:19 +00:00
|
|
|
|
|
|
|
name: str = Column(String, nullable=False, comment="Role Name")
|
2021-08-27 08:42:01 +00:00
|
|
|
desc: str = Column(String, nullable=True, comment="Role Description")
|
2021-08-26 21:14:20 +00:00
|
|
|
community: int = Column(ForeignKey("community.id", ondelete="CASCADE"), nullable=False, comment="Community")
|
2021-08-19 15:33:39 +00:00
|
|
|
permissions = relationship(lambda: Permission)
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-12-08 12:51:30 +00:00
|
|
|
@staticmethod
|
|
|
|
def init_table():
|
|
|
|
with local_session() as session:
|
|
|
|
default = session.query(Role).filter(Role.name == "author").first()
|
|
|
|
if default:
|
|
|
|
Role.default_role = default
|
|
|
|
return
|
|
|
|
|
|
|
|
default = Role.create(
|
|
|
|
name = "author",
|
|
|
|
desc = "Role for author",
|
|
|
|
community = Community.default_community.id
|
|
|
|
)
|
|
|
|
|
|
|
|
Role.default_role = default
|
|
|
|
|
2021-06-28 09:08:09 +00:00
|
|
|
class Operation(Base):
|
2021-08-17 09:14:26 +00:00
|
|
|
__tablename__ = 'operation'
|
|
|
|
name: str = Column(String, nullable=False, unique=True, comment="Operation Name")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_table():
|
|
|
|
with local_session() as session:
|
|
|
|
edit_op = session.query(Operation).filter(Operation.name == "edit").first()
|
|
|
|
if not edit_op:
|
|
|
|
edit_op = Operation.create(name = "edit")
|
|
|
|
Operation.edit_id = edit_op.id
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Resource(Base):
|
2021-08-17 09:14:26 +00:00
|
|
|
__tablename__ = "resource"
|
2021-08-24 09:32:20 +00:00
|
|
|
resource_class: str = Column(String, nullable=False, unique=True, comment="Resource class")
|
2021-08-17 09:14:26 +00:00
|
|
|
name: str = Column(String, nullable=False, unique=True, comment="Resource name")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init_table():
|
|
|
|
with local_session() as session:
|
|
|
|
shout_res = session.query(Resource).filter(Resource.name == "shout").first()
|
|
|
|
if not shout_res:
|
|
|
|
shout_res = Resource.create(name = "shout", resource_class = "shout")
|
|
|
|
Resource.shout_id = shout_res.id
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Permission(Base):
|
2021-08-17 09:14:26 +00:00
|
|
|
__tablename__ = "permission"
|
|
|
|
__table_args__ = (UniqueConstraint("role_id", "operation_id", "resource_id"), {"extend_existing": True})
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
role_id: int = Column(ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role")
|
|
|
|
operation_id: int = Column(ForeignKey("operation.id", ondelete="CASCADE"), nullable=False, comment="Operation")
|
2021-08-19 15:33:39 +00:00
|
|
|
resource_id: int = Column(ForeignKey("resource.id", ondelete="CASCADE"), nullable=False, comment="Resource")
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-08-17 09:14:26 +00:00
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
ops = [
|
|
|
|
Permission(role_id=1, operation_id=1, resource_id=1),
|
|
|
|
Permission(role_id=1, operation_id=2, resource_id=1),
|
|
|
|
Permission(role_id=1, operation_id=3, resource_id=1),
|
|
|
|
Permission(role_id=1, operation_id=4, resource_id=1),
|
|
|
|
Permission(role_id=2, operation_id=4, resource_id=1)
|
|
|
|
]
|
|
|
|
global_session.add_all(ops)
|
|
|
|
global_session.commit()
|