2022-10-14 09:01:36 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from assets.models import Account
|
2022-10-18 07:21:44 +00:00
|
|
|
from .permission import AssetPermissionUtil
|
2022-10-14 09:01:36 +00:00
|
|
|
|
2022-10-18 08:42:32 +00:00
|
|
|
__all__ = ['PermAccountUtil']
|
|
|
|
|
2022-10-14 09:01:36 +00:00
|
|
|
|
2022-10-18 07:21:44 +00:00
|
|
|
class PermAccountUtil(AssetPermissionUtil):
|
|
|
|
""" 资产授权账号相关的工具 """
|
2022-10-14 09:01:36 +00:00
|
|
|
|
2022-10-18 08:42:32 +00:00
|
|
|
def get_perm_accounts_for_user_asset(self, user, asset, with_actions=False):
|
2022-10-14 09:01:36 +00:00
|
|
|
""" 获取授权给用户某个资产的账号 """
|
2022-10-18 07:21:44 +00:00
|
|
|
perms = self.get_permissions_for_user_asset(user, asset)
|
2022-10-18 08:42:32 +00:00
|
|
|
accounts = self.get_perm_accounts_for_permissions(perms, with_actions=with_actions)
|
2022-10-14 09:53:54 +00:00
|
|
|
return accounts
|
|
|
|
|
2022-10-18 08:42:32 +00:00
|
|
|
def get_perm_accounts_for_user(self, user, with_actions=False):
|
2022-10-14 09:53:54 +00:00
|
|
|
""" 获取授权给用户的所有账号 """
|
2022-10-18 07:21:44 +00:00
|
|
|
perms = self.get_permissions_for_user(user)
|
2022-10-18 08:42:32 +00:00
|
|
|
accounts = self.get_perm_accounts_for_permissions(perms, with_actions=with_actions)
|
|
|
|
return accounts
|
|
|
|
|
|
|
|
def get_perm_accounts_for_user_group_asset(self, user_group, asset, with_actions=False):
|
|
|
|
perms = self.get_permissions_for_user_group_asset(user_group, asset)
|
|
|
|
accounts = self.get_perm_accounts_for_permissions(perms, with_actions=with_actions)
|
2022-10-14 09:53:54 +00:00
|
|
|
return accounts
|
|
|
|
|
|
|
|
@staticmethod
|
2022-10-18 08:42:32 +00:00
|
|
|
def get_perm_accounts_for_permissions(permissions, with_actions=False):
|
2022-10-14 09:53:54 +00:00
|
|
|
aid_actions_map = defaultdict(int)
|
|
|
|
for perm in permissions:
|
2022-10-14 09:01:36 +00:00
|
|
|
account_ids = perm.get_all_accounts(flat=True)
|
|
|
|
actions = perm.actions
|
|
|
|
for aid in account_ids:
|
|
|
|
aid_actions_map[str(aid)] |= actions
|
|
|
|
account_ids = list(aid_actions_map.keys())
|
|
|
|
accounts = Account.objects.filter(id__in=account_ids)
|
|
|
|
if with_actions:
|
|
|
|
for account in accounts:
|
|
|
|
account.actions = aid_actions_map.get(str(account.id))
|
|
|
|
return accounts
|
|
|
|
|