Browse Source

Users switch between projects, not between accounts.

pull/214/head
Pēteris Caune 6 years ago
parent
commit
b12eb1ee75
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 23 additions and 26 deletions
  1. +12
    -12
      hc/accounts/tests/test_switch_project.py
  2. +2
    -2
      hc/accounts/urls.py
  3. +8
    -11
      hc/accounts/views.py
  4. +1
    -1
      templates/base.html

hc/accounts/tests/test_switch_team.py → hc/accounts/tests/test_switch_project.py View File


+ 2
- 2
hc/accounts/urls.py View File

@ -31,7 +31,7 @@ urlpatterns = [
path('change_email/<slug:token>/',
views.change_email, name="hc-change-email"),
path('switch_team/<slug:target_username>/',
views.switch_team, name="hc-switch-team"),
path('switch_project/<uuid:code>/',
views.switch_project, name="hc-switch-project"),
]

+ 8
- 11
hc/accounts/views.py View File

@ -16,6 +16,7 @@ from django.utils.timezone import now
from django.urls import resolve, Resolver404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.shortcuts import get_object_or_404
from hc.accounts.forms import (ChangeEmailForm, EmailPasswordForm,
InviteTeamMemberForm, RemoveTeamMemberForm,
ReportSettingsForm, SetPasswordForm,
@ -440,30 +441,26 @@ def unsubscribe_reports(request, username):
@login_required
def switch_team(request, target_username):
try:
target_team = Profile.objects.get(user__username=target_username)
target_project = target_team.get_own_project()
except Profile.DoesNotExist:
return HttpResponseForbidden()
def switch_project(request, code):
project = get_object_or_404(Project, code=code)
# The rules:
# Superuser can switch to any team.
access_ok = request.user.is_superuser
# Users can switch to their own teams.
if not access_ok and target_team == request.profile:
# Users can switch to their own projects.
if not access_ok and project.owner_id == request.user.id:
access_ok = True
# Users can switch to teams they are members of.
# Users can switch to projects they are members of.
if not access_ok:
q = request.user.memberships.filter(project=target_project)
q = project.member_set.filter(user=request.user)
access_ok = q.exists()
if not access_ok:
return HttpResponseForbidden()
request.profile.current_project = target_project
request.profile.current_project = project
request.profile.save()
return redirect("hc-checks")


+ 1
- 1
templates/base.html View File

@ -127,7 +127,7 @@
{% for project in projects %}
<li class="dropdown-header">{{ project }}</li>
<li>
<a href="{% url 'hc-switch-team' project.owner.username %}">Checks</a>
<a href="{% url 'hc-switch-project' project.code %}">Checks</a>
</li>
{% if project.owner == request.user %}
<li>


Loading…
Cancel
Save