2022-10-14 09:01:36 +00:00
|
|
|
from collections import defaultdict
|
2022-11-11 07:04:31 +00:00
|
|
|
|
2022-10-14 09:01:36 +00:00
|
|
|
from assets.models import Account
|
2022-12-27 09:45:41 +00:00
|
|
|
from assets.const import AliasAccount
|
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-11-14 06:44:18 +00:00
|
|
|
|
2022-12-02 12:33:32 +00:00
|
|
|
def validate_permission(self, user, asset, account_name):
|
2022-11-16 03:29:02 +00:00
|
|
|
""" 校验用户有某个资产下某个账号名的权限
|
|
|
|
:param user: User
|
|
|
|
:param asset: Asset
|
2022-12-02 12:33:32 +00:00
|
|
|
:param account_name: 可能是 @USER @INPUT 字符串
|
2022-11-16 03:29:02 +00:00
|
|
|
"""
|
|
|
|
permed_accounts = self.get_permed_accounts_for_user(user, asset)
|
2022-12-15 08:02:34 +00:00
|
|
|
accounts_mapper = {account.alias: account for account in permed_accounts}
|
2022-12-02 12:33:32 +00:00
|
|
|
account = accounts_mapper.get(account_name)
|
2022-11-22 13:54:40 +00:00
|
|
|
return account
|
2022-11-16 03:29:02 +00:00
|
|
|
|
2022-11-15 09:49:28 +00:00
|
|
|
def get_permed_accounts_for_user(self, user, asset):
|
|
|
|
""" 获取授权给用户某个资产的账号 """
|
|
|
|
perms = self.get_permissions_for_user_asset(user, asset)
|
|
|
|
permed_accounts = self.get_permed_accounts_from_perms(perms, user, asset)
|
|
|
|
return permed_accounts
|
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_permed_accounts_from_perms(perms, user, asset):
|
2022-11-16 03:29:02 +00:00
|
|
|
# alias: is a collection of account usernames and special accounts [@ALL, @INPUT, @USER]
|
2022-11-11 07:04:31 +00:00
|
|
|
alias_action_bit_mapper = defaultdict(int)
|
|
|
|
alias_expired_mapper = defaultdict(list)
|
|
|
|
|
|
|
|
for perm in perms:
|
|
|
|
for alias in perm.accounts:
|
|
|
|
alias_action_bit_mapper[alias] |= perm.actions
|
|
|
|
alias_expired_mapper[alias].append(perm.date_expired)
|
|
|
|
|
|
|
|
asset_accounts = asset.accounts.all()
|
|
|
|
username_account_mapper = {account.username: account for account in asset_accounts}
|
2022-11-16 03:29:02 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
cleaned_accounts_action_bit = defaultdict(int)
|
|
|
|
cleaned_accounts_expired = defaultdict(list)
|
|
|
|
|
|
|
|
# @ALL 账号先处理,后面的每个最多映射一个账号
|
2022-12-27 09:45:41 +00:00
|
|
|
all_action_bit = alias_action_bit_mapper.pop(AliasAccount.ALL, None)
|
2022-11-11 07:04:31 +00:00
|
|
|
if all_action_bit:
|
|
|
|
for account in asset_accounts:
|
|
|
|
cleaned_accounts_action_bit[account] |= all_action_bit
|
2022-11-16 03:29:02 +00:00
|
|
|
cleaned_accounts_expired[account].extend(
|
2022-12-27 09:45:41 +00:00
|
|
|
alias_expired_mapper[AliasAccount.ALL]
|
2022-11-16 03:29:02 +00:00
|
|
|
)
|
2022-11-11 07:04:31 +00:00
|
|
|
|
|
|
|
for alias, action_bit in alias_action_bit_mapper.items():
|
2022-12-27 09:45:41 +00:00
|
|
|
if alias == AliasAccount.USER:
|
2022-11-11 07:04:31 +00:00
|
|
|
if user.username in username_account_mapper:
|
|
|
|
account = username_account_mapper[user.username]
|
|
|
|
else:
|
|
|
|
account = Account.get_user_account(user.username)
|
2022-12-27 09:45:41 +00:00
|
|
|
elif alias == AliasAccount.INPUT:
|
2022-11-11 07:04:31 +00:00
|
|
|
account = Account.get_manual_account()
|
|
|
|
elif alias in username_account_mapper:
|
|
|
|
account = username_account_mapper[alias]
|
|
|
|
else:
|
|
|
|
account = None
|
2022-10-14 09:01:36 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
if account:
|
|
|
|
cleaned_accounts_action_bit[account] |= action_bit
|
|
|
|
cleaned_accounts_expired[account].extend(alias_expired_mapper[alias])
|
|
|
|
|
|
|
|
accounts = []
|
|
|
|
for account, action_bit in cleaned_accounts_action_bit.items():
|
|
|
|
account.actions = action_bit
|
|
|
|
account.date_expired = max(cleaned_accounts_expired[account])
|
|
|
|
accounts.append(account)
|
2022-10-18 08:42:32 +00:00
|
|
|
return accounts
|