permissions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from django.conf import settings
  2. from django.db.models import Q
  3. from django.utils.translation import gettext_lazy as _
  4. __all__ = (
  5. 'get_permission_for_model',
  6. 'permission_is_exempt',
  7. 'qs_filter_from_constraints',
  8. 'resolve_permission',
  9. 'resolve_permission_type',
  10. )
  11. def get_permission_for_model(model, action):
  12. """
  13. Resolve the named permission for a given model (or instance) and action (e.g. view or add).
  14. :param model: A model or instance
  15. :param action: View, add, change, or delete (string)
  16. """
  17. # Resolve to the "concrete" model (for proxy models)
  18. model = model._meta.concrete_model
  19. return f'{model._meta.app_label}.{action}_{model._meta.model_name}'
  20. def resolve_permission(name):
  21. """
  22. Given a permission name, return the app_label, action, and model_name components. For example, "dcim.view_site"
  23. returns ("dcim", "view", "site").
  24. :param name: Permission name in the format <app_label>.<action>_<model>
  25. """
  26. try:
  27. app_label, codename = name.split('.')
  28. action, model_name = codename.rsplit('_', 1)
  29. except ValueError:
  30. raise ValueError(
  31. _("Invalid permission name: {name}. Must be in the format <app_label>.<action>_<model>").format(name=name)
  32. )
  33. return app_label, action, model_name
  34. def resolve_permission_type(name):
  35. """
  36. Given a permission name, return the relevant ObjectType and action. For example, "dcim.view_site" returns
  37. (Site, "view").
  38. :param name: Permission name in the format <app_label>.<action>_<model>
  39. """
  40. from core.models import ObjectType
  41. app_label, action, model_name = resolve_permission(name)
  42. try:
  43. object_type = ObjectType.objects.get_by_natural_key(app_label=app_label, model=model_name)
  44. except ObjectType.DoesNotExist:
  45. raise ValueError(_("Unknown app_label/model_name for {name}").format(name=name))
  46. return object_type, action
  47. def permission_is_exempt(name):
  48. """
  49. Determine whether a specified permission is exempt from evaluation.
  50. :param name: Permission name in the format <app_label>.<action>_<model>
  51. """
  52. app_label, action, model_name = resolve_permission(name)
  53. if action == 'view':
  54. if (
  55. # All models (excluding those in EXEMPT_EXCLUDE_MODELS) are exempt from view permission enforcement
  56. '*' in settings.EXEMPT_VIEW_PERMISSIONS and (app_label, model_name) not in settings.EXEMPT_EXCLUDE_MODELS
  57. ) or (
  58. # This specific model is exempt from view permission enforcement
  59. f'{app_label}.{model_name}' in settings.EXEMPT_VIEW_PERMISSIONS
  60. ):
  61. return True
  62. return False
  63. def qs_filter_from_constraints(constraints, tokens=None):
  64. """
  65. Construct a Q filter object from an iterable of ObjectPermission constraints.
  66. Args:
  67. tokens: A dictionary mapping string tokens to be replaced with a value.
  68. """
  69. if tokens is None:
  70. tokens = {}
  71. def _replace_tokens(value, tokens):
  72. if type(value) is list:
  73. return list(map(lambda v: tokens.get(v, v), value))
  74. return tokens.get(value, value)
  75. params = Q()
  76. for constraint in constraints:
  77. if constraint:
  78. params |= Q(**{k: _replace_tokens(v, tokens) for k, v in constraint.items()})
  79. else:
  80. # Found null constraint; permit model-level access
  81. return Q()
  82. return params