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