From c9ca21fdd91461efe7fd52e7e6711f3217361582 Mon Sep 17 00:00:00 2001 From: chirac Date: Mon, 4 Jul 2016 20:04:11 +0200 Subject: [PATCH] Ajoute une table whitelist --- users/admin.py | 6 ++- users/migrations/0015_whitelist.py | 25 ++++++++++ users/models.py | 24 +++++++++ users/templates/users/aff_whitelists.html | 21 ++++++++ users/templates/users/profil.html | 30 +++++++++--- users/urls.py | 2 + users/views.py | 60 ++++++++++++++++++++--- 7 files changed, 153 insertions(+), 15 deletions(-) create mode 100644 users/migrations/0015_whitelist.py create mode 100644 users/templates/users/aff_whitelists.html diff --git a/users/admin.py b/users/admin.py index 70307df0..ceb37a66 100644 --- a/users/admin.py +++ b/users/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import User, School, Right, ListRight, Ban +from .models import User, School, Right, ListRight, Ban, Whitelist class UserAdmin(admin.ModelAdmin): list_display = ('name','surname','pseudo','room','email', 'school', 'state') @@ -17,8 +17,12 @@ class RightAdmin(admin.ModelAdmin): class BanAdmin(admin.ModelAdmin): list_display = ('user', 'raison', 'date_start', 'date_end') +class WhitelistAdmin(admin.ModelAdmin): + list_display = ('user', 'raison', 'date_start', 'date_end') + admin.site.register(User, UserAdmin) admin.site.register(School, SchoolAdmin) admin.site.register(Right, RightAdmin) admin.site.register(ListRight, ListRightAdmin) admin.site.register(Ban, BanAdmin) +admin.site.register(Whitelist, WhitelistAdmin) diff --git a/users/migrations/0015_whitelist.py b/users/migrations/0015_whitelist.py new file mode 100644 index 00000000..88c11624 --- /dev/null +++ b/users/migrations/0015_whitelist.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0014_auto_20160704_1548'), + ] + + operations = [ + migrations.CreateModel( + name='Whitelist', + fields=[ + ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)), + ('raison', models.CharField(max_length=255)), + ('date_start', models.DateTimeField(auto_now_add=True)), + ('date_end', models.DateTimeField(help_text='%m/%d/%y %H:%M:%S')), + ('user', models.ForeignKey(to='users.User', on_delete=django.db.models.deletion.PROTECT)), + ], + ), + ] diff --git a/users/models.py b/users/models.py index e65c3966..68807056 100644 --- a/users/models.py +++ b/users/models.py @@ -71,6 +71,15 @@ class Ban(models.Model): def __str__(self): return str(self.user) + ' ' + str(self.raison) +class Whitelist(models.Model): + user = models.ForeignKey('User', on_delete=models.PROTECT) + raison = models.CharField(max_length=255) + date_start = models.DateTimeField(auto_now_add=True) + date_end = models.DateTimeField(help_text='%m/%d/%y %H:%M:%S') + + def __str__(self): + return str(self.user) + ' ' + str(self.raison) + class UserForm(ModelForm): def __init__(self, *args, **kwargs): super(InfoForm, self).__init__(*args, **kwargs) @@ -151,5 +160,20 @@ class BanForm(ModelForm): raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps") return date_end +class WhitelistForm(ModelForm): + def __init__(self, *args, **kwargs): + super(WhitelistForm, self).__init__(*args, **kwargs) + self.fields['date_end'].label = 'Date de fin' + + class Meta: + model = Whitelist + exclude = ['user'] + + def clean_date_end(self): + date_end = self.cleaned_data['date_end'] + if date_end < timezone.now(): + raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps") + return date_end + class ProfilForm(Form): user =forms.CharField(label ='Ok', max_length=100) diff --git a/users/templates/users/aff_whitelists.html b/users/templates/users/aff_whitelists.html new file mode 100644 index 00000000..a23a78ce --- /dev/null +++ b/users/templates/users/aff_whitelists.html @@ -0,0 +1,21 @@ + + + + + + + + + + + {% for whitelist in white_list %} + + + + + + + + {% endfor %} +
UtilisateurRaisonDate de débutDate de fin
{{ whitelist.user }}{{ whitelist.raison }}{{ whitelist.date_start }}{{ whitelist.date_end }} Editer
+ diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index 4b94c851..d25933c2 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -42,20 +42,19 @@ {% else %} Non adhérent {% endif %} + Accès gracieux + {% if end_whitelist != None %} + {{ end_whitelist }} + {% else %} + Aucun + {% endif %} + Bannissement {% if end_ban != None %} {{ end_ban }} {% else %} Non banni {% endif %} - - - Connexion - {% if actif == True %} - Active - {% else %} - Désactivée - {% endif %} Statut {% if user.state == 0 %} Actif @@ -64,6 +63,14 @@ {% else %} Archivé {% endif %} + + + Connexion + {% if actif == True %} + Active + {% else %} + Désactivée + {% endif %}

