views.py 11.7 KB
Newer Older
chirac's avatar
chirac committed
1 2 3
# App de gestion des users pour re2o
# Goulven Kermarec, Gabriel Détraz
# Gplv2
4
from django.shortcuts import render_to_response, render, redirect
chirac's avatar
chirac committed
5
from django.core.context_processors import csrf
6
from django.template import RequestContext
7
from django.contrib import messages
8
from django.contrib.auth.decorators import login_required, permission_required
chirac's avatar
chirac committed
9
from django.db.models import Max, ProtectedError
10
from django.db import IntegrityError
11
from django.utils import timezone
lhark's avatar
lhark committed
12

13
from users.models import User, Right, Ban, Whitelist, School
14
from users.models import DelRightForm, BanForm, WhitelistForm, DelSchoolForm
15
from users.models import InfoForm, BaseInfoForm, StateForm, RightForm, SchoolForm
root's avatar
root committed
16
from cotisations.models import Facture
chirac's avatar
chirac committed
17
from machines.models import Machine, Interface
18
from users.forms import PassForm
chirac's avatar
chirac committed
19
from machines.views import unassign_ips, assign_ips
chirac's avatar
chirac committed
20

21 22
from re2o.login import hashNT

chirac's avatar
chirac committed
23

24 25 26 27 28
def archive(user):
    """ Archive un utilisateur """
    unassign_ips(user)
    return

29

chirac's avatar
chirac committed
30 31 32 33 34
def unarchive(user):
    """ Triger actions au desarchivage d'un user """
    assign_ips(user)
    return

chirac's avatar
chirac committed
35 36 37
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
38 39 40 41 42 43
    return render_to_response(
        template,
        c,
        context_instance=RequestContext(request)
    )

chirac's avatar
chirac committed
44
@login_required
chirac's avatar
chirac committed
45
@permission_required('cableur')
chirac's avatar
chirac committed
46
def new_user(request):
47 48 49 50 51 52
    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)
53

chirac's avatar
chirac committed
54
@login_required
55
def edit_info(request, userid):
56 57 58
    if not request.user.has_perms(('cableur',)) and str(userid)!=str(request.user.id):
        messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit cableur")
        return redirect("/users/profil/" + str(request.user.id))
59 60 61
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
62
        messages.error(request, "Utilisateur inexistant")
63
        return redirect("/users/")
64 65 66 67
    if not request.user.has_perms(('cableur',)):
        user = BaseInfoForm(request.POST or None, instance=user)
    else:
        user = InfoForm(request.POST or None, instance=user)
68 69
    if user.is_valid():
        user.save()
70
        messages.success(request, "L'user a bien été modifié")
71
        return redirect("/users/profil/" + userid)
72
    return form({'userform': user}, 'users/user.html', request)
73

chirac's avatar
chirac committed
74
@login_required
chirac's avatar
chirac committed
75
@permission_required('bureau')
76 77 78 79
def state(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
80
        messages.error(request, "Utilisateur inexistant")
81
        return redirect("/users/")
82 83 84 85 86
    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)
chirac's avatar
chirac committed
87 88
            else:
                unarchive(user)
89
        state.save()
90
        messages.success(request, "Etat changé avec succès")
91
        return redirect("/users/profil/" + userid)
92
    return form({'userform': state}, 'users/user.html', request)
93

chirac's avatar
chirac committed
94
@login_required
95
def password(request, userid):
96 97 98
    if not request.user.has_perms(('cableur',)) and str(userid)!=str(request.user.id):
        messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit cableur")
        return redirect("/users/profil/" + str(request.user.id))
99 100 101
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
102
        messages.error(request, "Utilisateur inexistant")
103
        return redirect("/users/")
104 105 106
    if not request.user.has_perms(('bureau',)) and str(userid)!=str(request.user.id) and Right.objects.filter(user=user):
        messages.error(request, "Il faut les droits bureau pour modifier le mot de passe d'un membre actif")
        return redirect("/users/profil/" + str(request.user.id))
