2019-09-23 10:36:02 +00:00
|
|
|
import logging
|
2019-06-30 12:10:34 +00:00
|
|
|
from functools import reduce
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-02-05 05:29:29 +00:00
|
|
|
from django.db.models import F
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
from common.db.models import TextChoices
|
2021-02-05 05:29:29 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.db import models
|
2020-05-09 06:51:19 +00:00
|
|
|
from common.utils import lazyproperty
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.models import Asset, SystemUser, Node, FamilyMixin
|
2018-07-25 03:28:20 +00:00
|
|
|
|
2019-05-21 08:27:01 +00:00
|
|
|
from .base import BasePermission
|
2019-05-20 11:39:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2021-02-05 05:29:29 +00:00
|
|
|
'AssetPermission', 'Action', 'PermNode', 'UserAssetGrantedTreeNodeRelation',
|
2019-05-20 11:39:53 +00:00
|
|
|
]
|
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
|
|
|
|
|
|
|
|
2019-07-02 08:45:26 +00:00
|
|
|
class Action:
|
2019-07-06 06:02:56 +00:00
|
|
|
NONE = 0
|
2020-07-21 04:37:30 +00:00
|
|
|
|
|
|
|
CONNECT = 0b1
|
|
|
|
UPLOAD = 0b1 << 1
|
|
|
|
DOWNLOAD = 0b1 << 2
|
|
|
|
CLIPBOARD_COPY = 0b1 << 3
|
|
|
|
CLIPBOARD_PASTE = 0b1 << 4
|
|
|
|
ALL = 0xff
|
2019-06-30 12:10:34 +00:00
|
|
|
UPDOWNLOAD = UPLOAD | DOWNLOAD
|
2020-07-21 04:37:30 +00:00
|
|
|
CLIPBOARD_COPY_PASTE = CLIPBOARD_COPY | CLIPBOARD_PASTE
|
2019-06-28 14:07:22 +00:00
|
|
|
|
2019-07-01 10:22:40 +00:00
|
|
|
DB_CHOICES = (
|
2019-06-28 14:07:22 +00:00
|
|
|
(ALL, _('All')),
|
|
|
|
(CONNECT, _('Connect')),
|
2019-06-30 12:10:34 +00:00
|
|
|
(UPLOAD, _('Upload file')),
|
2019-06-28 14:07:22 +00:00
|
|
|
(DOWNLOAD, _('Download file')),
|
2019-07-01 10:22:40 +00:00
|
|
|
(UPDOWNLOAD, _("Upload download")),
|
2020-07-21 04:37:30 +00:00
|
|
|
(CLIPBOARD_COPY, _('Clipboard copy')),
|
|
|
|
(CLIPBOARD_PASTE, _('Clipboard paste')),
|
|
|
|
(CLIPBOARD_COPY_PASTE, _('Clipboard copy paste'))
|
2019-06-28 14:07:22 +00:00
|
|
|
)
|
|
|
|
|
2019-07-01 10:22:40 +00:00
|
|
|
NAME_MAP = {
|
|
|
|
ALL: "all",
|
|
|
|
CONNECT: "connect",
|
|
|
|
UPLOAD: "upload_file",
|
|
|
|
DOWNLOAD: "download_file",
|
|
|
|
UPDOWNLOAD: "updownload",
|
2020-07-21 04:37:30 +00:00
|
|
|
CLIPBOARD_COPY: 'clipboard_copy',
|
|
|
|
CLIPBOARD_PASTE: 'clipboard_paste',
|
|
|
|
CLIPBOARD_COPY_PASTE: 'clipboard_copy_paste'
|
2019-07-01 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 04:37:30 +00:00
|
|
|
NAME_MAP_REVERSE = {v: k for k, v in NAME_MAP.items()}
|
2019-07-01 10:22:40 +00:00
|
|
|
CHOICES = []
|
|
|
|
for i, j in DB_CHOICES:
|
|
|
|
CHOICES.append((NAME_MAP[i], j))
|
|
|
|
|
2019-06-30 12:10:34 +00:00
|
|
|
@classmethod
|
|
|
|
def value_to_choices(cls, value):
|
2020-12-11 09:21:52 +00:00
|
|
|
if isinstance(value, list):
|
|
|
|
return value
|
2019-06-30 12:10:34 +00:00
|
|
|
value = int(value)
|
2019-07-01 10:22:40 +00:00
|
|
|
choices = [cls.NAME_MAP[i] for i, j in cls.DB_CHOICES if value & i == i]
|
|
|
|
return choices
|
2019-06-30 12:10:34 +00:00
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@classmethod
|
|
|
|
def value_to_choices_display(cls, value):
|
|
|
|
choices = cls.value_to_choices(value)
|
2020-12-30 10:14:06 +00:00
|
|
|
return [str(dict(cls.choices())[i]) for i in choices]
|
2021-04-28 08:42:54 +00:00
|
|
|
|
2019-06-30 12:10:34 +00:00
|
|
|
@classmethod
|
|
|
|
def choices_to_value(cls, value):
|
2019-07-06 06:46:01 +00:00
|
|
|
if not isinstance(value, list):
|
|
|
|
return cls.NONE
|
|
|
|
db_value = [
|
|
|
|
cls.NAME_MAP_REVERSE[v] for v in value
|
|
|
|
if v in cls.NAME_MAP_REVERSE.keys()
|
|
|
|
]
|
|
|
|
if not db_value:
|
2019-07-06 06:02:56 +00:00
|
|
|
return cls.NONE
|
|
|
|
|
2019-07-01 10:22:40 +00:00
|
|
|
def to_choices(x, y):
|
|
|
|
return x | y
|
2019-07-06 06:46:01 +00:00
|
|
|
|
|
|
|
result = reduce(to_choices, db_value)
|
|
|
|
return result
|
2019-07-01 10:22:40 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(cls.NAME_MAP[i], j) for i, j in cls.DB_CHOICES]
|
2019-06-30 12:10:34 +00:00
|
|
|
|
2019-06-28 14:07:22 +00:00
|
|
|
|
2019-05-21 09:01:47 +00:00
|
|
|
class AssetPermission(BasePermission):
|
2018-04-07 16:16:37 +00:00
|
|
|
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"))
|
|
|
|
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_permissions', verbose_name=_("System user"))
|
2019-07-02 08:45:26 +00:00
|
|
|
actions = models.IntegerField(choices=Action.DB_CHOICES, default=Action.ALL, verbose_name=_("Actions"))
|
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',)
|
2018-07-27 08:21:55 +00:00
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
@lazyproperty
|
|
|
|
def users_amount(self):
|
|
|
|
return self.users.count()
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user_groups_amount(self):
|
|
|
|
return self.user_groups.count()
|
|
|
|
|
2020-05-09 06:51:19 +00:00
|
|
|
@lazyproperty
|
|
|
|
def assets_amount(self):
|
|
|
|
return self.assets.count()
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def nodes_amount(self):
|
|
|
|
return self.nodes.count()
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def system_users_amount(self):
|
|
|
|
return self.system_users.count()
|
|
|
|
|
2019-06-30 12:10:34 +00:00
|
|
|
@classmethod
|
|
|
|
def get_queryset_with_prefetch(cls):
|
2019-07-11 10:12:14 +00:00
|
|
|
return cls.objects.all().valid().prefetch_related(
|
|
|
|
models.Prefetch('nodes', queryset=Node.objects.all().only('key')),
|
|
|
|
models.Prefetch('assets', queryset=Asset.objects.all().only('id')),
|
|
|
|
models.Prefetch('system_users', queryset=SystemUser.objects.all().only('id'))
|
2020-02-05 07:56:28 +00:00
|
|
|
).order_by()
|
2018-04-11 07:24:12 +00:00
|
|
|
|
2019-07-02 09:48:29 +00:00
|
|
|
def get_all_assets(self):
|
2019-09-24 07:18:12 +00:00
|
|
|
from assets.models import Node
|
2019-07-12 10:31:14 +00:00
|
|
|
nodes_keys = self.nodes.all().values_list('key', flat=True)
|
2021-03-08 02:08:51 +00:00
|
|
|
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)
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
2019-07-02 09:48:29 +00:00
|
|
|
return assets
|
2019-09-23 10:36:02 +00:00
|
|
|
|
2021-04-28 08:42:54 +00:00
|
|
|
def users_display(self):
|
|
|
|
names = [user.username for user in self.users.all()]
|
|
|
|
return names
|
|
|
|
|
|
|
|
def user_groups_display(self):
|
|
|
|
names = [group.name for group in self.user_groups.all()]
|
|
|
|
return names
|
|
|
|
|
|
|
|
def assets_display(self):
|
|
|
|
names = [asset.hostname for asset in self.assets.all()]
|
|
|
|
return names
|
|
|
|
|
|
|
|
def system_users_display(self):
|
|
|
|
names = [system_user.name for system_user in self.system_users.all()]
|
|
|
|
return names
|
|
|
|
|
|
|
|
def nodes_display(self):
|
|
|
|
names = [node.full_value for node in self.nodes.all()]
|
|
|
|
return names
|
|
|
|
|
2019-09-23 10:36:02 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserAssetGrantedTreeNodeRelation(OrgModelMixin, FamilyMixin, models.JMSBaseModel):
|
2021-07-08 06:23:18 +00:00
|
|
|
class NodeFrom(TextChoices):
|
2021-02-05 05:29:29 +00:00
|
|
|
granted = 'granted', 'Direct node granted'
|
|
|
|
child = 'child', 'Have children node'
|
|
|
|
asset = 'asset', 'Direct asset granted'
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey('users.User', db_constraint=False, on_delete=models.CASCADE)
|
2021-02-05 05:29:29 +00:00
|
|
|
node = models.ForeignKey('assets.Node', default=None, on_delete=models.CASCADE,
|
|
|
|
db_constraint=False, null=False, related_name='granted_node_rels')
|
|
|
|
node_key = models.CharField(max_length=64, verbose_name=_("Key"), db_index=True)
|
|
|
|
node_parent_key = models.CharField(max_length=64, default='', verbose_name=_('Parent key'), db_index=True)
|
|
|
|
node_from = models.CharField(choices=NodeFrom.choices, max_length=16, db_index=True)
|
|
|
|
node_assets_amount = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def key(self):
|
|
|
|
return self.node_key
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parent_key(self):
|
|
|
|
return self.node_parent_key
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_node_granted_status(cls, user, key):
|
|
|
|
ancestor_keys = set(cls.get_node_ancestor_keys(key, with_self=True))
|
|
|
|
ancestor_rel_nodes = cls.objects.filter(user=user, node_key__in=ancestor_keys)
|
|
|
|
|
|
|
|
for rel_node in ancestor_rel_nodes:
|
|
|
|
if rel_node.key == key:
|
|
|
|
return rel_node.node_from, rel_node
|
|
|
|
if rel_node.node_from == cls.NodeFrom.granted:
|
|
|
|
return cls.NodeFrom.granted, None
|
|
|
|
return '', None
|
|
|
|
|
|
|
|
|
|
|
|
class PermNode(Node):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
ordering = []
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
# 特殊节点
|
|
|
|
UNGROUPED_NODE_KEY = 'ungrouped'
|
|
|
|
UNGROUPED_NODE_VALUE = _('Ungrouped')
|
|
|
|
FAVORITE_NODE_KEY = 'favorite'
|
|
|
|
FAVORITE_NODE_VALUE = _('Favorite')
|
|
|
|
|
|
|
|
node_from = ''
|
|
|
|
granted_assets_amount = 0
|
|
|
|
|
|
|
|
annotate_granted_node_rel_fields = {
|
|
|
|
'granted_assets_amount': F('granted_node_rels__node_assets_amount'),
|
|
|
|
'node_from': F('granted_node_rels__node_from')
|
|
|
|
}
|
|
|
|
|
|
|
|
def use_granted_assets_amount(self):
|
|
|
|
self.assets_amount = self.granted_assets_amount
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_ungrouped_node(cls, assets_amount):
|
|
|
|
return cls(
|
|
|
|
id=cls.UNGROUPED_NODE_KEY,
|
|
|
|
key=cls.UNGROUPED_NODE_KEY,
|
|
|
|
value=cls.UNGROUPED_NODE_VALUE,
|
|
|
|
assets_amount=assets_amount
|
|
|
|
)
|
2020-09-27 08:02:44 +00:00
|
|
|
|
|
|
|
@classmethod
|
2021-02-05 05:29:29 +00:00
|
|
|
def get_favorite_node(cls, assets_amount):
|
|
|
|
node = cls(
|
|
|
|
id=cls.FAVORITE_NODE_KEY,
|
|
|
|
key=cls.FAVORITE_NODE_KEY,
|
|
|
|
value=cls.FAVORITE_NODE_VALUE,
|
|
|
|
)
|
|
|
|
node.assets_amount = assets_amount
|
|
|
|
return node
|
|
|
|
|
|
|
|
def get_granted_status(self, user):
|
|
|
|
status, rel_node = UserAssetGrantedTreeNodeRelation.get_node_granted_status(user, self.key)
|
|
|
|
self.node_from = status
|
|
|
|
if rel_node:
|
|
|
|
self.granted_assets_amount = rel_node.node_assets_amount
|
|
|
|
return status
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
# 这是个只读 Model
|
|
|
|
raise NotImplementedError
|