2019-11-25 23:03:27 +01:00
|
|
|
from django.conf import settings as django_settings
|
2017-09-13 01:57:31 +02:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import authenticate, login
|
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
|
from django.contrib.auth.models import Group, User
|
2017-09-25 17:16:19 +02:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2017-09-13 01:57:31 +02:00
|
|
|
from django.db.models import Prefetch
|
2017-09-25 17:16:19 +02:00
|
|
|
from django.http import QueryDict
|
|
|
|
from django.shortcuts import redirect, render
|
2019-03-19 10:18:56 +01:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2017-09-25 17:16:19 +02:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.views.decorators.http import require_http_methods
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.views.generic import View
|
2017-09-13 01:57:31 +02:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView
|
|
|
|
|
|
|
|
from .forms import GroupForm
|
|
|
|
from .models import GenericTeamToken
|
|
|
|
|
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
class GenericLoginView(View):
|
|
|
|
"""
|
|
|
|
View to authenticate as kfet generic user.
|
|
|
|
|
|
|
|
It is a 2-step view. First, issue a token if user is a team member and send
|
|
|
|
him to the logout view (for proper disconnect) with callback url to here.
|
|
|
|
Then authenticate the token to log in as the kfet generic user.
|
|
|
|
|
|
|
|
Token is stored in COOKIES to avoid share it with the authentication
|
|
|
|
provider, which can be external. Session is unusable as it will be cleared
|
|
|
|
on logout.
|
|
|
|
"""
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
TOKEN_COOKIE_NAME = "kfettoken"
|
|
|
|
|
|
|
|
@method_decorator(require_http_methods(["GET", "POST"]))
|
2017-09-25 17:16:19 +02:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
token = request.get_signed_cookie(self.TOKEN_COOKIE_NAME, None)
|
|
|
|
if not token:
|
2018-10-06 12:35:49 +02:00
|
|
|
if not request.user.has_perm("kfet.is_team"):
|
2017-09-25 17:16:19 +02:00
|
|
|
return redirect_to_login(request.get_full_path())
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
if request.method == "POST":
|
2017-09-25 17:16:19 +02:00
|
|
|
# Step 1: set token and logout user.
|
2019-11-25 23:03:27 +01:00
|
|
|
return self.prepare_auth(request)
|
2017-09-25 17:16:19 +02:00
|
|
|
else:
|
|
|
|
# GET request should not change server/client states. Send a
|
|
|
|
# confirmation template to emit a POST request.
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"kfet/confirm_form.html",
|
|
|
|
{
|
|
|
|
"title": _("Ouvrir une session partagée"),
|
|
|
|
"text": _(
|
|
|
|
"Êtes-vous sûr·e de vouloir ouvrir une session "
|
|
|
|
"partagée ?"
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
2017-09-25 17:16:19 +02:00
|
|
|
else:
|
|
|
|
# Step 2: validate token.
|
|
|
|
return self.validate_auth(token)
|
|
|
|
|
2019-11-25 23:03:27 +01:00
|
|
|
def prepare_auth(self, request):
|
2017-09-25 17:16:19 +02:00
|
|
|
# Issue token.
|
|
|
|
token = GenericTeamToken.objects.create_token()
|
|
|
|
|
2019-11-25 23:03:27 +01:00
|
|
|
# When CAS logs the user out, the generic login has to be called back.
|
|
|
|
# The corresponding callback URL is provided as a GET parameter.
|
|
|
|
# The renaming of the CAS logout "url" parameter to "service" is being forced,
|
|
|
|
# which is why the CAS logout URL with callback is constructed ad hoc,
|
|
|
|
# without relying on Django redirection to Django CAS.
|
|
|
|
|
2019-11-27 11:51:29 +01:00
|
|
|
generic_login_url = request.build_absolute_uri() # preserves next parameter
|
2019-11-25 23:03:27 +01:00
|
|
|
generic_login_qd = QueryDict(mutable=True)
|
|
|
|
generic_login_qd["service"] = generic_login_url
|
|
|
|
|
|
|
|
cas_server_url = django_settings.CAS_SERVER_URL
|
2019-11-26 00:15:05 +01:00
|
|
|
cas_logout_url = cas_server_url + "logout"
|
2019-11-25 23:03:27 +01:00
|
|
|
cas_callback_url = cas_logout_url + "?{}".format(generic_login_qd.urlencode())
|
|
|
|
|
|
|
|
logout_url = cas_callback_url
|
2017-09-25 17:16:19 +02:00
|
|
|
|
|
|
|
resp = redirect(logout_url)
|
2018-10-06 12:35:49 +02:00
|
|
|
resp.set_signed_cookie(self.TOKEN_COOKIE_NAME, token.token, httponly=True)
|
2017-09-25 17:16:19 +02:00
|
|
|
return resp
|
|
|
|
|
|
|
|
def validate_auth(self, token):
|
|
|
|
# Authenticate with GenericBackend.
|
|
|
|
user = authenticate(request=self.request, kfet_token=token)
|
|
|
|
|
|
|
|
if user:
|
|
|
|
# Log in generic user.
|
|
|
|
login(self.request, user)
|
2018-10-06 12:35:49 +02:00
|
|
|
messages.success(
|
|
|
|
self.request, _("K-Fêt — Ouverture d'une session partagée.")
|
|
|
|
)
|
2017-09-25 17:16:19 +02:00
|
|
|
resp = redirect(self.get_next_url())
|
|
|
|
else:
|
|
|
|
# Try again.
|
|
|
|
resp = redirect(self.request.get_full_path())
|
|
|
|
|
|
|
|
# Prevents blocking due to an invalid COOKIE.
|
|
|
|
resp.delete_cookie(self.TOKEN_COOKIE_NAME)
|
|
|
|
return resp
|
2017-09-13 01:57:31 +02:00
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
def get_next_url(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return self.request.GET.get("next", reverse("kfet.kpsul"))
|
2017-09-13 01:57:31 +02:00
|
|
|
|
|
|
|
|
2017-09-25 17:16:19 +02:00
|
|
|
login_generic = GenericLoginView.as_view()
|
2017-09-13 01:57:31 +02:00
|
|
|
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
@permission_required("kfet.manage_perms")
|
2017-09-13 01:57:31 +02:00
|
|
|
def account_group(request):
|
|
|
|
user_pre = Prefetch(
|
2018-10-06 12:35:49 +02:00
|
|
|
"user_set", queryset=User.objects.select_related("profile__account_kfet")
|
2017-09-13 01:57:31 +02:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
groups = Group.objects.filter(name__icontains="K-Fêt").prefetch_related(
|
|
|
|
"permissions", user_pre
|
2017-09-13 01:57:31 +02:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
return render(request, "kfet/account_group.html", {"groups": groups})
|
2017-09-13 01:57:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccountGroupCreate(SuccessMessageMixin, CreateView):
|
|
|
|
model = Group
|
2018-10-06 12:35:49 +02:00
|
|
|
template_name = "kfet/account_group_form.html"
|
2017-09-13 01:57:31 +02:00
|
|
|
form_class = GroupForm
|
2018-10-06 12:35:49 +02:00
|
|
|
success_message = "Nouveau groupe : %(name)s"
|
|
|
|
success_url = reverse_lazy("kfet.account.group")
|
2017-09-13 01:57:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccountGroupUpdate(SuccessMessageMixin, UpdateView):
|
2018-10-06 12:35:49 +02:00
|
|
|
queryset = Group.objects.filter(name__icontains="K-Fêt")
|
|
|
|
template_name = "kfet/account_group_form.html"
|
2017-09-13 01:57:31 +02:00
|
|
|
form_class = GroupForm
|
2018-10-06 12:35:49 +02:00
|
|
|
success_message = "Groupe modifié : %(name)s"
|
|
|
|
success_url = reverse_lazy("kfet.account.group")
|