2016-06-30 18:18:06 +00:00
|
|
|
# App de gestion des users pour re2o
|
|
|
|
# Goulven Kermarec, Gabriel Détraz
|
|
|
|
# Gplv2
|
2016-07-01 20:47:08 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2016-06-30 18:18:06 +00:00
|
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
|
|
from django.core.context_processors import csrf
|
|
|
|
from django.template import Context, RequestContext, loader
|
2016-07-01 20:47:08 +00:00
|
|
|
from django.contrib import messages
|
2016-07-03 00:15:17 +00:00
|
|
|
from django.db.models import Max
|
2016-07-04 20:37:04 +00:00
|
|
|
from django.db import IntegrityError
|
2016-07-03 00:15:17 +00:00
|
|
|
from django.utils import timezone
|
2016-06-30 01:39:07 +00:00
|
|
|
|
2016-07-04 18:04:11 +00:00
|
|
|
from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm
|
2016-07-03 16:09:58 +00:00
|
|
|
from cotisations.models import Facture
|
2016-07-04 01:01:09 +00:00
|
|
|
from machines.models import Machine, Interface
|
2016-07-01 20:47:08 +00:00
|
|
|
from users.forms import PassForm
|
2016-07-03 16:09:58 +00:00
|
|
|
from search.models import SearchForm
|
2016-07-03 17:48:46 +00:00
|
|
|
from cotisations.views import is_adherent, end_adhesion
|
2016-07-04 15:13:26 +00:00
|
|
|
from machines.views import unassign_ips, assign_ips
|
2016-06-30 18:18:06 +00:00
|
|
|
|
2016-07-01 22:35:44 +00:00
|
|
|
from re2o.login import makeSecret, hashNT
|
|
|
|
|
2016-07-04 09:31:20 +00:00
|
|
|
def archive(user):
|
|
|
|
""" Archive un utilisateur """
|
|
|
|
unassign_ips(user)
|
|
|
|
return
|
|
|
|
|
2016-07-04 15:13:26 +00:00
|
|
|
def unarchive(user):
|
|
|
|
""" Triger actions au desarchivage d'un user """
|
|
|
|
assign_ips(user)
|
|
|
|
return
|
|
|
|
|
2016-07-02 23:37:58 +00:00
|
|
|
def end_ban(user):
|
|
|
|
""" Renvoie la date de fin de ban d'un user, False sinon """
|
|
|
|
date_max = Ban.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
|
|
|
|
return date_max
|
|
|
|
|
2016-07-04 18:04:11 +00:00
|
|
|
def end_whitelist(user):
|
|
|
|
""" Renvoie la date de fin de ban d'un user, False sinon """
|
|
|
|
date_max = Whitelist.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
|
|
|
|
return date_max
|
|
|
|
|
2016-07-03 00:15:17 +00:00
|
|
|
def is_ban(user):
|
|
|
|
""" Renvoie si un user est banni ou non """
|
|
|
|
end = end_ban(user)
|
|
|
|
if not end:
|
|
|
|
return False
|
|
|
|
elif end < timezone.now():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2016-07-04 18:04:11 +00:00
|
|
|
def is_whitelisted(user):
|
|
|
|
""" Renvoie si un user est whitelisté ou non """
|
|
|
|
end = end_whitelist(user)
|
|
|
|
if not end:
|
|
|
|
return False
|
|
|
|
elif end < timezone.now():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2016-07-03 00:15:17 +00:00
|
|
|
def has_access(user):
|
|
|
|
""" Renvoie si un utilisateur a accès à internet"""
|
2016-07-05 10:36:08 +00:00
|
|
|
return user.state == User.STATE_ACTIVE and not is_ban(user) and ( is_adherent(user) or is_whitelisted(user))
|
2016-07-03 00:15:17 +00:00
|
|
|
|
2016-07-05 10:39:31 +00:00
|
|
|
def is_active(interface):
|
|
|
|
""" Renvoie si une interface doit avoir accès ou non """
|
|
|
|
machine = interface.machine
|
|
|
|
user = machine.user
|
|
|
|
return machine.active and has_access(user)
|
|
|
|
|
2016-06-30 18:18:06 +00:00
|
|
|
def form(ctx, template, request):
|
|
|
|
c = ctx
|
|
|
|
c.update(csrf(request))
|
|
|
|
return render_to_response(template, c, context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
def new_user(request):
|
2016-07-02 00:42:04 +00:00
|
|
|
user = InfoForm(request.POST or None)
|
|
|
|
if user.is_valid():
|
|
|
|
user.save()
|
|
|
|
messages.success(request, "L'utilisateur a été crée")
|
|
|
|
return redirect("/users/")
|
|
|
|
return form({'userform': user}, 'users/user.html', request)
|
2016-07-01 16:22:52 +00:00
|
|
|
|
|
|
|
def edit_info(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
2016-07-01 20:47:08 +00:00
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
2016-07-01 16:22:52 +00:00
|
|
|
user = InfoForm(request.POST or None, instance=user)
|
|
|
|
if user.is_valid():
|
|
|
|
user.save()
|
2016-07-02 00:42:04 +00:00
|
|
|
messages.success(request, "L'user a bien été modifié")
|
|
|
|
return redirect("/users/")
|
2016-07-01 16:22:52 +00:00
|
|
|
return form({'userform': user}, 'users/user.html', request)
|
2016-07-01 20:47:08 +00:00
|
|
|
|
|
|
|
def state(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
2016-07-04 09:31:20 +00:00
|
|
|
state = StateForm(request.POST or None, instance=user)
|
|
|
|
if state.is_valid():
|
|
|
|
if state.has_changed():
|
|
|
|
if state.cleaned_data['state'] == User.STATE_ARCHIVED:
|
|
|
|
archive(user)
|
2016-07-04 15:13:26 +00:00
|
|
|
else:
|
|
|
|
unarchive(user)
|
2016-07-04 09:31:20 +00:00
|
|
|
state.save()
|
2016-07-02 00:42:04 +00:00
|
|
|
messages.success(request, "Etat changé avec succès")
|
|
|
|
return redirect("/users/")
|
2016-07-04 09:31:20 +00:00
|
|
|
return form({'userform': state}, 'users/user.html', request)
|
2016-07-01 20:47:08 +00:00
|
|
|
|
|
|
|
def password(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
|
|
|
user_form = PassForm(request.POST or None)
|
|
|
|
if user_form.is_valid():
|
2016-07-01 22:35:44 +00:00
|
|
|
if user_form.cleaned_data['passwd1'] != user_form.cleaned_data['passwd2']:
|
|
|
|
messages.error(request, u"Les 2 mots de passe différent" )
|
|
|
|
return form({'userform': user_form}, 'users/user.html', request)
|
|
|
|
user.pwd_ssha = makeSecret(user_form.cleaned_data['passwd1'])
|
|
|
|
user.pwd_ntlm = hashNT(user_form.cleaned_data['passwd1'])
|
2016-07-01 20:47:08 +00:00
|
|
|
user.save()
|
2016-07-02 00:42:04 +00:00
|
|
|
messages.success(request, "Le mot de passe a changé")
|
|
|
|
return redirect("/users/")
|
2016-07-01 20:47:08 +00:00
|
|
|
return form({'userform': user_form}, 'users/user.html', request)
|
2016-07-01 22:35:44 +00:00
|
|
|
|
2016-07-04 20:37:04 +00:00
|
|
|
def add_right(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
2016-07-02 00:07:17 +00:00
|
|
|
right = RightForm(request.POST or None)
|
|
|
|
if right.is_valid():
|
2016-07-04 20:37:04 +00:00
|
|
|
right = right.save(commit=False)
|
|
|
|
right.user = user
|
|
|
|
try:
|
|
|
|
right.save()
|
|
|
|
messages.success(request, "Droit ajouté")
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2016-07-02 00:42:04 +00:00
|
|
|
return redirect("/users/")
|
2016-07-02 00:07:17 +00:00
|
|
|
return form({'userform': right}, 'users/user.html', request)
|
|
|
|
|
2016-07-02 02:40:24 +00:00
|
|
|
def del_right(request):
|
|
|
|
right = DelRightForm(request.POST or None)
|
|
|
|
if right.is_valid():
|
|
|
|
right_del = right.cleaned_data['rights']
|
|
|
|
right_del.delete()
|
|
|
|
messages.success(request, "Droit retiré avec succès")
|
|
|
|
return redirect("/users/")
|
|
|
|
return form({'userform': right}, 'users/user.html', request)
|
|
|
|
|
2016-07-02 20:31:50 +00:00
|
|
|
def add_ban(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
|
|
|
ban_instance = Ban(user=user)
|
|
|
|
ban = BanForm(request.POST or None, instance=ban_instance)
|
|
|
|
if ban.is_valid():
|
|
|
|
ban.save()
|
|
|
|
messages.success(request, "Bannissement ajouté")
|
|
|
|
return redirect("/users/")
|
2016-07-03 00:15:17 +00:00
|
|
|
if is_ban(user):
|
|
|
|
messages.error(request, u"Attention, cet utilisateur a deja un bannissement actif" )
|
2016-07-02 20:31:50 +00:00
|
|
|
return form({'userform': ban}, 'users/user.html', request)
|
|
|
|
|
|
|
|
def edit_ban(request, banid):
|
|
|
|
try:
|
|
|
|
ban_instance = Ban.objects.get(pk=banid)
|
2016-07-04 18:04:11 +00:00
|
|
|
except Ban.DoesNotExist:
|
2016-07-02 20:31:50 +00:00
|
|
|
messages.error(request, u"Entrée inexistante" )
|
|
|
|
return redirect("/users/")
|
|
|
|
ban = BanForm(request.POST or None, instance=ban_instance)
|
|
|
|
if ban.is_valid():
|
|
|
|
ban.save()
|
|
|
|
messages.success(request, "Bannissement modifié")
|
|
|
|
return redirect("/users/")
|
|
|
|
return form({'userform': ban}, 'users/user.html', request)
|
|
|
|
|
2016-07-04 18:04:11 +00:00
|
|
|
def add_whitelist(request, userid):
|
|
|
|
try:
|
|
|
|
user = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
|
|
|
whitelist_instance = Whitelist(user=user)
|
|
|
|
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
|
|
|
if whitelist.is_valid():
|
|
|
|
whitelist.save()
|
|
|
|
messages.success(request, "Accès à titre gracieux accordé")
|
|
|
|
return redirect("/users/")
|
|
|
|
if is_whitelisted(user):
|
|
|
|
messages.error(request, u"Attention, cet utilisateur a deja un accès gracieux actif" )
|
|
|
|
return form({'userform': whitelist}, 'users/user.html', request)
|
|
|
|
|
|
|
|
def edit_whitelist(request, whitelistid):
|
|
|
|
try:
|
|
|
|
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
|
|
|
|
except Whitelist.DoesNotExist:
|
|
|
|
messages.error(request, u"Entrée inexistante" )
|
|
|
|
return redirect("/users/")
|
|
|
|
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
|
|
|
if whitelist.is_valid():
|
|
|
|
whitelist.save()
|
|
|
|
messages.success(request, "Whitelist modifiée")
|
|
|
|
return redirect("/users/")
|
|
|
|
return form({'userform': whitelist}, 'users/user.html', request)
|
|
|
|
|
2016-07-01 22:35:44 +00:00
|
|
|
def index(request):
|
2016-07-02 00:07:17 +00:00
|
|
|
users_list = User.objects.order_by('pk')
|
2016-07-03 19:35:50 +00:00
|
|
|
connexion = []
|
|
|
|
for user in users_list:
|
2016-07-05 18:01:37 +00:00
|
|
|
end = end_adhesion(user)
|
|
|
|
access = has_access(user)
|
|
|
|
if(end!=None):
|
|
|
|
connexion.append([user, access, end])
|
|
|
|
else:
|
|
|
|
connexion.append([user, access, "Non adhérent"])
|
2016-07-03 19:35:50 +00:00
|
|
|
return render(request, 'users/index.html', {'users_list': connexion})
|
2016-07-03 16:09:58 +00:00
|
|
|
|
2016-07-05 17:34:57 +00:00
|
|
|
def index_ban(request):
|
|
|
|
ban_list = Ban.objects.order_by('date_start')
|
|
|
|
return render(request, 'users/index_ban.html', {'ban_list':ban_list})
|
|
|
|
|
|
|
|
def index_white(request):
|
|
|
|
white_list = Whitelist.objects.order_by('date_start')
|
|
|
|
return render(request, 'users/index_whitelist.html', {'white_list':white_list})
|
|
|
|
|
|
|
|
def profil(request, userid):
|
|
|
|
try:
|
|
|
|
users = User.objects.get(pk=userid)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
messages.error(request, u"Utilisateur inexistant" )
|
|
|
|
return redirect("/users/")
|
|
|
|
machines = Interface.objects.filter(machine=Machine.objects.filter(user__pseudo = users))
|
|
|
|
factures = Facture.objects.filter(user__pseudo = users)
|
|
|
|
bans = Ban.objects.filter(user__pseudo = users)
|
|
|
|
whitelists = Whitelist.objects.filter(user__pseudo = users)
|
|
|
|
end_bans = None
|
|
|
|
end_whitelists = None
|
|
|
|
if(is_ban(users)):
|
|
|
|
end_bans=end_ban(users)
|
|
|
|
if(is_whitelisted(users)):
|
|
|
|
end_whitelists=end_whitelist(users)
|
|
|
|
list_droits = Right.objects.filter(user=users)
|
|
|
|
return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'white_list':whitelists,'end_ban':end_bans,'end_whitelist':end_whitelists, 'end_adhesion':end_adhesion(users), 'actif':has_access(users), 'list_droits': list_droits})
|
2016-07-03 16:09:58 +00:00
|
|
|
|