Browse Source

Merge pull request #3268 from jumpserver/v52_bugfix

[Update] 修复密码匣子的bug
pull/3269/head
BaiJiangJie 5 years ago committed by GitHub
parent
commit
326f2160d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      apps/assets/api/asset_user.py
  2. 22
      apps/assets/backends/asset_user.py
  3. 5
      apps/assets/backends/base.py

2
apps/assets/api/asset_user.py

@ -49,7 +49,7 @@ class AssetUserSearchBackend(filters.BaseFilterBackend):
if field in ("node_id", "system_user_id", "admin_user_id"):
continue
_queryset |= queryset.filter(**{field: value})
return _queryset
return _queryset.distinct()
class AssetUserViewSet(IDInCacheFilterMixin, BulkModelViewSet):

22
apps/assets/backends/asset_user.py

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
#
from collections import defaultdict
from .base import BaseBackend
@ -23,6 +24,7 @@ class AssetUserBackend(BaseBackend):
queryset = queryset.filter(username=username)
if assets:
queryset = queryset.filter(assets__in=assets).distinct()
queryset = cls.filter_queryset_more(queryset)
instances = cls.construct_authbook_objects(queryset, assets)
return instances
@ -30,10 +32,26 @@ class AssetUserBackend(BaseBackend):
@classmethod
def construct_authbook_objects(cls, asset_users, assets):
instances = []
assets_user_assets_map = defaultdict(set)
if isinstance(asset_users, list):
assets_user_assets_map = {
asset_user.id: asset_user.assets.values_list('id', flat=True)
for asset_user in asset_users
}
else:
assets_user_assets = asset_users.values_list('id', 'assets')
for i, asset_id in assets_user_assets:
assets_user_assets_map[i].add(asset_id)
for asset_user in asset_users:
if not assets:
assets = asset_user.assets.all()
for asset in assets:
related_assets = asset_user.assets.all()
else:
assets_map = {a.id: a for a in assets}
related_assets = [
assets_map.get(i) for i in assets_user_assets_map.get(asset_user.id) if i in assets_map
]
for asset in related_assets:
instance = asset_user.construct_to_authbook(asset)
instance.backend = cls.backend
instances.append(instance)

5
apps/assets/backends/base.py

@ -81,6 +81,11 @@ class AssetUserQuerySet(list):
queryset = self.filter_in(kwargs).filter_equal(kwargs)
return queryset
def distinct(self):
items = list(set(self))
self[:] = items
return self
def __or__(self, other):
self.extend(other)
return self

Loading…
Cancel
Save