-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from CentreForDigitalHumanities/feature/anonymous
Login automatically to anonymous user
- Loading branch information
Showing
9 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.contrib.auth import login | ||
from django.http import HttpRequest, HttpResponseRedirect | ||
from django.urls import reverse | ||
|
||
from lidiabrowser.init import get_anonymous_user | ||
|
||
|
||
def index_view_autologin(request: HttpRequest): | ||
# If the user is not authenticated, automatically login to the | ||
# anonymous user, which should be created if necessary. | ||
if not request.user.is_authenticated: | ||
# Create anonymous user and viewer group if necessary | ||
anonymous_user = get_anonymous_user() | ||
login(request, anonymous_user) | ||
return HttpResponseRedirect(reverse("admin:index")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.contrib.auth.models import Group, Permission, User | ||
|
||
ANONYMOUSUSERNAME = "anonymous" | ||
|
||
|
||
def initiate_groups() -> dict[str, Group]: | ||
"""Create groups for viewer accounts and return a dictionary containing | ||
the group objects.""" | ||
# For now, only create a 'view all' group - no distinction yet | ||
# between all or restricted access | ||
view_all, _ = Group.objects.get_or_create(name="view_all") | ||
models = [ | ||
'publication', 'language', 'annotation', 'articleterm', | ||
'lidiaterm', 'category', 'termgroup' | ||
] | ||
permissions = [] | ||
for model in models: | ||
permissions.append(Permission.objects.get_by_natural_key( | ||
"view_" + model, "lidia", model | ||
)) | ||
view_all.permissions.add(*permissions) | ||
return { | ||
"view_all": view_all | ||
} | ||
|
||
|
||
def create_anonymous_user(view_all_group: Group) -> User: | ||
anonymous_user = User.objects.create_user( | ||
ANONYMOUSUSERNAME, | ||
is_staff=True | ||
) | ||
anonymous_user.groups.add(view_all_group) | ||
return anonymous_user | ||
|
||
|
||
def get_anonymous_user() -> User: | ||
try: | ||
anonymous_user = User.objects.get(username=ANONYMOUSUSERNAME) | ||
except User.DoesNotExist: | ||
groups = initiate_groups() | ||
anonymous_user = create_anonymous_user(groups["view_all"]) | ||
return anonymous_user | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lidiabrowser/lidiabrowser/templates/lidiabrowser/logged_out.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends "registration/logged_out.html" %} | ||
|
||
{% load i18n %} | ||
|
||
{% block content %} | ||
|
||
<p>{% translate "You are now logged out." %}</p> | ||
|
||
<p><a href="{% url 'admin:index' %}">{% translate "Log in again" %}</a></p> | ||
|
||
<p><a href="{% url 'index' %}">{% translate "Log in as an anonymous user" %}</a></p> | ||
|
||
{% endblock %} |
54 changes: 54 additions & 0 deletions
54
lidiabrowser/lidiabrowser/templates/lidiabrowser/login.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{% extends "admin/login.html" %} | ||
{% load i18n %} | ||
{% block content %} | ||
{% if form.errors and not form.non_field_errors %} | ||
<p class="errornote"> | ||
{% blocktranslate count counter=form.errors.items|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %} | ||
</p> | ||
{% endif %} | ||
|
||
{% if form.non_field_errors %} | ||
{% for error in form.non_field_errors %} | ||
<p class="errornote"> | ||
{{ error }} | ||
</p> | ||
{% endfor %} | ||
{% endif %} | ||
|
||
<div id="content-main"> | ||
|
||
{% if user.is_authenticated %} | ||
<p class="errornote"> | ||
{% blocktranslate trimmed %} | ||
You are authenticated as {{ username }}, but are not authorized to | ||
access this page. Would you like to login to a different account? | ||
{% endblocktranslate %} | ||
</p> | ||
{% endif %} | ||
|
||
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %} | ||
<div class="form-row"> | ||
{{ form.username.errors }} | ||
{{ form.username.label_tag }} {{ form.username }} | ||
</div> | ||
<div class="form-row"> | ||
{{ form.password.errors }} | ||
{{ form.password.label_tag }} {{ form.password }} | ||
<input type="hidden" name="next" value="{{ next }}"> | ||
</div> | ||
{% url 'admin_password_reset' as password_reset_url %} | ||
{% if password_reset_url %} | ||
<div class="password-reset-link"> | ||
<a href="{{ password_reset_url }}">{% translate 'Forgotten your password or username?' %}</a> | ||
</div> | ||
{% endif %} | ||
<div style="text-align: center"> | ||
<a href="{% url "index" %}">{% translate 'Log in as anonymous user' %}</a> | ||
</div> | ||
<div class="submit-row"> | ||
<input type="submit" value="{% translate 'Log in' %}"> | ||
</div> | ||
</form> | ||
|
||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters