2022-12-02 03:12:14 +00:00
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
2021-03-11 12:17:44 +00:00
|
|
|
from django.db import models
|
2022-12-02 03:12:14 +00:00
|
|
|
from django.db.models import Q
|
2023-04-20 07:46:12 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-03-11 12:17:44 +00:00
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2022-12-02 03:12:14 +00:00
|
|
|
from common.utils import contains_ip
|
2023-02-03 07:20:37 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin, OrgManager
|
2021-03-11 12:17:44 +00:00
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
__all__ = [
|
|
|
|
'ACLManager',
|
|
|
|
'BaseACL',
|
|
|
|
'BaseACLQuerySet',
|
|
|
|
'UserAssetAccountBaseACL',
|
|
|
|
'UserAssetAccountACLQuerySet'
|
|
|
|
]
|
2022-12-01 11:41:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ActionChoices(models.TextChoices):
|
|
|
|
reject = 'reject', _('Reject')
|
2022-12-02 09:52:51 +00:00
|
|
|
accept = 'accept', _('Accept')
|
2022-12-02 03:12:14 +00:00
|
|
|
review = 'review', _('Review')
|
2021-03-11 12:17:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseACLQuerySet(models.QuerySet):
|
|
|
|
def active(self):
|
|
|
|
return self.filter(is_active=True)
|
|
|
|
|
|
|
|
def inactive(self):
|
|
|
|
return self.filter(is_active=False)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
return self.active()
|
|
|
|
|
|
|
|
def invalid(self):
|
|
|
|
return self.inactive()
|
|
|
|
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
class UserAssetAccountACLQuerySet(BaseACLQuerySet):
|
|
|
|
def filter_user(self, username):
|
|
|
|
q = Q(users__username_group__contains=username) | \
|
2022-12-02 03:12:14 +00:00
|
|
|
Q(users__username_group__contains='*')
|
2022-12-03 16:04:39 +00:00
|
|
|
return self.filter(q)
|
|
|
|
|
|
|
|
def filter_asset(self, name=None, address=None):
|
|
|
|
queryset = self.filter()
|
|
|
|
if name:
|
|
|
|
q = Q(assets__name_group__contains=name) | \
|
|
|
|
Q(assets__name_group__contains='*')
|
|
|
|
queryset = queryset.filter(q)
|
|
|
|
if address:
|
|
|
|
ids = [
|
|
|
|
q.id for q in queryset
|
|
|
|
if contains_ip(address, q.assets.get('address_group', []))
|
|
|
|
]
|
|
|
|
queryset = queryset.filter(id__in=ids)
|
2022-12-02 03:12:14 +00:00
|
|
|
return queryset
|
|
|
|
|
2022-12-04 06:44:30 +00:00
|
|
|
def filter_account(self, username):
|
|
|
|
q = Q(accounts__username_group__contains=username) | \
|
2022-12-20 12:23:42 +00:00
|
|
|
Q(accounts__username_group__contains='*')
|
2022-12-03 16:04:39 +00:00
|
|
|
return self.filter(q)
|
2022-12-02 03:12:14 +00:00
|
|
|
|
|
|
|
|
2023-02-03 11:53:01 +00:00
|
|
|
class ACLManager(models.Manager):
|
2022-12-01 11:41:18 +00:00
|
|
|
def valid(self):
|
|
|
|
return self.get_queryset().valid()
|
|
|
|
|
|
|
|
|
2023-02-03 11:53:01 +00:00
|
|
|
class OrgACLManager(OrgManager, ACLManager):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class BaseACL(JMSBaseModel):
|
2021-03-11 12:17:44 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
|
|
|
priority = models.IntegerField(
|
|
|
|
default=50, verbose_name=_("Priority"),
|
|
|
|
help_text=_("1-100, the lower the value will be match first"),
|
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(100)]
|
|
|
|
)
|
2022-12-02 03:12:14 +00:00
|
|
|
action = models.CharField(max_length=64, default=ActionChoices.reject, verbose_name=_('Action'))
|
2022-12-01 11:41:18 +00:00
|
|
|
reviewers = models.ManyToManyField('users.User', blank=True, verbose_name=_("Reviewers"))
|
2021-03-11 12:17:44 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_("Active"))
|
|
|
|
|
2022-12-01 11:41:18 +00:00
|
|
|
ActionChoices = ActionChoices
|
2022-12-03 16:04:39 +00:00
|
|
|
objects = ACLManager.from_queryset(BaseACLQuerySet)()
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2021-03-11 12:17:44 +00:00
|
|
|
class Meta:
|
2023-02-02 06:53:18 +00:00
|
|
|
ordering = ('priority', 'date_updated', 'name')
|
2021-03-11 12:17:44 +00:00
|
|
|
abstract = True
|
2022-12-03 16:04:39 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def is_action(self, action):
|
|
|
|
return self.action == action
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
|
|
|
|
class UserAssetAccountBaseACL(BaseACL, OrgModelMixin):
|
|
|
|
# username_group
|
|
|
|
users = models.JSONField(verbose_name=_('User'))
|
|
|
|
# name_group, address_group
|
|
|
|
assets = models.JSONField(verbose_name=_('Asset'))
|
2022-12-04 06:44:30 +00:00
|
|
|
# username_group
|
2022-12-03 16:04:39 +00:00
|
|
|
accounts = models.JSONField(verbose_name=_('Account'))
|
|
|
|
|
2023-02-03 11:53:01 +00:00
|
|
|
objects = OrgACLManager.from_queryset(UserAssetAccountACLQuerySet)()
|
2022-12-03 16:04:39 +00:00
|
|
|
|
2022-12-05 05:27:51 +00:00
|
|
|
class Meta(BaseACL.Meta):
|
|
|
|
unique_together = ('name', 'org_id')
|
2022-12-03 16:04:39 +00:00
|
|
|
abstract = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def filter_queryset(cls, user=None, asset=None, account=None, account_username=None, **kwargs):
|
|
|
|
queryset = cls.objects.all()
|
|
|
|
org_id = None
|
|
|
|
if user:
|
|
|
|
queryset = queryset.filter_user(user.username)
|
|
|
|
if account:
|
|
|
|
org_id = account.org_id
|
2022-12-04 06:44:30 +00:00
|
|
|
queryset = queryset.filter_account(account.username)
|
2022-12-03 16:04:39 +00:00
|
|
|
if account_username:
|
|
|
|
queryset = queryset.filter_account(username=account_username)
|
2023-02-08 08:44:11 +00:00
|
|
|
if asset:
|
|
|
|
org_id = asset.org_id
|
|
|
|
queryset = queryset.filter_asset(asset.name, asset.address)
|
2022-12-03 16:04:39 +00:00
|
|
|
if org_id:
|
|
|
|
kwargs['org_id'] = org_id
|
|
|
|
if kwargs:
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
return queryset
|