mirror of
https://github.com/ansible/awx.git
synced 2026-03-13 15:09:32 -02:30
Generalize the python boolean converter so we can use it in RBAC methods
This commit is contained in:
@@ -17,7 +17,7 @@ from rest_framework.exceptions import ParseError
|
||||
from rest_framework.filters import BaseFilterBackend
|
||||
|
||||
# Ansible Tower
|
||||
from awx.main.utils import get_type_for_model
|
||||
from awx.main.utils import get_type_for_model, to_python_boolean
|
||||
|
||||
class ActiveOnlyBackend(BaseFilterBackend):
|
||||
'''
|
||||
@@ -116,17 +116,6 @@ class FieldLookupBackend(BaseFilterBackend):
|
||||
new_lookup = '__'.join(new_parts)
|
||||
return field, new_lookup
|
||||
|
||||
def to_python_boolean(self, value, allow_none=False):
|
||||
value = unicode(value)
|
||||
if value.lower() in ('true', '1'):
|
||||
return True
|
||||
elif value.lower() in ('false', '0'):
|
||||
return False
|
||||
elif allow_none and value.lower() in ('none', 'null'):
|
||||
return None
|
||||
else:
|
||||
raise ValueError(u'Unable to convert "%s" to boolean' % unicode(value))
|
||||
|
||||
def to_python_related(self, value):
|
||||
value = unicode(value)
|
||||
if value.lower() in ('none', 'null'):
|
||||
@@ -136,11 +125,11 @@ class FieldLookupBackend(BaseFilterBackend):
|
||||
|
||||
def value_to_python_for_field(self, field, value):
|
||||
if isinstance(field, models.NullBooleanField):
|
||||
return self.to_python_boolean(value, allow_none=True)
|
||||
return to_python_boolean(value, allow_none=True)
|
||||
elif isinstance(field, models.BooleanField):
|
||||
return self.to_python_boolean(value)
|
||||
return to_python_boolean(value)
|
||||
elif isinstance(field, RelatedObject):
|
||||
return self.to_python_related(value)
|
||||
return to_python_related(value)
|
||||
else:
|
||||
return field.to_python(value)
|
||||
|
||||
|
||||
@@ -184,14 +184,14 @@ class UserAccess(BaseAccess):
|
||||
|
||||
def can_add(self, data):
|
||||
if data is not None and 'is_superuser' in data:
|
||||
if bool(data['is_superuser']) and not self.user.is_superuser:
|
||||
if to_python_boolean(data['is_superuser'], allow_none=True) and not self.user.is_superuser:
|
||||
return False
|
||||
return bool(self.user.is_superuser or
|
||||
self.user.admin_of_organizations.filter(active=True).exists())
|
||||
|
||||
def can_change(self, obj, data):
|
||||
if data is not None and 'is_superuser' in data:
|
||||
if bool(data['is_superuser']) and not self.user.is_superuser:
|
||||
if to_python_boolean(data['is_superuser'], allow_none=True) and not self.user.is_superuser:
|
||||
return False
|
||||
# A user can be changed if they are themselves, or by org admins or
|
||||
# superusers. Change permission implies changing only certain fields
|
||||
|
||||
@@ -61,6 +61,16 @@ def get_object_or_403(klass, *args, **kwargs):
|
||||
except queryset.model.MultipleObjectsReturned, e:
|
||||
raise PermissionDenied(*e.args)
|
||||
|
||||
def to_python_boolean(value, allow_none=False):
|
||||
value = unicode(value)
|
||||
if value.lower() in ('true', '1'):
|
||||
return True
|
||||
elif value.lower() in ('false', '0'):
|
||||
return False
|
||||
elif allow_none and value.lower() in ('none', 'null'):
|
||||
return None
|
||||
else:
|
||||
raise ValueError(u'Unable to convert "%s" to boolean' % unicode(value))
|
||||
|
||||
def camelcase_to_underscore(s):
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user