def validate_permission(user, application, system_user, action='connect'): app_perm_ids = get_user_all_app_perm_ids(user) app_perm_ids = ApplicationPermission.applications.through.objects.filter( applicationpermission_id__in=app_perm_ids, application_id=application.id).values_list('applicationpermission_id', flat=True) app_perm_ids = set(app_perm_ids) app_perm_ids = ApplicationPermission.system_users.through.objects.filter( applicationpermission_id__in=app_perm_ids, systemuser_id=system_user.id).values_list('applicationpermission_id', flat=True) app_perm_ids = set(app_perm_ids) app_perms = ApplicationPermission.objects.filter( id__in=app_perm_ids).order_by('-date_expired') if app_perms: actions = set() actions_values = app_perms.values_list('actions', flat=True) for value in actions_values: _actions = Action.value_to_choices(value) actions.update(_actions) actions = list(actions) app_perm: ApplicationPermission = app_perms.first() expire_at = app_perm.date_expired.timestamp() else: actions = [] expire_at = time.time() # TODO: 组件改造API完成后统一通过actions判断has_perm has_perm = action in actions return has_perm, actions, expire_at
def validate_permission(user, asset, system_user, action='connect'): if not system_user.protocol in asset.protocols_as_dict.keys(): return False, time.time() asset_perm_ids = get_user_all_asset_perm_ids(user) asset_perm_ids_from_asset = AssetPermission.assets.through.objects.filter( assetpermission_id__in=asset_perm_ids, asset_id=asset.id).values_list('assetpermission_id', flat=True) nodes = asset.get_nodes() node_keys = set() for node in nodes: ancestor_keys = node.get_ancestor_keys(with_self=True) node_keys.update(ancestor_keys) node_ids = Node.objects.filter(key__in=node_keys).values_list('id', flat=True) node_ids = set(node_ids) asset_perm_ids_from_node = AssetPermission.nodes.through.objects.filter( assetpermission_id__in=asset_perm_ids, node_id__in=node_ids).values_list('assetpermission_id', flat=True) asset_perm_ids = {*asset_perm_ids_from_asset, *asset_perm_ids_from_node} asset_perm_ids = AssetPermission.system_users.through.objects.filter( assetpermission_id__in=asset_perm_ids, systemuser_id=system_user.id).values_list('assetpermission_id', flat=True) asset_perm_ids = set(asset_perm_ids) asset_perms = AssetPermission.objects.filter( id__in=asset_perm_ids).order_by('-date_expired') if asset_perms: actions = set() actions_values = asset_perms.values_list('actions', flat=True) for value in actions_values: _actions = Action.value_to_choices(value) actions.update(_actions) asset_perm: AssetPermission = asset_perms.first() actions = list(actions) expire_at = asset_perm.date_expired.timestamp() else: actions = [] expire_at = time.time() # TODO: 组件改造API完成后统一通过actions判断has_perm has_perm = action in actions return has_perm, actions, expire_at
def validate_permission(user, asset, system_user, action_name): if not system_user.protocol in asset.protocols_as_dict.keys(): return False, time.time() asset_perm_ids = get_user_all_asset_perm_ids(user) asset_perm_ids_from_asset = AssetPermission.assets.through.objects.filter( assetpermission_id__in=asset_perm_ids, asset_id=asset.id).values_list('assetpermission_id', flat=True) nodes = asset.get_nodes() node_keys = set() for node in nodes: ancestor_keys = node.get_ancestor_keys(with_self=True) node_keys.update(ancestor_keys) node_ids = Node.objects.filter(key__in=node_keys).values_list('id', flat=True) node_ids = set(node_ids) asset_perm_ids_from_node = AssetPermission.nodes.through.objects.filter( assetpermission_id__in=asset_perm_ids, node_id__in=node_ids).values_list('assetpermission_id', flat=True) asset_perm_ids = {*asset_perm_ids_from_asset, *asset_perm_ids_from_node} asset_perm_ids = AssetPermission.system_users.through.objects.filter( assetpermission_id__in=asset_perm_ids, systemuser_id=system_user.id).values_list('assetpermission_id', flat=True) asset_perm_ids = set(asset_perm_ids) asset_perms = AssetPermission.objects.filter( id__in=asset_perm_ids).order_by('-date_expired') for asset_perm in asset_perms: if action_name in Action.value_to_choices(asset_perm.actions): return True, asset_perm.date_expired.timestamp() return False, time.time()
def get(self, request, *args, **kwargs): asset_id = request.query_params.get('asset_id', '') system_id = request.query_params.get('system_user_id', '') action_name = request.query_params.get('action_name', '') try: asset_id = uuid.UUID(asset_id) system_id = uuid.UUID(system_id) except ValueError: return Response({'msg': False}, status=403) asset = get_object_or_404(Asset, id=asset_id) system_user = get_object_or_404(SystemUser, id=system_id) system_users_actions = get_asset_system_user_ids_with_actions_by_user(self.get_user(), asset) actions = system_users_actions.get(system_user.id) if actions is None: return Response({'msg': False}, status=403) if action_name in Action.value_to_choices(actions): return Response({'msg': True}, status=200) return Response({'msg': False}, status=403)
def to_representation(self, value): return Action.value_to_choices(value)