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/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" value="true"> |
|
11 |
<div class=warningnotice> |
|
12 |
<p>{% trans "This user may already exist, please check the list below before creating it :" %}</p> |
|
13 |
<ul> |
|
14 |
{% for user in duplicate_users %} |
|
15 |
<li><a href="{% url 'a2-manager-user-detail' pk=user.pk %}">{{ user.get_full_name }}</a> |
|
16 |
- {% blocktrans with date=user.date_joined %}Created on {{ date }}{% endblocktrans %}</li> |
|
17 |
{% endfor %} |
|
18 |
</ul> |
|
19 |
</div> |
|
20 |
{% endif %} |
|
21 |
{% endblock %} |
|
22 | ||
8 | 23 |
{% block hidden_inputs %} |
9 | 24 |
{{ block.super }} |
10 | 25 |
{% 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 |
duplicate_users = User.objects.find_duplicates( |
|
203 |
first_name=form.cleaned_data['first_name'], |
|
204 |
last_name=form.cleaned_data['last_name'], |
|
205 |
birthdate=form.cleaned_data.get('birthdate'), |
|
206 |
) |
|
207 |
if duplicate_users and not 'confirm-creation' in self.request.POST: |
|
208 |
self.duplicate_users = duplicate_users |
|
209 |
return self.form_invalid(form) |
|
210 | ||
199 | 211 |
response = super(UserAddView, self).form_valid(form) |
200 | 212 |
hooks.call_hooks('event', name='manager-add-user', user=self.request.user, |
201 | 213 |
instance=form.instance, form=form) |
tests/test_user_manager.py | ||
---|---|---|
920 | 920 |
resp = resp.follow() |
921 | 921 |
assert resp.html.find('td').text == \ |
922 | 922 |
'This user has not granted profile data access to any service yet.' |
923 | ||
924 | ||
925 |
def test_manager_create_user_duplicates(admin, app, ou1, settings): |
|
926 |
settings.A2_MANAGER_CHECK_DUPLICATE_USERS = True |
|
927 |
login(app, admin) |
|
928 |
user = User.objects.create(first_name='Alexander', last_name='Longname') |
|
929 |
user2 = User.objects.create(first_name='Alexandra', last_name='Longname') |
|
930 | ||
931 |
url = '/manage/users/%s/add/' % ou1.pk |
|
932 |
resp = app.get(url) |
|
933 | ||
934 |
form = resp.form |
|
935 |
form.set('first_name', 'Alexandre') |
|
936 |
form.set('last_name', 'Longname') |
|
937 |
form.set('email', 'alexandre.longname@entrouvert.com') |
|
938 |
form.set('password1', 'ABcd1234') |
|
939 |
form.set('password2', 'ABcd1234') |
|
940 |
resp = form.submit() |
|
941 | ||
942 |
assert 'user may already exist' in resp.text |
|
943 |
assert '/users/%s/' % user.pk in resp.text |
|
944 |
assert '/users/%s/' % user2.pk in resp.text |
|
945 | ||
946 |
# simply submitting again creates user |
|
947 |
resp = resp.form.submit().follow() |
|
948 |
assert User.objects.filter(first_name='Alexandre').count() == 1 |
|
923 |
- |