0001-manager-look-for-duplicates-on-user-creation-45419.patch
src/authentic2/manager/app_settings.py | ||
---|---|---|
28 | 28 |
'USER_SEARCH_MINIMUM_CHARS': 0, |
29 | 29 |
'LOGIN_URL': None, |
30 | 30 |
'SITE_TITLE': None, |
31 |
'CHECK_DUPLICATE_USERS': False, |
|
31 | 32 |
} |
32 | 33 | |
33 | 34 |
def __getattr__(self, name): |
src/authentic2/manager/templates/authentic2/manager/duplicate_user_add.html | ||
---|---|---|
1 |
{% load i18n %} |
|
2 | ||
3 |
<li> |
|
4 |
<a href="{% url 'a2-manager-user-detail' pk=user.pk %}">{{ user.get_full_name }}</a> |
|
5 |
{% if user.email %}- {{ user.email }}{% endif %} |
|
6 |
{% if form.cleaned_data.birthdate and user.attributes.birthdate %} |
|
7 |
- {% trans "Birthdate:" %} {{ user.attributes.birthdate|date }} |
|
8 |
{% endif %} |
|
9 |
- {% blocktrans with date=user.date_joined %}Created on {{ date }}{% endblocktrans %} |
|
10 |
{% if user.last_login %} |
|
11 |
- {% blocktrans with date=user.last_login %}Last login on {{ date }}{% endblocktrans %} |
|
12 |
{% else %} |
|
13 |
- {% trans "Never logged in" %} |
|
14 |
{% endif %} |
|
15 |
</li> |
src/authentic2/manager/templates/authentic2/manager/user_add.html | ||
---|---|---|
5 | 5 |
{% trans "Add an user" %} |
6 | 6 |
{% endblock %} |
7 | 7 | |
8 |
{% block beforeform %} |
|
9 |
{% if duplicate_users %} |
|
10 |
<input type="hidden" name="confirm-creation-token" value="{{ form.cleaned_data.first_name }} {{ form.cleaned_data.last_name }}"> |
|
11 |
<div class=warningnotice> |
|
12 |
<p>{% trans "This user may already exist, please check the list below before creating it :" %}</p> |
|
13 |
<ul class="user-duplicates"> |
|
14 |
{% for user in duplicate_users %} |
|
15 |
{% include "authentic2/manager/duplicate_user_add.html" with user=user %} |
|
16 |
{% endfor %} |
|
17 |
</ul> |
|
18 |
</div> |
|
19 |
{% endif %} |
|
20 |
{% endblock %} |
|
21 | ||
8 | 22 |
{% block hidden_inputs %} |
9 | 23 |
{{ block.super }} |
10 | 24 |
{% if next %}<input type="hidden" name="next" value="{{ next }}">{% endif %} |
src/authentic2/manager/user_views.py | ||
---|---|---|
148 | 148 |
form_class = UserAddForm |
149 | 149 |
permissions = ['custom_user.add_user'] |
150 | 150 |
template_name = 'authentic2/manager/user_add.html' |
151 |
duplicate_users = None |
|
151 | 152 | |
152 | 153 |
def dispatch(self, request, *args, **kwargs): |
153 | 154 |
qs = request.user.ous_with_perm('custom_user.add_user') |
... | ... | |
193 | 194 |
field_name='cancel') |
194 | 195 |
context['next'] = select_next_url(self.request, default=None, include_post=True) |
195 | 196 |
context['ou'] = self.ou |
197 |
context['duplicate_users'] = self.duplicate_users |
|
196 | 198 |
return context |
197 | 199 | |
198 | 200 |
def form_valid(self, form): |
201 |
if app_settings.CHECK_DUPLICATE_USERS: |
|
202 |
first_name = form.cleaned_data['first_name'] |
|
203 |
last_name = form.cleaned_data['last_name'] |
|
204 |
duplicate_users = User.objects.find_duplicates( |
|
205 |
first_name=first_name, |
|
206 |
last_name=last_name, |
|
207 |
birthdate=form.cleaned_data.get('birthdate'), |
|
208 |
) |
|
209 |
token = self.request.POST.get('confirm-creation-token') |
|
210 |
valid_confirmation_token = bool(token == '%s %s' % (first_name, last_name)) |
|
211 |
if duplicate_users and not valid_confirmation_token: |
|
212 |
self.duplicate_users = duplicate_users |
|
213 |
return self.form_invalid(form) |
|
214 | ||
199 | 215 |
response = super(UserAddView, self).form_valid(form) |
200 | 216 |
hooks.call_hooks('event', name='manager-add-user', user=self.request.user, |
201 | 217 |
instance=form.instance, form=form) |
tests/test_user_manager.py | ||
---|---|---|
18 | 18 |
from __future__ import unicode_literals |
19 | 19 | |
20 | 20 |
import csv |
21 |
import datetime |
|
21 | 22 |
import re |
22 | 23 |
import time |
23 | 24 |
from urllib.parse import urlparse |
... | ... | |
920 | 921 |
resp = resp.follow() |
921 | 922 |
assert resp.html.find('td').text == \ |
922 | 923 |
'This user has not granted profile data access to any service yet.' |
924 | ||
925 | ||
926 |
def test_manager_create_user_duplicates(admin, app, ou1, settings): |
|
927 |
settings.A2_MANAGER_CHECK_DUPLICATE_USERS = True |
|
928 |
Attribute.objects.create(kind='birthdate', name='birthdate', label='birthdate', required=False) |
|
929 | ||
930 |
user = User.objects.create(first_name='Alexander', last_name='Longname') |
|
931 |
user.attributes.birthdate = datetime.date(1980, 1, 2) |
|
932 |
user2 = User.objects.create(first_name='Alexandra', last_name='Longname') |
|
933 |
user3 = User.objects.create(first_name='Alex', last_name='Shortname') |
|
934 | ||
935 |
login(app, admin) |
|
936 |
resp = app.get('/manage/users/%s/add/' % ou1.pk) |
|
937 | ||
938 |
form = resp.form |
|
939 |
form.set('first_name', 'Alexandre') |
|
940 |
form.set('last_name', 'Longname') |
|
941 |
form.set('email', 'alexandre.longname@entrouvert.com') |
|
942 |
form.set('birthdate', '1980-01-02') |
|
943 |
form.set('password1', 'ABcd1234') |
|
944 |
form.set('password2', 'ABcd1234') |
|
945 |
resp = form.submit() |
|
946 | ||
947 |
assert 'user may already exist' in resp.text |
|
948 |
assert 'Birthdate' in resp.text |
|
949 |
assert '/users/%s/' % user.pk in resp.text |
|
950 |
assert '/users/%s/' % user2.pk in resp.text |
|
951 | ||
952 |
# This user was in fact duplicate. Agent reuses the form to fill details on another user |
|
953 |
form = resp.form |
|
954 |
form.set('first_name', 'Alexa') |
|
955 |
form.set('last_name', 'Shortname') |
|
956 |
form.set('email', 'ashortname@entrouvert.com') |
|
957 |
resp = form.submit() |
|
958 | ||
959 |
assert 'user may already exist' in resp.text |
|
960 |
assert '/users/%s/' % user3.pk in resp.text |
|
961 | ||
962 |
# Not a duplicate this time. Simply submitting again creates user |
|
963 |
resp = resp.form.submit().follow() |
|
964 |
assert User.objects.filter(first_name='Alexa').count() == 1 |
|
923 |
- |