Skip to content

Commit

Permalink
Merge pull request #1201 from syucream/fix/enum-based-acltypes
Browse files Browse the repository at this point in the history
Make ACLType enum
  • Loading branch information
userlocalhost authored Jun 24, 2024
2 parents 438cf79 + 14baa97 commit 4106ca6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
2 changes: 1 addition & 1 deletion acl/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ACLBase(models.Model):
created_user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
is_active = models.BooleanField(default=True)
status = models.IntegerField(default=0)
default_permission = models.IntegerField(default=ACLType.Nothing().id)
default_permission = models.IntegerField(default=ACLType.Nothing.id)
created_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now=True)
deleted_user = models.ForeignKey(
Expand Down
2 changes: 0 additions & 2 deletions acl/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@ def test_operation_for_acltype(self):

self.assertTrue(type_readable == ACLType.Readable)
self.assertTrue(type_readable == ACLType.Readable.id)
self.assertTrue(type_readable == ACLType.Readable.name)

self.assertFalse(type_readable != ACLType.Readable)
self.assertFalse(type_readable != ACLType.Readable.id)
self.assertFalse(type_readable != ACLType.Readable.name)
self.assertTrue(type_readable != ACLType.Writable)

self.assertTrue(type_readable <= ACLType.Writable)
Expand Down
82 changes: 29 additions & 53 deletions airone/lib/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
__all__ = ["ACLType", "ACLObjType"]


class Iteratable(object):
def __iter__(self):
return self._types.__iter__()


@enum.unique
class ACLObjType(enum.IntEnum):
Entity = 1 << 0
Expand All @@ -16,54 +11,35 @@ class ACLObjType(enum.IntEnum):
EntryAttr = 1 << 3


class MetaACLType(type):
def __eq__(cls, comp):
if isinstance(comp, int):
return cls.id == comp
elif isinstance(comp, str):
return cls.name == comp
elif issubclass(comp, ACLTypeBase):
return cls.id == comp.id
else:
return False

def __ne__(cls, comp):
return not cls == comp

def __le__(cls, comp):
if isinstance(comp, int):
return cls.id <= comp
elif issubclass(comp, ACLTypeBase):
return cls.id <= comp.id
else:
return False


class ACLTypeBase(metaclass=MetaACLType):
pass


class ACLType(Iteratable):
Nothing = type(
"ACLTypeNone",
(ACLTypeBase,),
{"id": (1 << 0), "name": "nothing", "label": "Nothing"},
)
Readable = type(
"ACLTypeReadable",
(ACLTypeBase,),
{"id": (1 << 1), "name": "readable", "label": "Readable"},
)
Writable = type(
"ACLTypeWritable",
(ACLTypeBase,),
{"id": (1 << 2), "name": "writable", "label": "Writable"},
)
Full = type(
"ACLTypeFull",
(ACLTypeBase,),
{"id": (1 << 3), "name": "full", "label": "Full Controllable"},
)
class ACLType(enum.IntEnum):
Nothing = 1 << 0
Readable = 1 << 1
Writable = 1 << 2
Full = 1 << 3

@property
def id(self):
return self.value

@property
def name(self):
names = {
self.Nothing: "nothing",
self.Readable: "readable",
self.Writable: "writable",
self.Full: "full",
}
return names[self.value]

@property
def label(self):
labels = {
self.Nothing: "Nothing",
self.Readable: "Readable",
self.Writable: "Writable",
self.Full: "Full Controllable",
}
return labels[self.value]

@classmethod
def all(cls):
Expand Down
2 changes: 1 addition & 1 deletion role/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_current_permission(self, aclbase) -> int:
if permissions:
return permissions[0].get_aclid()
else:
return ACLType.Nothing().id
return ACLType.Nothing.id

def get_referred_entries(self, entity_name: str | None = None):
# make query to identify AttributeValue that specify this Role instance
Expand Down
4 changes: 2 additions & 2 deletions user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db import models
from rest_framework.authtoken.models import Token

from airone.lib.acl import ACLType, ACLTypeBase
from airone.lib.acl import ACLType
from group.models import Group
from role.models import Role

Expand Down Expand Up @@ -83,7 +83,7 @@ def has_permission(self, target_obj, permission_level) -> bool:
# This try-catch syntax is needed because the 'issubclass' may occur a
# TypeError exception when permission_level is not object.
try:
if not issubclass(permission_level, ACLTypeBase):
if not isinstance(permission_level, ACLType):
return False
except TypeError:
return False
Expand Down

0 comments on commit 4106ca6

Please sign in to comment.