|
@@ -7,12 +7,17 @@ from django.contrib.auth import login as django_login
|
7
|
7
|
from django.contrib.auth import logout as django_logout
|
8
|
8
|
from django.contrib.auth.decorators import login_required
|
9
|
9
|
from django.contrib.auth.forms import AuthenticationForm
|
|
10
|
+from django.contrib.auth.models import User
|
|
11
|
+from django.utils.encoding import force_str
|
|
12
|
+from django.utils.http import urlsafe_base64_decode
|
10
|
13
|
from django.utils.translation import gettext as _
|
11
|
|
-from .forms import UserRegistrationForm, UserProfileForm
|
|
14
|
+from .forms import UserRegistrationForm, UserProfileForm, UserActivationForm
|
12
|
15
|
import logging
|
13
|
16
|
from .models import get_userprofile
|
14
|
17
|
from themes import Context
|
|
18
|
+from . tokens import generate_token
|
15
|
19
|
import users
|
|
20
|
+from users import emails
|
16
|
21
|
from users import parameter
|
17
|
22
|
|
18
|
23
|
logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
|
|
@@ -66,9 +71,16 @@ def register(request):
|
66
|
71
|
else:
|
67
|
72
|
form = UserRegistrationForm(request.POST)
|
68
|
73
|
if form.is_valid():
|
|
74
|
+ # Deactivate the user, if validation or activation is required
|
|
75
|
+ if parameter.get(parameter.USERS_MAIL_VALIDATION) or parameter.get(parameter.USERS_ADMIN_ACTIVATION):
|
|
76
|
+ form.instance.is_active = False
|
69
|
77
|
form.save()
|
70
|
|
- messages.success(request, _('Your account has been created! You are able to log in as %(username)s.') %
|
71
|
|
- {'username': form.cleaned_data.get('username')})
|
|
78
|
+ # Send welcome message
|
|
79
|
+ emails.send_welcome_mail(form.instance)
|
|
80
|
+ if parameter.get(parameter.USERS_MAIL_VALIDATION):
|
|
81
|
+ emails.send_validation_mail(form.instance, request)
|
|
82
|
+ # Add success message
|
|
83
|
+ messages.success(request, parameter.registration_flow_description(form.cleaned_data.get('username')))
|
72
|
84
|
return redirect('users-login')
|
73
|
85
|
else:
|
74
|
86
|
messages.error(request, _('Registration failed!'))
|
|
@@ -95,12 +107,17 @@ def login(request):
|
95
|
107
|
messages.success(request, _('You are now logged in as %(username)s.') % {'username': username})
|
96
|
108
|
return redirect(request.GET.get('next') or '/')
|
97
|
109
|
else:
|
98
|
|
- if parameter.get(parameter.USERS_SELF_REGISTRATION):
|
99
|
|
- 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>.') %
|
100
|
|
- {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
|
|
110
|
+ username = form.cleaned_data.get('username')
|
|
111
|
+ user = User.objects.get(username=username)
|
|
112
|
+ if user.is_active:
|
|
113
|
+ if parameter.get(parameter.USERS_SELF_REGISTRATION):
|
|
114
|
+ 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>.') %
|
|
115
|
+ {'url_register': users.url_register(request), 'url_recover': users.url_password_recovery(request)})
|
|
116
|
+ else:
|
|
117
|
+ messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a>.') %
|
|
118
|
+ {'url_recover': users.url_password_recovery(request)})
|
101
|
119
|
else:
|
102
|
|
- messages.error(request, _('Login failed! You can do a password recorvery <a href="%(url_recover)s">here</a>.') %
|
103
|
|
- {'url_recover': users.url_password_recovery(request)})
|
|
120
|
+ messages.info(request, _("The account is deactivated. Confirm your email adress and wait for the administrator to activate your account."))
|
104
|
121
|
|
105
|
122
|
context['form'] = form
|
106
|
123
|
return render(request, 'users/login.html', context)
|
|
@@ -120,3 +137,79 @@ def logout(request):
|
120
|
137
|
for variable in session_cache:
|
121
|
138
|
request.session[variable] = session_cache[variable]
|
122
|
139
|
return redirect(request.GET.get('next') or '/')
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+def validate(request, uidb64, token):
|
|
143
|
+ context = Context(request) # needs to be executed first because of time mesurement
|
|
144
|
+ try:
|
|
145
|
+ uid = force_str(urlsafe_base64_decode(uidb64))
|
|
146
|
+ except (TypeError, ValueError, OverflowError, User.DoesNotExist):
|
|
147
|
+ uid = None
|
|
148
|
+ myuser = None
|
|
149
|
+ else:
|
|
150
|
+ try:
|
|
151
|
+ myuser = User.objects.get(pk=uid)
|
|
152
|
+ except User.DoesNotExist:
|
|
153
|
+ myuser = None
|
|
154
|
+
|
|
155
|
+ if myuser is not None and generate_token.check_token(myuser, token):
|
|
156
|
+ # Store mail validation to user profile
|
|
157
|
+ profile = get_userprofile(myuser)
|
|
158
|
+ profile.mail_validated = True
|
|
159
|
+ profile.save()
|
|
160
|
+ if not parameter.get(parameter.USERS_ADMIN_ACTIVATION):
|
|
161
|
+ # Activate user
|
|
162
|
+ myuser.is_active = True
|
|
163
|
+ myuser.save()
|
|
164
|
+ messages.success(request, _("Your Account has been activated."))
|
|
165
|
+ return redirect('users-login')
|
|
166
|
+ else:
|
|
167
|
+ emails.send_activation_mail(myuser, request)
|
|
168
|
+ messages.success(request, _("Your Email has been validated. Wait for the administrator to activate your account"))
|
|
169
|
+ return redirect("/")
|
|
170
|
+ else:
|
|
171
|
+ context_adaption(
|
|
172
|
+ context,
|
|
173
|
+ request,
|
|
174
|
+ _('Validation failed'),
|
|
175
|
+ )
|
|
176
|
+ messages.info(request, _("Vaildation failed. The system administrator will be informed."))
|
|
177
|
+ emails.send_validation_failed(uid, token)
|
|
178
|
+ return redirect("/")
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+@login_required
|
|
182
|
+def activate(request, pk):
|
|
183
|
+ context = Context(request) # needs to be executed first because of time mesurement
|
|
184
|
+ if not request.POST:
|
|
185
|
+ if request.user.is_superuser:
|
|
186
|
+ user_to_be_activated = User.objects.get(pk=pk)
|
|
187
|
+ if not user_to_be_activated.is_active:
|
|
188
|
+ user_to_be_activated.is_active = True
|
|
189
|
+ form = UserActivationForm(instance=user_to_be_activated)
|
|
190
|
+ context_adaption(
|
|
191
|
+ context,
|
|
192
|
+ request,
|
|
193
|
+ _('Activation of user: %s') % f"{user_to_be_activated.username} - {user_to_be_activated.email}",
|
|
194
|
+ form=form,
|
|
195
|
+ )
|
|
196
|
+ return render(request, 'users/activate.html', context)
|
|
197
|
+ else:
|
|
198
|
+ messages.error(request, _("The user %s is already active.") % user_to_be_activated.username)
|
|
199
|
+ else:
|
|
200
|
+ messages.error(request, _("You are no administrator. Log in as administrator and try again!"))
|
|
201
|
+ else:
|
|
202
|
+ submit = request.POST.get("submit")
|
|
203
|
+ delete = request.POST.get("delete")
|
|
204
|
+ user_to_be_activated = User.objects.get(pk=pk)
|
|
205
|
+ if submit:
|
|
206
|
+ form = UserActivationForm(request.POST, instance=user_to_be_activated)
|
|
207
|
+ if form.is_valid():
|
|
208
|
+ form.save()
|
|
209
|
+ messages.info(request, _("User permissions changed."))
|
|
210
|
+ else:
|
|
211
|
+ messages.error(request, _("Error while processing user change form"))
|
|
212
|
+ if delete:
|
|
213
|
+ user_to_be_activated.delete()
|
|
214
|
+ messages.info(request, _("User deleted."))
|
|
215
|
+ return redirect("/")
|