示例#1
0
class UserAssetGrantedTreeNodeRelation(OrgModelMixin, FamilyMixin, models.JMSBaseModel):
    class NodeFrom(ChoiceSet):
        granted = 'granted', 'Direct node granted'
        child = 'child', 'Have children node'
        asset = 'asset', 'Direct asset granted'

    user = models.ForeignKey('users.User', db_constraint=False, on_delete=models.CASCADE)
    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
示例#2
0
class UserGrantedMappingNode(FamilyMixin, models.JMSBaseModel):
    node = models.ForeignKey('assets.Node', default=None, on_delete=models.CASCADE,
                             db_constraint=False, null=True, related_name='mapping_nodes')
    key = models.CharField(max_length=64, verbose_name=_("Key"), db_index=True)  # '1:1:1:1'
    user = models.ForeignKey('users.User', db_constraint=False, on_delete=models.CASCADE)
    granted = models.BooleanField(default=False, db_index=True)
    asset_granted = models.BooleanField(default=False, db_index=True)
    parent_key = models.CharField(max_length=64, default='', verbose_name=_('Parent key'), db_index=True)  # '1:1:1:1'
    assets_amount = models.IntegerField(default=0)

    GRANTED_DIRECT = 1
    GRANTED_INDIRECT = 2
    GRANTED_NONE = 0

    @classmethod
    def get_node_granted_status(cls, key, user):
        ancestor_keys = Node.get_node_ancestor_keys(key, with_self=True)
        has_granted = UserGrantedMappingNode.objects.filter(
            key__in=ancestor_keys, user=user
        ).values_list('granted', flat=True)
        if not has_granted:
            return cls.GRANTED_NONE
        if any(list(has_granted)):
            return cls.GRANTED_DIRECT
        return cls.GRANTED_INDIRECT
示例#3
0
class AssetPermission(BasePermission):
    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"))
    actions = models.IntegerField(choices=Action.DB_CHOICES,
                                  default=Action.ALL,
                                  verbose_name=_("Actions"))

    class Meta:
        unique_together = [('org_id', 'name')]
        verbose_name = _("Asset permission")
        ordering = ('name', )

    @lazyproperty
    def users_amount(self):
        return self.users.count()

    @lazyproperty
    def user_groups_amount(self):
        return self.user_groups.count()

    @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()

    @classmethod
    def get_queryset_with_prefetch(cls):
        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'))).order_by()

    def get_all_assets(self):
        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)
        assets = Asset.objects.filter(id__in=asset_ids)
        return assets
示例#4
0
class AssetPermission(BasePermission):
    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"))
    actions = models.IntegerField(choices=Action.DB_CHOICES, default=Action.ALL, verbose_name=_("Actions"))

    class Meta:
        unique_together = [('org_id', 'name')]
        verbose_name = _("Asset permission")
        ordering = ('name',)

    @lazyproperty
    def users_amount(self):
        return self.users.count()

    @lazyproperty
    def user_groups_amount(self):
        return self.user_groups.count()

    @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()

    @classmethod
    def get_queryset_with_prefetch(cls):
        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'))
        ).order_by()

    def get_all_assets(self):
        from assets.models import Node
        nodes_keys = self.nodes.all().values_list('key', flat=True)
        assets_ids = set(self.assets.all().values_list('id', flat=True))
        nodes_assets_ids = Node.get_nodes_all_assets_ids(nodes_keys)
        assets_ids.update(nodes_assets_ids)
        assets = Asset.objects.filter(id__in=assets_ids)
        return assets

    @classmethod
    def generate_fake(cls, count=100):
        from ..hands import User, Node, SystemUser
        import random

        org = get_current_org()
        if not org or not org.is_real():
            Organization.default().change_to()

        nodes = list(Node.objects.all())
        assets = list(Asset.objects.all())
        system_users = list(SystemUser.objects.all())
        users = User.objects.filter(username='******')

        for i in range(count):
            name = "fake_perm_to_admin_{}".format(str(uuid.uuid4())[:6])
            perm = cls(name=name)
            try:
                perm.save()
                perm.users.set(users)
                if system_users and len(system_users) > 3:
                    _system_users = random.sample(system_users, 3)
                elif system_users:
                    _system_users = [system_users[0]]
                else:
                    _system_users = []
                perm.system_users.set(_system_users)

                if nodes and len(nodes) > 3:
                    _nodes = random.sample(nodes, 3)
                else:
                    _nodes = [Node.default_node()]
                perm.nodes.set(_nodes)

                if assets and len(assets) > 3:
                    _assets = random.sample(assets, 3)
                elif assets:
                    _assets = [assets[0]]
                else:
                    _assets = []
                perm.assets.set(_assets)

                logger.debug('Generate fake perm: %s' % perm.name)

            except Exception as e:
                print('Error continue')
                continue