views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. from typing import Iterable
  2. from django.conf import settings
  3. from django.contrib.auth.mixins import AccessMixin
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.urls import reverse
  6. from django.urls.exceptions import NoReverseMatch
  7. from django.utils.http import url_has_allowed_host_and_scheme
  8. from django.utils.translation import gettext_lazy as _
  9. from netbox.plugins import PluginConfig
  10. from netbox.registry import registry
  11. from utilities.relations import get_related_models
  12. from .permissions import resolve_permission
  13. __all__ = (
  14. 'ConditionalLoginRequiredMixin',
  15. 'ContentTypePermissionRequiredMixin',
  16. 'GetRelatedModelsMixin',
  17. 'GetReturnURLMixin',
  18. 'ObjectPermissionRequiredMixin',
  19. 'ViewTab',
  20. 'get_viewname',
  21. 'register_model_view',
  22. )
  23. #
  24. # View Mixins
  25. #
  26. class ConditionalLoginRequiredMixin(AccessMixin):
  27. """
  28. Similar to Django's LoginRequiredMixin, but enforces authentication only if LOGIN_REQUIRED is True.
  29. """
  30. def dispatch(self, request, *args, **kwargs):
  31. if settings.LOGIN_REQUIRED and not request.user.is_authenticated:
  32. return self.handle_no_permission()
  33. return super().dispatch(request, *args, **kwargs)
  34. class ContentTypePermissionRequiredMixin(ConditionalLoginRequiredMixin):
  35. """
  36. Similar to Django's built-in PermissionRequiredMixin, but extended to check model-level permission assignments.
  37. This is related to ObjectPermissionRequiredMixin, except that it does not enforce object-level permissions,
  38. and fits within NetBox's custom permission enforcement system.
  39. additional_permissions: An optional iterable of statically declared permissions to evaluate in addition to those
  40. derived from the object type
  41. """
  42. additional_permissions = list()
  43. def get_required_permission(self):
  44. """
  45. Return the specific permission necessary to perform the requested action on an object.
  46. """
  47. raise NotImplementedError(_("{self.__class__.__name__} must implement get_required_permission()").format(
  48. class_name=self.__class__.__name__
  49. ))
  50. def has_permission(self):
  51. user = self.request.user
  52. permission_required = self.get_required_permission()
  53. # Check that the user has been granted the required permission(s).
  54. if user.has_perms((permission_required, *self.additional_permissions)):
  55. return True
  56. return False
  57. def dispatch(self, request, *args, **kwargs):
  58. if not self.has_permission():
  59. return self.handle_no_permission()
  60. return super().dispatch(request, *args, **kwargs)
  61. class ObjectPermissionRequiredMixin(ConditionalLoginRequiredMixin):
  62. """
  63. Similar to Django's built-in PermissionRequiredMixin, but extended to check for both model-level and object-level
  64. permission assignments. If the user has only object-level permissions assigned, the view's queryset is filtered
  65. to return only those objects on which the user is permitted to perform the specified action.
  66. additional_permissions: An optional iterable of statically declared permissions to evaluate in addition to those
  67. derived from the object type
  68. """
  69. additional_permissions = list()
  70. def get_required_permission(self):
  71. """
  72. Return the specific permission necessary to perform the requested action on an object.
  73. """
  74. raise NotImplementedError(_("{class_name} must implement get_required_permission()").format(
  75. class_name=self.__class__.__name__
  76. ))
  77. def has_permission(self):
  78. user = self.request.user
  79. permission_required = self.get_required_permission()
  80. # Check that the user has been granted the required permission(s).
  81. if user.has_perms((permission_required, *self.additional_permissions)):
  82. # Update the view's QuerySet to filter only the permitted objects
  83. action = resolve_permission(permission_required)[1]
  84. self.queryset = self.queryset.restrict(user, action)
  85. return True
  86. return False
  87. def dispatch(self, request, *args, **kwargs):
  88. if not hasattr(self, 'queryset'):
  89. raise ImproperlyConfigured(
  90. _(
  91. '{class_name} has no queryset defined. ObjectPermissionRequiredMixin may only be used on views '
  92. 'which define a base queryset'
  93. ).format(class_name=self.__class__.__name__)
  94. )
  95. if not self.has_permission():
  96. return self.handle_no_permission()
  97. return super().dispatch(request, *args, **kwargs)
  98. class GetReturnURLMixin:
  99. """
  100. Provides logic for determining where a user should be redirected after processing a form.
  101. """
  102. default_return_url = None
  103. def get_return_url(self, request, obj=None):
  104. # First, see if `return_url` was specified as a query parameter or form data. Use this URL only if it's
  105. # considered safe.
  106. return_url = request.GET.get('return_url') or request.POST.get('return_url')
  107. if return_url and url_has_allowed_host_and_scheme(return_url, allowed_hosts=None):
  108. return return_url
  109. # Next, check if the object being modified (if any) has an absolute URL.
  110. if obj is not None and obj.pk and hasattr(obj, 'get_absolute_url'):
  111. return obj.get_absolute_url()
  112. # Fall back to the default URL (if specified) for the view.
  113. if self.default_return_url is not None:
  114. return reverse(self.default_return_url)
  115. # Attempt to dynamically resolve the list view for the object
  116. if hasattr(self, 'queryset'):
  117. model_opts = self.queryset.model._meta
  118. try:
  119. return reverse(f'{model_opts.app_label}:{model_opts.model_name}_list')
  120. except NoReverseMatch:
  121. pass
  122. # If all else fails, return home. Ideally this should never happen.
  123. return reverse('home')
  124. class GetRelatedModelsMixin:
  125. """
  126. Provides logic for collecting all related models for the currently viewed model.
  127. """
  128. def get_related_models(self, request, instance, omit=[], extra=[]):
  129. """
  130. Get related models of the view's `queryset` model without those listed in `omit`. Will be sorted alphabetical.
  131. Args:
  132. request: Current request being processed.
  133. instance: The instance related models should be looked up for. A list of instances can be passed to match
  134. related objects in this list (e.g. to find sites of a region including child regions).
  135. omit: Remove relationships to these models from the result. Needs to be passed, if related models don't
  136. provide a `_list` view.
  137. extra: Add extra models to the list of automatically determined related models. Can be used to add indirect
  138. relationships.
  139. """
  140. model = self.queryset.model
  141. related = filter(
  142. lambda m: m[0] is not model and m[0] not in omit,
  143. get_related_models(model, False)
  144. )
  145. related_models = [
  146. (
  147. model.objects.restrict(request.user, 'view').filter(**(
  148. {f'{field}__in': instance}
  149. if isinstance(instance, Iterable)
  150. else {field: instance}
  151. )),
  152. f'{field}_id'
  153. )
  154. for model, field in related
  155. ]
  156. related_models.extend(extra)
  157. return sorted(related_models, key=lambda x: x[0].model._meta.verbose_name.lower())
  158. class ViewTab:
  159. """
  160. ViewTabs are used for navigation among multiple object-specific views, such as the changelog or journal for
  161. a particular object.
  162. Args:
  163. label: Human-friendly text
  164. visible: A callable which determines whether the tab should be displayed. This callable must accept exactly one
  165. argument: the object instance. If a callable is not specified, the tab's visibility will be determined by
  166. its badge (if any) and the value of `hide_if_empty`.
  167. badge: A static value or callable to display alongside the label (optional). If a callable is used, it must
  168. accept a single argument representing the object being viewed.
  169. weight: Numeric weight to influence ordering among other tabs (default: 1000)
  170. permission: The permission required to display the tab (optional).
  171. hide_if_empty: If true, the tab will be displayed only if its badge has a meaningful value. (This parameter is
  172. evaluated only if the tab is permitted to be displayed according to the `visible` parameter.)
  173. """
  174. def __init__(self, label, visible=None, badge=None, weight=1000, permission=None, hide_if_empty=False):
  175. self.label = label
  176. self.visible = visible
  177. self.badge = badge
  178. self.weight = weight
  179. self.permission = permission
  180. self.hide_if_empty = hide_if_empty
  181. def render(self, instance):
  182. """
  183. Return the attributes needed to render a tab in HTML if the tab should be displayed. Otherwise, return None.
  184. """
  185. if self.visible is not None and not self.visible(instance):
  186. return None
  187. badge_value = self._get_badge_value(instance)
  188. if self.badge and self.hide_if_empty and not badge_value:
  189. return None
  190. return {
  191. 'label': self.label,
  192. 'badge': badge_value,
  193. 'weight': self.weight,
  194. }
  195. def _get_badge_value(self, instance):
  196. if not self.badge:
  197. return None
  198. if callable(self.badge):
  199. return self.badge(instance)
  200. return self.badge
  201. #
  202. # Utility functions
  203. #
  204. def get_viewname(model, action=None, rest_api=False):
  205. """
  206. Return the view name for the given model and action, if valid.
  207. :param model: The model or instance to which the view applies
  208. :param action: A string indicating the desired action (if any); e.g. "add" or "list"
  209. :param rest_api: A boolean indicating whether this is a REST API view
  210. """
  211. is_plugin = isinstance(model._meta.app_config, PluginConfig)
  212. app_label = model._meta.app_label
  213. model_name = model._meta.model_name
  214. if rest_api:
  215. viewname = f'{app_label}-api:{model_name}'
  216. if is_plugin:
  217. viewname = f'plugins-api:{viewname}'
  218. if action:
  219. viewname = f'{viewname}-{action}'
  220. else:
  221. viewname = f'{app_label}:{model_name}'
  222. if is_plugin:
  223. viewname = f'plugins:{viewname}'
  224. if action:
  225. viewname = f'{viewname}_{action}'
  226. return viewname
  227. def register_model_view(model, name='', path=None, detail=True, kwargs=None):
  228. """
  229. This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject
  230. additional tabs within a model's detail view. For example, to add a custom tab to NetBox's dcim.Site model:
  231. @register_model_view(Site, 'myview', path='my-custom-view')
  232. class MyView(ObjectView):
  233. ...
  234. This will automatically create a URL path for MyView at `/dcim/sites/<id>/my-custom-view/` which can be
  235. resolved using the view name `dcim:site_myview'.
  236. Args:
  237. model: The Django model class with which this view will be associated.
  238. name: The string used to form the view's name for URL resolution (e.g. via `reverse()`). This will be appended
  239. to the name of the base view for the model using an underscore. If blank, the model name will be used.
  240. path: The URL path by which the view can be reached (optional). If not provided, `name` will be used.
  241. detail: True if the path applied to an individual object; False if it attaches to the base (list) path.
  242. kwargs: A dictionary of keyword arguments for the view to include when registering its URL path (optional).
  243. """
  244. def _wrapper(cls):
  245. app_label = model._meta.app_label
  246. model_name = model._meta.model_name
  247. if model_name not in registry['views'][app_label]:
  248. registry['views'][app_label][model_name] = []
  249. registry['views'][app_label][model_name].append({
  250. 'name': name,
  251. 'view': cls,
  252. 'path': path if path is not None else name,
  253. 'detail': detail,
  254. 'kwargs': kwargs or {},
  255. })
  256. return cls
  257. return _wrapper