diff --git a/hc/accounts/tests/test_project.py b/hc/accounts/tests/test_project.py index e7c5aac7..e8a1cc8b 100644 --- a/hc/accounts/tests/test_project.py +++ b/hc/accounts/tests/test_project.py @@ -212,3 +212,23 @@ class ProjectTestCase(BaseTestCase): r = self.client.get("/projects/%s/settings/" % p2.code) self.assertContains(r, "Add Users from Other Teams") self.assertContains(r, "bob@example.org") + + def test_it_checks_rw_access_when_updating_project_name(self): + self.bobs_membership.rw = False + self.bobs_membership.save() + + self.client.login(username="bob@example.org", password="password") + + form = {"set_project_name": "1", "name": "Alpha Team"} + r = self.client.post(self.url, form) + self.assertEqual(r.status_code, 403) + + def test_it_hides_actions_for_readonly_users(self): + self.bobs_membership.rw = False + self.bobs_membership.save() + + self.client.login(username="bob@example.org", password="password") + + r = self.client.get(self.url) + self.assertNotContains(r, "#set-project-name-modal", status_code=200) + self.assertNotContains(r, "Show API Keys") diff --git a/hc/accounts/views.py b/hc/accounts/views.py index e88d8954..0e97fce9 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -246,25 +246,27 @@ def add_project(request): @login_required def project(request, code): - if request.user.is_superuser: - q = Project.objects - else: - q = request.profile.projects() + project = get_object_or_404(Project, code=code) + is_owner = project.owner_id == request.user.id - try: - project = q.get(code=code) - except Project.DoesNotExist: - return HttpResponseNotFound() + if request.user.is_superuser or is_owner: + rw = True + else: + membership = get_object_or_404(Member, project=project, user=request.user) + rw = membership.rw - is_owner = project.owner_id == request.user.id ctx = { "page": "project", + "rw": rw, "project": project, "is_owner": is_owner, "show_api_keys": "show_api_keys" in request.GET, } if request.method == "POST": + if not rw: + return HttpResponseForbidden() + if "create_api_keys" in request.POST: project.set_api_keys() project.save() diff --git a/templates/accounts/project.html b/templates/accounts/project.html index d9ff5235..fb07559f 100644 --- a/templates/accounts/project.html +++ b/templates/accounts/project.html @@ -59,11 +59,13 @@