@classmethod @db_session definstance(cls, name, weight=None): """ Return an PermissionGroup instance.
If a PermissionGroup with the name exists, it will directly return the group, with new weight value. It is noticeable that, if weight is not given, it won't be changed.
Or, it will create a new PermissionGroup with the given name and weight value. Notice that if the weight is not given here, an ValueError will be thrown.
""" result = cls.get(name=name) if result isNone: if weight isNone: raise ValueError return cls(name=name, weight=weight) if weight isnotNone: result.weight = weight return result
If a PermissionNode with the permission exists, it will directly return the node, with new weight and owner value. It is noticeable that, if value or owner are not given, it won't be changed.
Or, it will create a new PermissionNode with the given permission, value, and owner value. Notice that if the weight or owner are not given here, an ValueError will be thrown.
""" result = cls.get(permission=permission) if result isNone: if value isNone: raise ValueError if owner isNone: raise ValueError return cls(permission=permission, value=value, owner=owner) if value isnotNone: result.value = value if owner isnotNone: result.owner = owner return result
+
models/permission_node.py
1 2 3 4 5 6 7 8 9 10 11
from pony.orm import Required, db_session
from .db import db from .permission_group import PermissionGroup
classPermissionNode(db.Entity): permission = Required(str) value = Required(bool) owner = Required(PermissionGroup)
from models import User from pony.orm import select
defcheck_permission(user: User, permission): queue = Queue() weight = float("-inf") value = False for group in user.permission_groups: queue.put(group) whilenot queue.empty(): group = queue.get() for g in group.parents: queue.put(g) if group.weight < weight: continue values = select(node.value for node in group.nodes if node.permission == permission) for v in values: value = v weight = group.weight return value
@classmethod @db_session definstance(cls, name, weight=None): """ Return an PermissionGroup instance.
If a PermissionGroup with the name exists, it will directly return the group, with new weight value. It is noticeable that, if weight is not given, it won't be changed.
Or, it will create a new PermissionGroup with the given name and weight value. Notice that if the weight is not given here, an ValueError will be thrown.
""" result = cls.get(name=name) if result isNone: if weight isNone: raise ValueError return cls(name=name, weight=weight) if weight isnotNone: result.weight = weight return result
If a PermissionNode with the permission exists, it will directly return the node, with new weight and owner value. It is noticeable that, if value or owner are not given, it won't be changed.
Or, it will create a new PermissionNode with the given permission, value, and owner value. Notice that if the weight or owner are not given here, an ValueError will be thrown.
""" result = cls.get(permission=permission) if result isNone: if value isNone: raise ValueError if owner isNone: raise ValueError return cls(permission=permission, value=value, owner=owner) if value isnotNone: result.value = value if owner isnotNone: result.owner = owner return result
from models import User from pony.orm import select
defcheck_permission(user: User, permission): queue = Queue() weight = float("-inf") value = False for group in user.permission_groups: queue.put(group) whilenot queue.empty(): group = queue.get() for g in group.parents: queue.put(g) if group.weight < weight: continue values = select(node.value for node in group.nodes if node.permission == permission) for v in values: value = v weight = group.weight return value
@classmethod @db_session definstance(cls, name, weight=None): """ Return an PermissionGroup instance.
If a PermissionGroup with the name exists, it will directly return the group, with new weight value. It is noticeable that, if weight is not given, it won't be changed.
Or, it will create a new PermissionGroup with the given name and weight value. Notice that if the weight is not given here, an ValueError will be thrown.
""" result = cls.get(name=name) if result isNone: if weight isNone: raise ValueError return cls(name=name, weight=weight) if weight isnotNone: result.weight = weight return result
models/permission_node.py
1 2 3 4 5 6 7 8 9 10 11
from pony.orm import Required, db_session
from .db import db from .permission_group import PermissionGroup
classPermissionNode(db.Entity): permission = Required(str) value = Required(bool) owner = Required(PermissionGroup)
from models import User from pony.orm import select
defcheck_permission(user: User, permission): queue = Queue() weight = float("-inf") value = False for group in user.permission_groups: queue.put(group) whilenot queue.empty(): group = queue.get() for g in group.parents: queue.put(g) if group.weight < weight: continue values = select(node.value for node in group.nodes if node.permission == permission) for v in values: value = v weight = group.weight return value