Skip to content
Snippets Groups Projects
Commit 05d4c546 authored by James Graham's avatar James Graham
Browse files

Add alternate template to DetailView when user does not have access

to a DataSource
parent 96d0bfb5
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,16 @@ from . import models
class DataSourceAdmin(admin.ModelAdmin):
pass
def has_change_permission(self, request, obj=None) -> bool:
"""
Does the user have permission to change this object?
"""
permission = super().has_change_permission(request, obj)
if obj is not None:
permission &= obj.owner == request.user
return permission
admin.site.register(models.DataSource, DataSourceAdmin)
......@@ -64,8 +64,10 @@ class DataSource(models.Model):
"""
if not self.access_control:
return True
if self.owner == user:
return True
return user.groups.exists(self.users_group)
return self.users_group.user_set.filter(pk=user.pk).exists()
def save(self, **kwargs):
if self.access_control:
......
{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item" aria-current="page">
<a href="{% url 'datasources:datasource.list' %}">Data Sources</a>
</li>
<li class="breadcrumb-item active" aria-current="page">
{{ datasource.name }}
</li>
</ol>
</nav>
<h2>View Data Source - {{ datasource.name }}</h2>
<div class="alert alert-warning">
You do not have permission to access this resource.
<a href="#"
class="btn btn-primary" role="button">Request Access</a>
</div>
{% if datasource.description %}
<p>{{ datasource.description }}</p>
{% endif %}
{% endblock %}
\ No newline at end of file
......@@ -28,6 +28,11 @@ class DataSourceDetailView(DetailView):
template_name = 'datasources/datasource/detail.html'
context_object_name = 'datasource'
def get_template_names(self):
if not self.object.has_permission(self.request.user):
return ['datasources/datasource/detail-no-access.html']
return super().get_template_names()
class DataSourceUpdateView(OwnerPermissionRequiredMixin, UpdateView):
model = models.DataSource
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment