permissions.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from dataclasses import dataclass
  2. from django.apps import apps
  3. from django.conf import settings
  4. from django.db.models import Model, Q
  5. from django.utils.translation import gettext_lazy as _
  6. from netbox.registry import registry
  7. from users.constants import CONSTRAINT_TOKEN_USER
  8. __all__ = (
  9. 'ModelAction',
  10. 'get_permission_for_model',
  11. 'permission_is_exempt',
  12. 'qs_filter_from_constraints',
  13. 'register_model_actions',
  14. 'resolve_permission',
  15. 'resolve_permission_type',
  16. )
  17. @dataclass
  18. class ModelAction:
  19. """
  20. Represents a custom permission action for a model.
  21. Attributes:
  22. name: The action identifier (e.g. 'sync', 'render_config')
  23. help_text: Optional description displayed in the ObjectPermission form
  24. """
  25. name: str
  26. help_text: str = ''
  27. def __hash__(self):
  28. return hash(self.name)
  29. def __eq__(self, other):
  30. if isinstance(other, ModelAction):
  31. return self.name == other.name
  32. return self.name == other
  33. def register_model_actions(model: type[Model], actions: list[ModelAction | str]):
  34. """
  35. Register custom permission actions for a model. These actions will appear as
  36. checkboxes in the ObjectPermission form when the model is selected.
  37. Args:
  38. model: The model class to register actions for
  39. actions: A list of ModelAction instances or action name strings
  40. """
  41. label = f'{model._meta.app_label}.{model._meta.model_name}'
  42. for action in actions:
  43. if isinstance(action, str):
  44. action = ModelAction(name=action)
  45. if action not in registry['model_actions'][label]:
  46. registry['model_actions'][label].append(action)
  47. def get_permission_for_model(model, action):
  48. """
  49. Resolve the named permission for a given model (or instance) and action (e.g. view or add).
  50. :param model: A model or instance
  51. :param action: View, add, change, or delete (string)
  52. """
  53. # Resolve to the "concrete" model (for proxy models)
  54. model = model._meta.concrete_model
  55. return f'{model._meta.app_label}.{action}_{model._meta.model_name}'
  56. def resolve_permission(name):
  57. """
  58. Given a permission name, return the app_label, action, and model_name components. For example, "dcim.view_site"
  59. returns ("dcim", "view", "site").
  60. :param name: Permission name in the format <app_label>.<action>_<model>
  61. """
  62. try:
  63. app_label, codename = name.split('.')
  64. action, model_name = codename.rsplit('_', 1)
  65. except ValueError:
  66. raise ValueError(
  67. _("Invalid permission name: {name}. Must be in the format <app_label>.<action>_<model>").format(name=name)
  68. )
  69. return app_label, action, model_name
  70. def resolve_permission_type(name):
  71. """
  72. Given a permission name, return the relevant ObjectType and action. For example, "dcim.view_site" returns
  73. (Site, "view").
  74. :param name: Permission name in the format <app_label>.<action>_<model>
  75. """
  76. from core.models import ObjectType
  77. app_label, action, model_name = resolve_permission(name)
  78. try:
  79. object_type = ObjectType.objects.get_by_natural_key(app_label=app_label, model=model_name)
  80. except ObjectType.DoesNotExist:
  81. raise ValueError(_("Unknown app_label/model_name for {name}").format(name=name))
  82. return object_type, action
  83. def permission_is_exempt(name):
  84. """
  85. Determine whether a specified permission is exempt from evaluation.
  86. :param name: Permission name in the format <app_label>.<action>_<model>
  87. """
  88. app_label, action, model_name = resolve_permission(name)
  89. if action == 'view':
  90. if (
  91. # All models (excluding those in EXEMPT_EXCLUDE_MODELS) are exempt from view permission enforcement
  92. '*' in settings.EXEMPT_VIEW_PERMISSIONS and (app_label, model_name) not in settings.EXEMPT_EXCLUDE_MODELS
  93. ) or (
  94. # This specific model is exempt from view permission enforcement
  95. f'{app_label}.{model_name}' in settings.EXEMPT_VIEW_PERMISSIONS
  96. ):
  97. return True
  98. return False
  99. def qs_filter_from_constraints(constraints, tokens=None):
  100. """
  101. Construct a Q filter object from an iterable of ObjectPermission constraints.
  102. Args:
  103. tokens: A dictionary mapping string tokens to be replaced with a value.
  104. """
  105. if tokens is None:
  106. tokens = {}
  107. User = apps.get_model('users.User')
  108. for token, value in tokens.items():
  109. if token == CONSTRAINT_TOKEN_USER and isinstance(value, User):
  110. tokens[token] = value.id
  111. def _replace_tokens(value, tokens):
  112. if type(value) is list:
  113. return list(map(lambda v: tokens.get(v, v), value))
  114. return tokens.get(value, value)
  115. params = Q()
  116. for constraint in constraints:
  117. if constraint:
  118. params |= Q(**{k: _replace_tokens(v, tokens) for k, v in constraint.items()})
  119. else:
  120. # Found null constraint; permit model-level access
  121. return Q()
  122. return params