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-06-30 01:39:07 +00:00
|
|
|
|
2016-07-01 16:22:52 +00:00
|
|
|
from users.models import User, UserForm, InfoForm, PasswordForm, StateForm
|
2016-07-01 20:47:08 +00:00
|
|
|
from users.forms import PassForm
|
2016-06-30 18:18:06 +00:00
|
|
|
|
2016-07-01 22:35:44 +00:00
|
|
|
from re2o.login import makeSecret, hashNT
|
|
|
|
|
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):
|
|
|
|
if request.method == 'POST':
|
2016-06-30 23:03:28 +00:00
|
|
|
user = InfoForm(request.POST)
|
2016-06-30 18:18:06 +00:00
|
|
|
if user.is_valid():
|
|
|
|
user.save()
|
2016-07-01 16:22:52 +00:00
|
|
|
return form({'userform': user}, 'users/user.html', request)
|
2016-06-30 18:18:06 +00:00
|
|
|
else:
|
2016-06-30 23:03:28 +00:00
|
|
|
user = InfoForm()
|
2016-07-01 16:22:52 +00:00
|
|
|
return form({'userform': user}, 'users/user.html', request)
|
|
|
|
|
|
|
|
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()
|
|
|
|
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/")
|
|
|
|
user = StateForm(request.POST or None, instance=user)
|
|
|
|
if user.is_valid():
|
|
|
|
user.save()
|
|
|
|
return form({'userform': user}, 'users/user.html', request)
|
|
|
|
|
|
|
|
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()
|
|
|
|
return form({'userform': user_form}, 'users/user.html', request)
|
2016-07-01 22:35:44 +00:00
|
|
|
|
|
|
|
def index(request):
|
|
|
|
return render(request, 'users/index.html')
|