107 108 109 110 111 112 113
    u_form = PassForm(request.POST or None)
    if u_form.is_valid():
        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'])
        user.pwd_ntlm = hashNT(u_form.cleaned_data['passwd1'])
114
        user.save()
115
        messages.success(request, "Le mot de passe a changé")
116
        return redirect("/users/profil/" + userid)
117
    return form({'userform': u_form}, 'users/user.html', request)
chirac's avatar
chirac committed
118

chirac's avatar
chirac committed
119
@login_required
120
@permission_required('bureau')
121 122 123 124
def add_right(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
125
        messages.error(request, "Utilisateur inexistant")
126
        return redirect("/users/")
lhark's avatar
lhark committed
127 128
    right = RightForm(request.POST or None)
    if right.is_valid():
129 130 131 132 133 134 135
        right = right.save(commit=False)
        right.user = user
        try:
            right.save()
            messages.success(request, "Droit ajouté")
        except IntegrityError:
            pass
136
        return redirect("/users/profil/" + userid)
lhark's avatar
lhark committed
137 138
    return form({'userform': right}, 'users/user.html', request)

chirac's avatar
chirac committed
139
@login_required
140
@permission_required('bureau')
141 142 143 144 145 146 147 148 149
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)

chirac's avatar
chirac committed
150
@login_required
chirac's avatar
chirac committed
151
@permission_required('bofh')
152 153 154 155
def add_ban(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
156
        messages.error(request, "Utilisateur inexistant")
157 158 159 160 161 162
        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é")
163
        return redirect("/users/profil/" + userid)
164
    if is_ban(user):
165 166 167 168
        messages.error(
            request,
            "Attention, cet utilisateur a deja un bannissement actif"
        )
169 170
    return form({'userform': ban}, 'users/user.html', request)

chirac's avatar
chirac committed
171
@login_required
chirac's avatar
chirac committed
172
@permission_required('bofh')
173 174 175
def edit_ban(request, banid):
    try:
        ban_instance = Ban.objects.get(pk=banid)
chirac's avatar
chirac committed
176
    except Ban.DoesNotExist:
177
        messages.error(request, "Entrée inexistante")
178 179 180 181 182 183 184 185
        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)

chirac's avatar
chirac committed
186
@login_required
chirac's avatar
chirac committed
187
@permission_required('cableur')
chirac's avatar
chirac committed
188 189 190 191
def add_whitelist(request, userid):
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
192
        messages.error(request, "Utilisateur inexistant")
chirac's avatar
chirac committed
193 194 195 196 197 198
        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é")
199
        return redirect("/users/profil/" + userid)
chirac's avatar
chirac committed
200
    if is_whitelisted(user):
201 202 203 204
        messages.error(
            request,
            "Attention, cet utilisateur a deja un accès gracieux actif"
        )
chirac's avatar
chirac committed
205 206
    return form({'userform': whitelist}, 'users/user.html', request)

chirac's avatar
chirac committed
207
@login_required
chirac's avatar
chirac committed
208
@permission_required('cableur')
chirac's avatar
chirac committed
209 210 211 212
def edit_whitelist(request, whitelistid):
    try:
        whitelist_instance = Whitelist.objects.get(pk=whitelistid)
    except Whitelist.DoesNotExist:
213
        messages.error(request, "Entrée inexistante")
chirac's avatar
chirac committed
214 215 216 217 218 219 220 221
        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)

chirac's avatar
chirac committed
222
@login_required
chirac's avatar
chirac committed
223
@permission_required('cableur')
chirac's avatar
chirac committed
224 225 226 227 228
def add_school(request):
    school = SchoolForm(request.POST or None)
    if school.is_valid():
        school.save()
        messages.success(request, "L'établissement a été ajouté")
229
        return redirect("/users/index_school/")
