|
@@ -11,7 +11,7 @@ from django.contrib.auth.models import User
|
11
|
11
|
from django.utils.encoding import force_str
|
12
|
12
|
from django.utils.http import urlsafe_base64_decode
|
13
|
13
|
from django.utils.translation import gettext as _
|
14
|
|
-from .forms import UserRegistrationForm, UserProfileForm, UserActivationForm, UserPasswordChangeForm
|
|
14
|
+from .forms import PasswordRecoverForm, UserRegistrationForm, UserProfileForm, UserActivationForm, UserPasswordChangeForm, PasswordRecoverChangeForm
|
15
|
15
|
import logging
|
16
|
16
|
from .models import get_userprofile
|
17
|
17
|
from themes import Context
|
|
@@ -59,6 +59,26 @@ def profile(request):
|
59
|
59
|
)
|
60
|
60
|
return render(request, 'users/profile.html', context=context)
|
61
|
61
|
|
|
62
|
+def recover(request):
|
|
63
|
+ context = Context(request) # needs to be executed first because of time mesurement
|
|
64
|
+ context_adaption(context, request, _('Password Recovery'))
|
|
65
|
+ if not request.POST:
|
|
66
|
+ form = PasswordRecoverForm(request)
|
|
67
|
+ else:
|
|
68
|
+ username = request.POST.get("username")
|
|
69
|
+ try:
|
|
70
|
+ user = User.objects.get(username=username)
|
|
71
|
+ except User.DoesNotExist:
|
|
72
|
+ pass # hide non existing user (just do nothing)
|
|
73
|
+ else:
|
|
74
|
+ profile = get_userprofile(user)
|
|
75
|
+ if profile.mail_validated:
|
|
76
|
+ emails.send_recover_mail(user, request)
|
|
77
|
+ #
|
|
78
|
+ messages.info(request, _("If the user exists, you will get a reover email."))
|
|
79
|
+ return redirect("users-login")
|
|
80
|
+ context['form'] = form
|
|
81
|
+ return render(request, 'users/recover.html', context)
|
62
|
82
|
|
63
|
83
|
def register(request):
|
64
|
84
|
context = Context(request) # needs to be executed first because of time mesurement
|
|
@@ -116,10 +136,10 @@ def login(request):
|
116
|
136
|
if is_active:
|
117
|
137
|
if parameter.get(parameter.USERS_SELF_REGISTRATION):
|
118
|
138
|
messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a> or you can register <a href="%(url_register)s">here</a>.') %
|
119
|
|
- {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
|
|
139
|
+ {'url_register': users.url_register(request), 'url_recover': users.url_recover(request)})
|
120
|
140
|
else:
|
121
|
141
|
messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a>.') %
|
122
|
|
- {'url_recover': users.url_password_recovery(request)})
|
|
142
|
+ {'url_recover': users.url_recover(request)})
|
123
|
143
|
else:
|
124
|
144
|
messages.info(request, _("The account is deactivated. Confirm your email adress and wait for the administrator to activate your account."))
|
125
|
145
|
|
|
@@ -227,3 +247,44 @@ def activate(request, pk):
|
227
|
247
|
user_to_be_activated.delete()
|
228
|
248
|
messages.info(request, _("User deleted."))
|
229
|
249
|
return redirect("/")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+def recover_token(request, uidb64, token):
|
|
253
|
+ context = Context(request) # needs to be executed first because of time mesurement
|
|
254
|
+ print(settings.PASSWORD_RESET_TIMEOUT)
|
|
255
|
+ try:
|
|
256
|
+ uid = force_str(urlsafe_base64_decode(uidb64))
|
|
257
|
+ except (TypeError, ValueError, OverflowError, User.DoesNotExist):
|
|
258
|
+ uid = None
|
|
259
|
+ myuser = None
|
|
260
|
+ else:
|
|
261
|
+ try:
|
|
262
|
+ myuser = User.objects.get(pk=uid)
|
|
263
|
+ except User.DoesNotExist:
|
|
264
|
+ myuser = None
|
|
265
|
+
|
|
266
|
+ if myuser is not None and generate_token.check_token(myuser, token):
|
|
267
|
+ if request.POST:
|
|
268
|
+ form = PasswordRecoverChangeForm(myuser, data=request.POST)
|
|
269
|
+ if form.is_valid():
|
|
270
|
+ form.save()
|
|
271
|
+ return redirect(request.GET.get('next') or 'users-login')
|
|
272
|
+ else:
|
|
273
|
+ form = PasswordRecoverChangeForm(myuser)
|
|
274
|
+ #
|
|
275
|
+ context_adaption(
|
|
276
|
+ context,
|
|
277
|
+ request,
|
|
278
|
+ _('Password recovery for %(username)s') % {'username': myuser.username},
|
|
279
|
+ form=form,
|
|
280
|
+ )
|
|
281
|
+ return render(request, 'users/recover.html', context=context)
|
|
282
|
+ else:
|
|
283
|
+ context_adaption(
|
|
284
|
+ context,
|
|
285
|
+ request,
|
|
286
|
+ _('Recovery failed'),
|
|
287
|
+ )
|
|
288
|
+ messages.info(request, _("Recovery failed. The system administrator will be informed."))
|
|
289
|
+ emails.send_recover_failed(uid, token)
|
|
290
|
+ return redirect("/")
|