Machines :

Ajouter une machine

@@ -86,6 +93,13 @@ {% else %}

Aucun bannissement

{% endif %} +

Accès à titre gracieux :

+

Accorder un accès à titre gracieux

+ {% if white_list %} + {% include "users/aff_whitelists.html" with white_list=white_list %} + {% else %} +

Aucun accès gracieux

+ {% endif %}


diff --git a/users/urls.py b/users/urls.py index 4006d639..87da16c6 100644 --- a/users/urls.py +++ b/users/urls.py @@ -9,6 +9,8 @@ urlpatterns = [ url(r'^password/(?P[0-9]+)$', views.password, name='password'), url(r'^add_ban/(?P[0-9]+)$', views.add_ban, name='add-ban'), url(r'^edit_ban/(?P[0-9]+)$', views.edit_ban, name='edit-ban'), + url(r'^add_whitelist/(?P[0-9]+)$', views.add_whitelist, name='add-whitelist'), + url(r'^edit_whitelist/(?P[0-9]+)$', views.edit_whitelist, name='edit-whitelist'), url(r'^add_right/$', views.add_right, name='add-right'), url(r'^del_right/$', views.del_right, name='del-right'), url(r'^profil/$', views.profil, name='profil'), diff --git a/users/views.py b/users/views.py index 2e1f1f59..3140e088 100644 --- a/users/views.py +++ b/users/views.py @@ -9,7 +9,7 @@ from django.contrib import messages from django.db.models import Max from django.utils import timezone -from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm +from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm from cotisations.models import Facture from machines.models import Machine, Interface from users.forms import PassForm @@ -34,6 +34,11 @@ def end_ban(user): date_max = Ban.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max'] return date_max +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 + def is_ban(user): """ Renvoie si un user est banni ou non """ end = end_ban(user) @@ -44,9 +49,19 @@ def is_ban(user): else: return True +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 + def has_access(user): """ Renvoie si un utilisateur a accès à internet""" - if user.state == User.STATE_ACTIVE and not is_ban(user) and is_adherent(user): + if user.state == User.STATE_ACTIVE and not is_ban(user) and ( is_adherent(user) or is_whitelisted(user)): return True else: return False @@ -149,7 +164,7 @@ def add_ban(request, userid): def edit_ban(request, banid): try: ban_instance = Ban.objects.get(pk=banid) - except User.DoesNotExist: + except Ban.DoesNotExist: messages.error(request, u"Entrée inexistante" ) return redirect("/users/") ban = BanForm(request.POST or None, instance=ban_instance) @@ -159,6 +174,35 @@ def edit_ban(request, banid): return redirect("/users/") return form({'userform': ban}, 'users/user.html', request) +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) + def index(request): users_list = User.objects.order_by('pk') connexion = [] @@ -175,10 +219,14 @@ def profil(request): 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) - end = None + whitelists = Whitelist.objects.filter(user__pseudo = users) + end_bans = None + end_whitelists = None if(is_ban(users)): - end=end_ban(users) - return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'end_ban':end, 'end_adhesion':end_adhesion(users), 'actif':has_access(users)}) + end_bans=end_ban(users) + if(is_whitelisted(users)): + end_whitelists=end_whitelist(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)}) return redirect("/users/") return redirect("/users/")