mirror of
https://github.com/ansible/awx.git
synced 2026-01-16 04:10:44 -03:30
refactor step 1, get access_class within method
This commit is contained in:
parent
fa08c8d4bb
commit
398e9466f8
@ -1515,6 +1515,7 @@ class InventoryList(ListCreateAPIView):
|
||||
|
||||
model = Inventory
|
||||
serializer_class = InventorySerializer
|
||||
capabilities_prefetch = ['admin', 'adhoc']
|
||||
|
||||
def get_queryset(self):
|
||||
qs = Inventory.accessible_objects(self.request.user, 'read_role')
|
||||
@ -1522,15 +1523,20 @@ class InventoryList(ListCreateAPIView):
|
||||
return qs
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
if not hasattr(self, 'capabilities_prefetch'):
|
||||
return super(ListCreateAPIView, self).list(request, *args, **kwargs)
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
|
||||
page = self.paginate_queryset(queryset)
|
||||
readable_ids = [obj.id for obj in page]
|
||||
editable_ids = Inventory.accessible_objects(request.user, 'admin_role').filter(pk__in=readable_ids).values_list('pk', flat=True)
|
||||
adhoc_ids = Inventory.accessible_objects(request.user, 'adhoc_role').filter(pk__in=readable_ids).values_list('pk', flat=True)
|
||||
for obj in page:
|
||||
obj.capabilities_cache = {'edit': False, 'adhoc': False}
|
||||
if obj.pk in editable_ids:
|
||||
obj.capabilities_cache['edit'] = True
|
||||
if obj.pk in adhoc_ids:
|
||||
obj.capabilities_cache['adhoc'] = True
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@ -225,7 +225,6 @@ class BaseAccess(object):
|
||||
for display_method in ['edit', 'delete', 'start', 'schedule', 'copy', 'adhoc']:
|
||||
# Custom ordering of methods used so we can reuse earlier calcs
|
||||
if display_method not in method_list:
|
||||
print ' Programming error: declared unavailable method'
|
||||
continue
|
||||
|
||||
# Grab the answer from the cache, if available
|
||||
@ -255,28 +254,33 @@ class BaseAccess(object):
|
||||
if method == 'add':
|
||||
data = {}
|
||||
|
||||
access_instance = self
|
||||
obj_check = obj
|
||||
if isinstance(obj, (Group, Host)):
|
||||
if method == 'start':
|
||||
if obj.inventory_source:
|
||||
obj = obj.inventory_source
|
||||
obj_check = obj.inventory_source
|
||||
else:
|
||||
user_capabilities[method] = False
|
||||
continue
|
||||
else:
|
||||
obj = obj.inventory
|
||||
obj_check = obj.inventory
|
||||
access_class = access_registry.get(type(obj_check), [])[0]
|
||||
access_instance = access_class(self.user)
|
||||
if isinstance(obj, JobTemplate):
|
||||
data = {'reference_obj': obj}
|
||||
|
||||
try:
|
||||
if method in ['change']: # 3 args
|
||||
user_capabilities[display_method] = self.user.can_access(type(obj), method, obj, data)
|
||||
elif method in ['delete', 'start', 'adhoc']: # 2 args
|
||||
user_capabilities[display_method] = self.user.can_access(type(obj), method, obj)
|
||||
elif method in ['add']: # 2 args with data
|
||||
user_capabilities[display_method] = self.user.can_access(type(obj), method, data)
|
||||
except Exception as exc:
|
||||
user_capabilities[display_method] = False
|
||||
print(exc)
|
||||
# try:
|
||||
access_method = getattr(access_instance, "can_%s" % method)
|
||||
if method in ['change']: # 3 args
|
||||
user_capabilities[display_method] = access_method(obj_check, data)
|
||||
elif method in ['delete', 'start', 'run_ad_hoc_commands']: # 2 args
|
||||
user_capabilities[display_method] = access_method(obj_check)
|
||||
elif method in ['add']: # 2 args with data
|
||||
user_capabilities[display_method] = access_method(data)
|
||||
# except Exception as exc:
|
||||
# user_capabilities[display_method] = False
|
||||
# print(exc)
|
||||
|
||||
return user_capabilities
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user