|
1
|
"""Custom view decorators for fossilrepo.""" |
|
2
|
|
|
3
|
from functools import wraps |
|
4
|
|
|
5
|
from django.contrib.auth.decorators import login_required |
|
6
|
|
|
7
|
|
|
8
|
def public_or_login(view_func): |
|
9
|
"""Allow anonymous access to public project views. |
|
10
|
|
|
11
|
For views that take a `slug` parameter: if the project is public, |
|
12
|
let anonymous users through. Otherwise, redirect to login. |
|
13
|
|
|
14
|
This replaces @login_required on read-only project/fossil views. |
|
15
|
The view itself must still call require_project_read() for the |
|
16
|
actual permission check. |
|
17
|
""" |
|
18
|
|
|
19
|
@wraps(view_func) |
|
20
|
def wrapper(request, *args, **kwargs): |
|
21
|
if request.user.is_authenticated: |
|
22
|
return view_func(request, *args, **kwargs) |
|
23
|
|
|
24
|
# Check if this is a public project |
|
25
|
slug = kwargs.get("slug") or (args[0] if args else None) |
|
26
|
if slug: |
|
27
|
from projects.models import Project |
|
28
|
|
|
29
|
try: |
|
30
|
project = Project.objects.get(slug=slug, deleted_at__isnull=True) |
|
31
|
if project.visibility == "public": |
|
32
|
return view_func(request, *args, **kwargs) |
|
33
|
except Project.DoesNotExist: |
|
34
|
pass |
|
35
|
|
|
36
|
# Not public or no slug -- require login |
|
37
|
return login_required(view_func)(request, *args, **kwargs) |
|
38
|
|
|
39
|
return wrapper |
|
40
|
|