med/users/views.py

553 lines
22 KiB
Python
Raw Normal View History

2019-08-02 12:57:53 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.core.mail import send_mail
2019-08-02 12:57:53 +00:00
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
2019-08-02 12:57:53 +00:00
from django.db import IntegrityError
from django.db import transaction
2019-08-02 12:57:53 +00:00
from django.db.models import ProtectedError
from django.shortcuts import get_object_or_404, render, redirect
from django.template import loader
from django.template.context_processors import csrf
from django.utils import timezone
from reversion import revisions as reversion
2019-08-02 12:57:53 +00:00
from reversion.models import Version
2017-06-30 01:25:07 +00:00
from med.settings import REQ_EXPIRE_STR, EMAIL_FROM, ASSO_NAME, ASSO_EMAIL, SITE_NAME, PAGINATION_NUMBER
2019-08-02 12:57:53 +00:00
from media.models import Emprunt
from users.decorators import user_is_in_campus
from users.forms import DelListRightForm, NewListRightForm, ListRightForm, RightForm, DelRightForm
from users.forms import InfoForm, BaseInfoForm, StateForm, ClefForm, AdhesionForm
from users.forms import PassForm, ResetPasswordForm
from users.models import User, Request, ListRight, Right, Clef, Adhesion
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
return render(request, template, c)
2019-08-02 12:57:53 +00:00
def password_change_action(u_form, user, request, req=False):
""" Fonction qui effectue le changeemnt de mdp bdd"""
if u_form.cleaned_data['passwd1'] != u_form.cleaned_data['passwd2']:
messages.error(request, "Les 2 mots de passe différent")
return form({'userform': u_form}, 'users/user.html', request)
user.set_password(u_form.cleaned_data['passwd1'])
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_comment("Réinitialisation du mot de passe")
messages.success(request, "Le mot de passe a changé")
if req:
req.delete()
return redirect("/")
return redirect("/users/profil/" + str(user.id))
2019-08-02 12:57:53 +00:00
def reset_passwd_mail(req, request):
""" Prend en argument un request, envoie un mail de réinitialisation de mot de pass """
t = loader.get_template('users/email_passwd_request')
c = {
2019-08-02 12:57:53 +00:00
'name': str(req.user.name) + ' ' + str(req.user.surname),
'asso': ASSO_NAME,
'asso_mail': ASSO_EMAIL,
'site_name': SITE_NAME,
'url': request.build_absolute_uri(
reverse('users:process', kwargs={'token': req.token})),
'expire_in': REQ_EXPIRE_STR,
}
2017-06-23 18:10:14 +00:00
send_mail('Votre compte %s' % SITE_NAME, t.render(c),
2019-08-02 12:57:53 +00:00
EMAIL_FROM, [req.user.email], fail_silently=False)
return
2017-06-30 01:25:07 +00:00
@login_required
2017-07-03 23:12:51 +00:00
@permission_required('bureau')
def new_user(request):
""" Vue de création d'un nouvel utilisateur, envoie un mail pour le mot de passe"""
user = BaseInfoForm(request.POST or None)
if user.is_valid():
user = user.save(commit=False)
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_comment("Création")
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
2019-08-02 12:57:53 +00:00
messages.success(request,
"L'utilisateur %s a été crée, un mail pour l'initialisation du mot de passe a été envoyé" % user.pseudo)
return redirect("/users/profil/" + str(user.id))
return form({'userform': user}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
def edit_info(request, userid):
""" Edite un utilisateur à partir de son id, si l'id est différent de request.user, vérifie la possession du droit admin """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2017-07-03 23:12:51 +00:00
if not request.user.has_perms(('bureau',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit admin")
return redirect("/users/profil/" + str(request.user.id))
2017-07-03 23:12:51 +00:00
if not request.user.has_perms(('bureau',)):
user = BaseInfoForm(request.POST or None, instance=user)
else:
user = InfoForm(request.POST or None, instance=user)
if user.is_valid():
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in user.changed_data))
messages.success(request, "L'user a bien été modifié")
return redirect("/users/profil/" + userid)
return form({'userform': user}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
2017-06-30 01:25:07 +00:00
@permission_required('bureau')
def state(request, userid):
""" Changer l'etat actif/desactivé/archivé d'un user, need droit bureau """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
state = StateForm(request.POST or None, instance=user)
if state.is_valid():
with transaction.atomic(), reversion.create_revision():
state.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in state.changed_data))
messages.success(request, "Etat changé avec succès")
return redirect("/users/profil/" + userid)
return form({'userform': state}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
def password(request, userid):
""" Reinitialisation d'un mot de passe à partir de l'userid,
pour self par défaut, pour tous sans droit si droit admin,
pour tous si droit bureau """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2017-07-03 23:12:51 +00:00
if not request.user.has_perms(('bureau',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit admin")
return redirect("/users/profil/" + str(request.user.id))
u_form = PassForm(request.POST or None)
if u_form.is_valid():
return password_change_action(u_form, user, request)
return form({'userform': u_form}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
2017-06-30 01:25:07 +00:00
@permission_required('bureau')
def add_listright(request):
""" Ajouter un droit/groupe, nécessite droit bureau.
Obligation de fournir un gid pour la synchro ldap, unique """
listright = NewListRightForm(request.POST or None)
if listright.is_valid():
with transaction.atomic(), reversion.create_revision():
listright.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Le droit/groupe a été ajouté")
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('bureau')
def edit_listright(request, listrightid):
""" Editer un groupe/droit, necessite droit bureau, à partir du listright id """
try:
listright_instance = ListRight.objects.get(pk=listrightid)
except ListRight.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
2017-06-30 01:25:07 +00:00
return redirect("/users/")
listright = ListRightForm(request.POST or None, instance=listright_instance)
if listright.is_valid():
with transaction.atomic(), reversion.create_revision():
listright.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in listright.changed_data))
messages.success(request, "Droit modifié")
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('bureau')
def del_listright(request):
""" Supprimer un ou plusieurs groupe, possible si il est vide, need droit bureau """
listright = DelListRightForm(request.POST or None)
if listright.is_valid():
listright_dels = listright.cleaned_data['listrights']
for listright_del in listright_dels:
try:
with transaction.atomic(), reversion.create_revision():
listright_del.delete()
reversion.set_comment("Destruction")
messages.success(request, "Le droit/groupe a été supprimé")
except ProtectedError:
messages.error(
request,
"L'établissement %s est affecté à au moins un user, \
vous ne pouvez pas le supprimer" % listright_del)
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('bureau')
def add_right(request, userid):
""" Ajout d'un droit à un user, need droit bureau """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
right = RightForm(request.POST or None)
if right.is_valid():
right = right.save(commit=False)
right.user = user
try:
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
reversion.set_comment("Ajout du droit %s" % right.right)
right.save()
messages.success(request, "Droit ajouté")
except IntegrityError:
pass
return redirect("/users/profil/" + userid)
return form({'userform': right}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('bureau')
def del_right(request):
""" Supprimer un droit à un user, need droit bureau """
user_right_list = dict()
for right in ListRight.objects.all():
2019-08-02 12:57:53 +00:00
user_right_list[right] = DelRightForm(right, request.POST or None)
for keys, right_item in user_right_list.items():
if right_item.is_valid():
right_del = right_item.cleaned_data['rights']
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
2019-08-02 12:57:53 +00:00
reversion.set_comment(
"Retrait des droit %s" % ','.join(str(deleted_right) for deleted_right in right_del))
right_del.delete()
messages.success(request, "Droit retiré avec succès")
return redirect("/users/")
return form({'userform': user_right_list}, 'users/del_right.html', request)
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('perm')
def index_listright(request):
""" Affiche l'ensemble des droits , need droit perm """
listright_list = ListRight.objects.order_by('listright')
2019-08-02 12:57:53 +00:00
return render(request, 'users/index_listright.html', {'listright_list': listright_list})
2017-06-30 01:25:07 +00:00
2017-07-03 18:06:21 +00:00
@login_required
2017-07-03 23:12:51 +00:00
@permission_required('bureau')
2017-07-03 18:06:21 +00:00
def add_clef(request):
clef = ClefForm(request.POST or None)
if clef.is_valid():
with transaction.atomic(), reversion.create_revision():
clef.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "La clef a été ajouté")
return redirect("/users/index_clef/")
return form({'userform': clef}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@user_is_in_campus
2017-07-03 18:06:21 +00:00
def edit_clef(request, clefid):
try:
clef_instance = Clef.objects.get(pk=clefid)
except Clef.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
2017-07-03 18:06:21 +00:00
return redirect("/users/index_clef/")
2017-09-06 20:40:24 +00:00
clef = ClefForm(request.POST or None, instance=clef_instance)
2017-07-03 18:06:21 +00:00
if clef.is_valid():
with transaction.atomic(), reversion.create_revision():
clef.save()
if request.user.is_authenticated:
reversion.set_user(request.user)
2017-07-03 18:06:21 +00:00
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in clef.changed_data))
messages.success(request, "Clef modifié")
return redirect("/users/index_clef/")
return form({'userform': clef}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
2017-07-03 18:06:21 +00:00
@login_required
2017-07-03 23:12:51 +00:00
@permission_required('bureau')
2017-07-03 18:06:21 +00:00
def del_clef(request, clefid):
try:
clef_instance = Clef.objects.get(pk=clefid)
except Clef.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
2017-07-03 18:06:21 +00:00
return redirect("/users/index_clef/")
if request.method == "POST":
with transaction.atomic(), reversion.create_revision():
clef_instance.delete()
reversion.set_user(request.user)
messages.success(request, "La clef a été détruite")
2017-07-03 18:06:21 +00:00
return redirect("/users/index_clef")
return form({'objet': clef_instance, 'objet_name': 'clef'}, 'users/delete.html', request)
2019-08-02 12:57:53 +00:00
@user_is_in_campus
2017-07-03 18:06:21 +00:00
def index_clef(request):
clef_list = Clef.objects.all().order_by('nom')
2019-08-02 12:57:53 +00:00
return render(request, 'users/index_clef.html', {'clef_list': clef_list})
2017-07-03 18:06:21 +00:00
@login_required
@permission_required('bureau')
def add_adhesion(request):
adhesion = AdhesionForm(request.POST or None)
if adhesion.is_valid():
with transaction.atomic(), reversion.create_revision():
adhesion.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "L'adhesion a été ajouté")
return redirect("/users/index_adhesion/")
return form({'userform': adhesion}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
@permission_required('bureau')
def edit_adhesion(request, adhesionid):
try:
adhesion_instance = Adhesion.objects.get(pk=adhesionid)
except Adhesion.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
return redirect("/users/index_adhesion/")
adhesion = AdhesionForm(request.POST or None, instance=adhesion_instance)
if adhesion.is_valid():
with transaction.atomic(), reversion.create_revision():
adhesion.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in adhesion.changed_data))
messages.success(request, "Adhesion modifiée")
return redirect("/users/index_adhesion/")
return form({'userform': adhesion}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
@login_required
@permission_required('bureau')
def del_adhesion(request, adhesionid):
try:
adhesion_instance = Adhesion.objects.get(pk=adhesionid)
except Adhesion.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, u"Entrée inexistante")
return redirect("/users/index_adhesion/")
if request.method == "POST":
with transaction.atomic(), reversion.create_revision():
adhesion_instance.delete()
reversion.set_user(request.user)
messages.success(request, "La adhesion a été détruit")
return redirect("/users/index_adhesion")
return form({'objet': adhesion_instance, 'objet_name': 'adhesion'}, 'users/delete.html', request)
2019-08-02 12:57:53 +00:00
@login_required
def index_adhesion(request):
adhesion_list = Adhesion.objects.all()
2019-08-02 12:57:53 +00:00
return render(request, 'users/index_adhesion.html', {'adhesion_list': adhesion_list})
2017-06-30 01:25:07 +00:00
@login_required
@permission_required('perm')
def index(request):
""" Affiche l'ensemble des users, need droit admin """
users_list = User.objects.order_by('state', 'name')
paginator = Paginator(users_list, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
users_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
users_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
users_list = paginator.page(paginator.num_pages)
return render(request, 'users/index.html', {'users_list': users_list})
2019-08-02 12:57:53 +00:00
@login_required
@permission_required('perm')
def index_ajour(request):
""" Affiche l'ensemble des users, need droit admin """
users_list = Adhesion.objects.all().order_by('annee_debut').reverse().first().adherent.all().order_by('name')
paginator = Paginator(users_list, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
users_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
users_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
users_list = paginator.page(paginator.num_pages)
return render(request, 'users/index.html', {'users_list': users_list})
2019-08-02 12:57:53 +00:00
@user_is_in_campus
def history(request, object, id):
""" Affichage de l'historique : (acl, argument)
user : self, userid"""
if object == 'clef':
try:
2019-08-02 12:57:53 +00:00
object_instance = Clef.objects.get(pk=id)
except Clef.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
elif not request.user.is_authenticated:
messages.error(request, "Permission denied")
return redirect("/users/")
if object == 'user':
try:
2019-08-02 12:57:53 +00:00
object_instance = User.objects.get(pk=id)
except User.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2017-06-30 01:25:07 +00:00
if not request.user.has_perms(('perm',)) and object_instance != request.user:
2019-08-02 12:57:53 +00:00
messages.error(request,
"Vous ne pouvez pas afficher l'historique d'un autre user que vous sans droit admin")
return redirect("/users/profil/" + str(request.user.id))
2017-07-03 18:06:21 +00:00
elif object == 'clef':
try:
2019-08-02 12:57:53 +00:00
object_instance = Clef.objects.get(pk=id)
2017-07-03 18:06:21 +00:00
except Clef.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
elif object == 'adhesion':
try:
2019-08-02 12:57:53 +00:00
object_instance = Adhesion.objects.get(pk=id)
except Adhesion.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2017-07-03 23:12:51 +00:00
elif object == 'listright':
try:
2019-08-02 12:57:53 +00:00
object_instance = ListRight.objects.get(pk=id)
2017-06-30 01:25:07 +00:00
except ListRight.DoesNotExist:
2019-08-02 12:57:53 +00:00
messages.error(request, "Droit inexistant")
return redirect("/users/")
else:
messages.error(request, "Objet inconnu")
return redirect("/users/")
reversions = Version.objects.get_for_object(object_instance)
paginator = Paginator(reversions, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
reversions = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
reversions = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
reversions = paginator.page(paginator.num_pages)
2017-06-30 01:25:07 +00:00
return render(request, 'med/history.html', {'reversions': reversions, 'object': object_instance})
2019-08-02 12:57:53 +00:00
@login_required
def mon_profil(request):
return redirect("/users/profil/" + str(request.user.id))
2019-08-02 12:57:53 +00:00
@login_required
def profil(request, userid):
try:
users = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2017-06-30 01:25:07 +00:00
if not request.user.has_perms(('perm',)) and users != request.user:
2017-07-03 23:12:51 +00:00
messages.error(request, "Vous ne pouvez pas afficher un autre user que vous sans droit perm")
return redirect("/users/profil/" + str(request.user.id))
2017-06-30 01:25:07 +00:00
emprunts_list = Emprunt.objects.filter(user=users)
list_droits = Right.objects.filter(user=users)
return render(
request,
'users/profil.html',
{
'user': users,
2017-06-30 01:25:07 +00:00
'emprunts_list': emprunts_list,
2019-08-02 12:57:53 +00:00
'list_droits': list_droits,
}
)
2019-08-02 12:57:53 +00:00
@login_required
@permission_required('bureau')
def adherer(request, userid):
try:
users = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
adh_annee = Adhesion.objects.all().order_by('annee_debut').reverse().first()
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
adh_annee.adherent.add(users)
adh_annee.save()
reversion.set_comment("Adhesion de %s" % users)
messages.success(request, "Adhesion effectuee")
return redirect("/users/profil/" + userid)
2019-08-02 12:57:53 +00:00
def reset_password(request):
userform = ResetPasswordForm(request.POST or None)
if userform.is_valid():
try:
2019-08-02 12:57:53 +00:00
user = User.objects.get(pseudo=userform.cleaned_data['pseudo'], email=userform.cleaned_data['email'])
except User.DoesNotExist:
messages.error(request, "Cet utilisateur n'existe pas")
2017-06-12 23:42:15 +00:00
return form({'userform': userform}, 'users/user.html', request)
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
messages.success(request, "Un mail pour l'initialisation du mot de passe a été envoyé")
2017-06-12 23:42:15 +00:00
redirect("/")
return form({'userform': userform}, 'users/user.html', request)
2019-08-02 12:57:53 +00:00
def process(request, token):
valid_reqs = Request.objects.filter(expires_at__gt=timezone.now())
req = get_object_or_404(valid_reqs, token=token)
if req.type == Request.PASSWD:
return process_passwd(request, req)
elif req.type == Request.EMAIL:
return process_email(request, req=req)
else:
messages.error(request, "Entrée incorrecte, contactez un admin")
redirect("/")
2019-08-02 12:57:53 +00:00
def process_passwd(request, req):
u_form = PassForm(request.POST or None)
user = req.user
if u_form.is_valid():
return password_change_action(u_form, user, request, req=req)
return form({'userform': u_form}, 'users/user.html', request)