Skip to content

Commit

Permalink
Merge pull request #79 from coredump-ch/member-list-access
Browse files Browse the repository at this point in the history
Member list access
  • Loading branch information
dbrgn authored Jan 22, 2025
2 parents eee8e85 + f72187c commit 7d42f28
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
14 changes: 7 additions & 7 deletions interna/front/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin


class LoginRequiredMixin:

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
class StaffRequiredMixin(UserPassesTestMixin, LoginRequiredMixin):
"""
Require login as staff.
"""
def test_func(self):
return self.request.user.is_staff
2 changes: 1 addition & 1 deletion interna/front/templates/front/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="{% ifactivetab 'navigation' 'home' %}active{% endifactivetab %}"><a href="{% url 'front:home' %}">Home</a></li>
<li class="{% ifactivetab 'navigation' 'members' %}active{% endifactivetab %}"><a href="{% url 'front:members' %}">Mitglieder</a></li>
{% if user.is_staff %}<li class="{% ifactivetab 'navigation' 'members' %}active{% endifactivetab %}"><a href="{% url 'front:members' %}">Mitglieder</a></li>{% endif %}
<li class="{% ifactivetab 'navigation' 'inventory' %}active{% endifactivetab %}"><a href="{% url 'inventory:index' %}">Inventar</a></li>
<li class="{% ifactivetab 'navigation' 'crowdfund' %}active{% endifactivetab %}"><a href="{% url 'crowdfund:index' %}">Crowdfund</a></li>
<li class="{% ifactivetab 'navigation' 'wishlist' %}active{% endifactivetab %}"><a href="{% url 'front:wishlist' %}">Wishlist</a></li>
Expand Down
19 changes: 5 additions & 14 deletions interna/front/templates/front/members.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
{% block content %}
<h1>Mitglieder</h1>

<p>Unsere aktuellen Mitglieder. {% if user.is_staff %}Um Einträge zu ändern
oder neue Mitglieder hinzuzufügen, benutze das
<a href="{% url 'admin:index' %}">Admin Interface</a>.{% endif %}</p>

<p>Unsere aktuellen Mitglieder. Um Einträge zu ändern oder neue Mitglieder hinzuzufügen, benutze das
<a href="{% url 'admin:index' %}">Admin Interface</a>.</p>

<div class="clearfix">
<h3 class="display-inline-block">Aktivmitglieder ({{ active_memberships|length }})</h3>
{% if user.is_staff %}
<div class="staff-actions pull-right">
<a href="{% url 'front:member_emails' %}"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> E-Mail-Liste</a>
</div>
{% endif %}
<div class="staff-actions pull-right">
<a href="{% url 'front:member_emails' %}"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> E-Mail-Liste</a>
</div>
</div>

<table class="table table-striped table-hover">
Expand Down Expand Up @@ -64,9 +60,6 @@ <h3 class="display-inline-block">Aktivmitglieder ({{ active_memberships|length }
</tbody>
</table>


{% if user.is_staff %}

<h3>Ehemalige Mitglieder ({{ expired_members|length }})</h3>

<table class="table table-striped table-hover">
Expand All @@ -88,6 +81,4 @@ <h3>Ehemalige Mitglieder ({{ expired_members|length }})</h3>
</tbody>
</table>

{% endif %}

{% endblock %}
6 changes: 3 additions & 3 deletions interna/front/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.shortcuts import redirect
from django.views.generic.base import View, TemplateView

from front.mixins import LoginRequiredMixin
from memberdb import models
from .mixins import StaffRequiredMixin


class HomeView(TemplateView):
Expand All @@ -20,7 +20,7 @@ def get(self, request, *args, **kwargs):
return redirect('front:home')


class MembersView(LoginRequiredMixin, TemplateView):
class MembersView(StaffRequiredMixin, TemplateView):
"""List members."""
template_name = 'front/members.html'

Expand All @@ -35,7 +35,7 @@ def get_context_data(self, **kwargs):
return context


class MemberEmailsView(LoginRequiredMixin, TemplateView):
class MemberEmailsView(StaffRequiredMixin, TemplateView):
"""List email addresses of all active members."""
template_name = 'front/member_emails.html'

Expand Down
41 changes: 38 additions & 3 deletions interna/memberdb/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from datetime import date

from django.contrib.auth.models import User
import django.test
from pytest import mark
from model_bakery import baker
from rest_framework.test import APIRequestFactory, force_authenticate

from .. import models, views


class TestMembershipView:

class TestApiMembershipView:
@mark.django_db
def test_active_members(self):
# Test data: Create 3 members, 2 of them active
Expand Down Expand Up @@ -57,7 +57,6 @@ def test_auth_required(self):
@mark.django_db
@mark.parametrize('staff', [True, False])
def test_admin_required(self, staff: bool):
print(staff)
user = baker.make(User, is_staff=staff)

factory = APIRequestFactory()
Expand All @@ -71,3 +70,39 @@ def test_admin_required(self, staff: bool):
else:
assert response.status_code == 403
assert response.data['detail'].code == 'permission_denied'


class TestMembersView:
@mark.django_db
@mark.parametrize('staff', [True, False])
def test_admin_required(self, client: django.test.Client, staff: bool):
# Auth
user = baker.make(User, is_staff=staff)
client.force_login(user)

# Request
response = client.get('/members/')

# Verify
if staff:
assert response.status_code == 200
assert b'Aktivmitglieder' in response.content
else:
assert response.status_code == 403
assert b'Aktivmitglieder' not in response.content

@mark.django_db
@mark.parametrize('staff', [True, False])
def test_emails_admin_required(self, client: django.test.Client, staff: bool):
# Auth
user = baker.make(User, is_staff=staff)
client.force_login(user)

# Request
response = client.get('/members/emails/')

# Verify
if staff:
assert response.status_code == 200
else:
assert response.status_code == 403

0 comments on commit 7d42f28

Please sign in to comment.