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
|
2023-04-20 07:46:12 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-03-11 12:17:44 +00:00
|
|
|
|
2023-04-26 11:11:53 +00:00
|
|
|
from common.db.fields import JSONManyToManyField
|
2022-12-20 12:23:42 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2023-05-29 11:45:55 +00:00
|
|
|
from common.utils import contains_ip
|
|
|
|
from common.utils.time_period import contains_time_period
|
2023-06-12 05:59:54 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin, OrgManager
|
2023-07-17 07:53:35 +00:00
|
|
|
from ..const import ActionChoices
|
2021-03-11 12:17:44 +00:00
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
__all__ = [
|
2023-06-07 09:39:56 +00:00
|
|
|
'BaseACL', 'UserBaseACL', 'UserAssetAccountBaseACL',
|
2022-12-03 16:04:39 +00:00
|
|
|
]
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2023-06-12 08:00:14 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2023-06-12 08:37:12 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
|
|
|
|
2022-12-01 11:41:18 +00:00
|
|
|
|
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-20 12:23:42 +00:00
|
|
|
class BaseACL(JMSBaseModel):
|
2023-06-07 09:39:56 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'), unique=True)
|
2021-03-11 12:17:44 +00:00
|
|
|
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
|
2023-05-08 06:09:44 +00:00
|
|
|
objects = BaseACLQuerySet.as_manager()
|
2022-12-01 11:41:18 +00:00
|
|
|
|
2021-03-11 12:17:44 +00:00
|
|
|
class Meta:
|
2023-06-15 02:45:07 +00:00
|
|
|
ordering = ('priority', '-is_active', '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
|
|
|
|
|
2023-05-29 11:45:55 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user_acls(cls, user):
|
|
|
|
return cls.objects.none()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_match_rule_acls(cls, user, ip, acl_qs=None):
|
|
|
|
if acl_qs is None:
|
|
|
|
acl_qs = cls.get_user_acls(user)
|
|
|
|
if not acl_qs:
|
|
|
|
return
|
|
|
|
|
|
|
|
for acl in acl_qs:
|
|
|
|
if acl.is_action(ActionChoices.review) and not acl.reviewers.exists():
|
|
|
|
continue
|
|
|
|
ip_group = acl.rules.get('ip_group')
|
|
|
|
time_periods = acl.rules.get('time_period')
|
|
|
|
is_contain_ip = contains_ip(ip, ip_group) if ip_group else True
|
|
|
|
is_contain_time_period = contains_time_period(time_periods) if time_periods else True
|
|
|
|
|
|
|
|
if is_contain_ip and is_contain_time_period:
|
|
|
|
# 满足条件,则返回
|
|
|
|
return acl
|
|
|
|
return None
|
|
|
|
|
2022-12-03 16:04:39 +00:00
|
|
|
|
2023-06-07 09:39:56 +00:00
|
|
|
class UserBaseACL(BaseACL):
|
2023-04-26 11:11:53 +00:00
|
|
|
users = JSONManyToManyField('users.User', default=dict, verbose_name=_('Users'))
|
2023-06-07 09:39:56 +00:00
|
|
|
|
2023-06-08 08:25:31 +00:00
|
|
|
class Meta(BaseACL.Meta):
|
2023-06-07 09:39:56 +00:00
|
|
|
abstract = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_user_acls(cls, user):
|
|
|
|
queryset = cls.objects.all()
|
2023-06-12 08:00:14 +00:00
|
|
|
with tmp_to_root_org():
|
|
|
|
q = cls.users.get_filter_q(user)
|
2023-06-07 09:39:56 +00:00
|
|
|
queryset = queryset.filter(q)
|
2023-06-08 10:33:43 +00:00
|
|
|
return queryset.filter(is_active=True).distinct()
|
2023-06-07 09:39:56 +00:00
|
|
|
|
|
|
|
|
2023-06-08 08:25:31 +00:00
|
|
|
class UserAssetAccountBaseACL(OrgModelMixin, UserBaseACL):
|
2023-06-07 09:39:56 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2023-04-26 11:11:53 +00:00
|
|
|
assets = JSONManyToManyField('assets.Asset', default=dict, verbose_name=_('Assets'))
|
2023-05-29 11:45:55 +00:00
|
|
|
accounts = models.JSONField(default=list, verbose_name=_("Accounts"))
|
2023-06-12 05:59:54 +00:00
|
|
|
objects = OrgManager.from_queryset(BaseACLQuerySet)()
|
2022-12-03 16:04:39 +00:00
|
|
|
|
2023-06-07 09:39:56 +00:00
|
|
|
class Meta(UserBaseACL.Meta):
|
|
|
|
unique_together = [('name', 'org_id')]
|
2022-12-03 16:04:39 +00:00
|
|
|
abstract = True
|
2023-05-25 10:42:54 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-09-21 02:15:10 +00:00
|
|
|
def _get_filter_queryset(cls, user=None, asset=None, account=None, account_username=None, **kwargs):
|
2023-05-25 10:42:54 +00:00
|
|
|
queryset = cls.objects.all()
|
2023-09-21 02:15:10 +00:00
|
|
|
q = models.Q()
|
2023-06-14 10:17:20 +00:00
|
|
|
|
2023-05-25 10:42:54 +00:00
|
|
|
if asset:
|
2023-09-21 02:15:10 +00:00
|
|
|
q &= cls.assets.get_filter_q(asset)
|
|
|
|
if user:
|
|
|
|
q &= cls.users.get_filter_q(user)
|
2023-05-25 10:42:54 +00:00
|
|
|
if account and not account_username:
|
|
|
|
account_username = account.username
|
|
|
|
if account_username:
|
2023-09-21 02:15:10 +00:00
|
|
|
q &= models.Q(accounts__contains=account_username) | \
|
|
|
|
models.Q(accounts__contains='*') | \
|
|
|
|
models.Q(accounts__contains='@ALL')
|
2023-05-25 10:42:54 +00:00
|
|
|
if kwargs:
|
2023-09-21 02:15:10 +00:00
|
|
|
q &= models.Q(**kwargs)
|
|
|
|
queryset = queryset.filter(q)
|
2023-06-12 05:59:54 +00:00
|
|
|
return queryset.valid().distinct()
|
2023-09-21 02:15:10 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def filter_queryset(cls, asset=None, **kwargs):
|
|
|
|
org_id = asset.org_id if asset else ''
|
|
|
|
with tmp_to_org(org_id):
|
|
|
|
return cls._get_filter_queryset(asset=asset, **kwargs)
|