2019-09-23 10:36:02 +00:00
|
|
|
import logging
|
2022-11-11 07:04:31 +00:00
|
|
|
import uuid
|
2022-11-09 03:43:55 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models import Q
|
2022-08-10 09:58:30 +00:00
|
|
|
from django.utils import timezone
|
2016-09-10 13:08:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
from assets.models import Asset, Account
|
|
|
|
from common.db.models import UnionQuerySet
|
|
|
|
from common.utils import date_expired_default
|
2022-08-10 09:58:30 +00:00
|
|
|
from orgs.mixins.models import OrgManager
|
2022-11-11 07:04:31 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2022-11-16 03:29:02 +00:00
|
|
|
from perms.const import ActionChoices
|
2019-05-20 11:39:53 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
__all__ = ['AssetPermission', 'ActionChoices']
|
2020-09-27 08:02:44 +00:00
|
|
|
|
|
|
|
# 使用场景
|
2019-09-23 10:36:02 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-04-22 03:42:20 +00:00
|
|
|
|
|
|
|
|
2022-08-10 09:58:30 +00:00
|
|
|
class AssetPermissionQuerySet(models.QuerySet):
|
|
|
|
def active(self):
|
|
|
|
return self.filter(is_active=True)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
return self.active().filter(date_start__lt=timezone.now()) \
|
|
|
|
.filter(date_expired__gt=timezone.now())
|
|
|
|
|
|
|
|
def inactive(self):
|
|
|
|
return self.filter(is_active=False)
|
|
|
|
|
|
|
|
def invalid(self):
|
|
|
|
now = timezone.now()
|
|
|
|
q = (Q(is_active=False) | Q(date_start__gt=now) | Q(date_expired__lt=now))
|
|
|
|
return self.filter(q)
|
|
|
|
|
2022-09-28 10:40:33 +00:00
|
|
|
def filter_by_accounts(self, accounts):
|
2022-09-30 08:08:28 +00:00
|
|
|
q = Q(accounts__contains=list(accounts)) | \
|
2022-11-16 03:29:02 +00:00
|
|
|
Q(accounts__contains=Account.AliasAccount.ALL.value)
|
2022-09-28 10:40:33 +00:00
|
|
|
return self.filter(q)
|
|
|
|
|
2022-08-10 09:58:30 +00:00
|
|
|
|
|
|
|
class AssetPermissionManager(OrgManager):
|
|
|
|
def valid(self):
|
|
|
|
return self.get_queryset().valid()
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermission(OrgModelMixin):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2022-10-13 12:14:04 +00:00
|
|
|
users = models.ManyToManyField(
|
|
|
|
'users.User', related_name='%(class)ss', blank=True, verbose_name=_("User")
|
|
|
|
)
|
|
|
|
user_groups = models.ManyToManyField(
|
|
|
|
'users.UserGroup', related_name='%(class)ss', blank=True, verbose_name=_("User group")
|
|
|
|
)
|
|
|
|
assets = models.ManyToManyField(
|
|
|
|
'assets.Asset', related_name='granted_by_permissions', blank=True, verbose_name=_("Asset")
|
|
|
|
)
|
|
|
|
nodes = models.ManyToManyField(
|
|
|
|
'assets.Node', related_name='granted_by_permissions', blank=True, verbose_name=_("Nodes")
|
|
|
|
)
|
|
|
|
# 特殊的账号: @ALL, @INPUT @USER 默认包含,将来在全局设置中进行控制.
|
2022-07-28 10:50:58 +00:00
|
|
|
accounts = models.JSONField(default=list, verbose_name=_("Accounts"))
|
2022-11-11 07:04:31 +00:00
|
|
|
actions = models.IntegerField(default=ActionChoices.connect, verbose_name=_("Actions"))
|
2022-11-17 10:04:38 +00:00
|
|
|
date_start = models.DateTimeField(default=timezone.now, db_index=True, verbose_name=_("Date start"))
|
2022-10-13 12:14:04 +00:00
|
|
|
date_expired = models.DateTimeField(
|
|
|
|
default=date_expired_default, db_index=True, verbose_name=_('Date expired')
|
|
|
|
)
|
2022-08-10 09:58:30 +00:00
|
|
|
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
2022-11-17 10:04:38 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
|
|
|
from_ticket = models.BooleanField(default=False, verbose_name=_('From ticket'))
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
|
|
|
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
|
2022-08-10 09:58:30 +00:00
|
|
|
|
|
|
|
objects = AssetPermissionManager.from_queryset(AssetPermissionQuerySet)()
|
2018-04-07 16:16:37 +00:00
|
|
|
|
2018-07-27 08:21:55 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = [('org_id', 'name')]
|
2018-09-03 03:24:25 +00:00
|
|
|
verbose_name = _("Asset permission")
|
2019-09-11 11:10:22 +00:00
|
|
|
ordering = ('name',)
|
2022-07-28 10:50:58 +00:00
|
|
|
permissions = []
|
2018-07-27 08:21:55 +00:00
|
|
|
|
2022-08-10 09:58:30 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_expired(self):
|
|
|
|
if self.date_expired > timezone.now() > self.date_start:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_valid(self):
|
|
|
|
if not self.is_expired and self.is_active:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_all_users(self):
|
|
|
|
from users.models import User
|
|
|
|
user_ids = self.users.all().values_list('id', flat=True)
|
|
|
|
group_ids = self.user_groups.all().values_list('id', flat=True)
|
|
|
|
user_ids = list(user_ids)
|
|
|
|
group_ids = list(group_ids)
|
|
|
|
qs1 = User.objects.filter(id__in=user_ids).distinct()
|
|
|
|
qs2 = User.objects.filter(groups__id__in=group_ids).distinct()
|
|
|
|
qs = UnionQuerySet(qs1, qs2)
|
|
|
|
return qs
|
|
|
|
|
2022-10-13 12:14:04 +00:00
|
|
|
def get_all_assets(self, flat=False):
|
|
|
|
from assets.models import Node
|
|
|
|
nodes_keys = self.nodes.all().values_list('key', flat=True)
|
|
|
|
asset_ids = set(self.assets.all().values_list('id', flat=True))
|
|
|
|
nodes_asset_ids = Node.get_nodes_all_asset_ids_by_keys(nodes_keys)
|
|
|
|
asset_ids.update(nodes_asset_ids)
|
|
|
|
if flat:
|
|
|
|
return asset_ids
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
|
|
|
return assets
|
|
|
|
|
2022-10-14 09:01:36 +00:00
|
|
|
def get_all_accounts(self, flat=False):
|
2022-10-14 06:56:38 +00:00
|
|
|
"""
|
|
|
|
:return: 返回授权的所有账号对象 Account
|
|
|
|
"""
|
|
|
|
asset_ids = self.get_all_assets(flat=True)
|
|
|
|
q = Q(asset_id__in=asset_ids)
|
2022-11-24 08:44:15 +00:00
|
|
|
if Account.AliasAccount.ALL not in self.accounts:
|
2022-10-14 06:56:38 +00:00
|
|
|
q &= Q(username__in=self.accounts)
|
2022-11-01 11:55:49 +00:00
|
|
|
accounts = Account.objects.filter(q).order_by('asset__name', 'name', 'username')
|
2022-10-14 09:01:36 +00:00
|
|
|
if not flat:
|
|
|
|
return accounts
|
|
|
|
return accounts.values_list('id', flat=True)
|