230
    return form({'userform': school}, 'users/user.html', request)
chirac's avatar
chirac committed
231

chirac's avatar
chirac committed
232
@login_required
chirac's avatar
chirac committed
233
@permission_required('cableur')
234 235 236 237 238 239 240 241 242 243 244 245 246
def edit_school(request, schoolid):
    try:
        school_instance = School.objects.get(pk=schoolid)
    except School.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/users/")
    school = SchoolForm(request.POST or None, instance=school_instance)
    if school.is_valid():
        school.save()
        messages.success(request, "Établissement modifié")
        return redirect("/users/index_school/")
    return form({'userform': school}, 'users/user.html', request)

chirac's avatar
chirac committed
247
@login_required
chirac's avatar
chirac committed
248
@permission_required('cableur')
chirac's avatar
chirac committed
249 250 251 252 253 254 255 256 257
def del_school(request):
    school = DelSchoolForm(request.POST or None)
    if school.is_valid():
        school_dels = school.cleaned_data['schools']
        for school_del in school_dels:
            try:
                school_del.delete()
                messages.success(request, "L'établissement a été supprimé")
            except ProtectedError:
258 259 260
                messages.error(
                    request,
                    "L'établissement %s est affecté à au moins un user, \
lhark's avatar
lhark committed
261
                        vous ne pouvez pas le supprimer" % school_del)
262
        return redirect("/users/index_school/")
chirac's avatar
chirac committed
263 264
    return form({'userform': school}, 'users/user.html', request)

chirac's avatar
chirac committed
265
@login_required
266
@permission_required('cableur')
chirac's avatar
chirac committed
267
def index(request):
lhark's avatar
lhark committed
268
    users_list = User.objects.order_by('pk')
269
    return render(request, 'users/index.html', {'users_list': users_list})
root's avatar
root committed
270

chirac's avatar
chirac committed
271
@login_required
chirac's avatar
chirac committed
272
@permission_required('cableur')
273 274
def index_ban(request):
    ban_list = Ban.objects.order_by('date_start')
275
    return render(request, 'users/index_ban.html', {'ban_list': ban_list})
276

chirac's avatar
chirac committed
277
@login_required
chirac's avatar
chirac committed
278
@permission_required('cableur')
279 280
def index_white(request):
    white_list = Whitelist.objects.order_by('date_start')
281 282 283 284 285 286
    return render(
        request,
        'users/index_whitelist.html',
        {'white_list': white_list}
    )

chirac's avatar
chirac committed
287
@login_required
chirac's avatar
chirac committed
288
@permission_required('cableur')
289 290 291 292
def index_school(request):
    school_list = School.objects.order_by('name')
    return render(request, 'users/index_schools.html', {'school_list':school_list})

293 294 295 296
@login_required
def mon_profil(request):
    return redirect("/users/profil/" + str(request.user.id))

chirac's avatar
chirac committed
297
@login_required
298
def profil(request, userid):
299 300 301
    if not request.user.has_perms(('cableur',)) and str(userid)!=str(request.user.id):
        messages.error(request, "Vous ne pouvez pas afficher un autre user que vous sans droit cableur")
        return redirect("/users/profil/" + str(request.user.id))
302 303 304
    try:
        users = User.objects.get(pk=userid)
    except User.DoesNotExist:
305
        messages.error(request, "Utilisateur inexistant")
306
        return redirect("/users/")
307 308 309 310 311 312
    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)
313
    list_droits = Right.objects.filter(user=users)
314 315 316 317 318
    return render(
        request,
        'users/profil.html',
        {
            'user': users,
lhark's avatar
lhark committed
319
            'interfaces_list': machines,
320 321 322
            'facture_list': factures,
            'ban_list': bans,
            'white_list': whitelists,
chirac's avatar
chirac committed
323
            'list_droits': list_droits,
lhark's avatar
lhark committed
324 325
        }
    )
root's avatar
root committed
326