Projet

Général

Profil

0001-always-unlink-from-all-FC-accounts-fixes-19947.patch

Benjamin Dauvergne, 07 novembre 2017 17:48

Télécharger (14 ko)

Voir les différences:

Subject: [PATCH] always unlink from all FC accounts (fixes #19947)

 .../locale/fr/LC_MESSAGES/django.po                | 62 +++++++++++-----------
 .../templates/authentic2_auth_fc/linking.html      |  4 +-
 .../templates/authentic2_auth_fc/unlink.html       |  7 ++-
 src/authentic2_auth_fc/urls.py                     |  2 +-
 src/authentic2_auth_fc/views.py                    | 33 +++++-------
 tests/test_auth_fc.py                              | 11 ++++
 6 files changed, 58 insertions(+), 61 deletions(-)
src/authentic2_auth_fc/locale/fr/LC_MESSAGES/django.po
7 7
msgstr ""
8 8
"Project-Id-Version: authentic2-auth-fc 1.0\n"
9 9
"Report-Msgid-Bugs-To: \n"
10
"POT-Creation-Date: 2017-01-30 22:51+0100\n"
10
"POT-Creation-Date: 2017-11-07 17:32+0100\n"
11 11
"PO-Revision-Date: 2016-09-14 09:35+0200\n"
12 12
"Last-Translator: Benjamin Dauvergne <bdauvergne@entrouvert.com>\n"
13 13
"Language-Team: french <fr@li.org>\n"
......
21 21
msgid "FranceConnect"
22 22
msgstr "FranceConnect"
23 23

  
24
#: authentic2_auth_fc/models.py:25
24
#: authentic2_auth_fc/models.py:69
25 25
msgid "user"
26 26
msgstr "utilisateur"
27 27

  
28
#: authentic2_auth_fc/models.py:28
28
#: authentic2_auth_fc/models.py:72
29 29
msgid "sub"
30 30
msgstr "sub"
31 31

  
32
#: authentic2_auth_fc/models.py:30 authentic2_auth_fc/models.py:31
32
#: authentic2_auth_fc/models.py:74 authentic2_auth_fc/models.py:75
33 33
msgid "access token"
34 34
msgstr "jeton d'accès"
35 35

  
......
43 43
msgstr "Créez votre compte avec FranceConnect"
44 44

  
45 45
#: authentic2_auth_fc/templates/authentic2_auth_fc/connecting.html:18
46
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:29
46
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:27
47 47
msgid "What is FranceConnect?"
48 48
msgstr "Qu'est-ce que FranceConnect ?"
49 49

  
......
64 64
msgid "Linked FranceConnect accounts"
65 65
msgstr "Comptes FranceConnect associés"
66 66

  
67
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:15
67
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:14
68 68
msgid "Delete link"
69 69
msgstr "Supprimer la liaison"
70 70

  
71
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:22
71
#: authentic2_auth_fc/templates/authentic2_auth_fc/linking.html:20
72 72
msgid "Link with a FranceConnect account"
73 73
msgstr "Se lier avec un compte FranceConnect"
74 74

  
75 75
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:7
76
#, python-format
77 76
msgid ""
78 77
"\n"
79 78
"        You're about to delete the link between your user account and your "
80
"FranceConnect account :\n"
81
"        %(fc_account)s.\n"
79
"FranceConnect account.\n"
82 80
"        "
83 81
msgstr ""
84 82
"\n"
85 83
"Vous êtes sur le point de supprimer la liaison entre votre compte usager et "
86
"votre compte FranceConnect : %(fc_account)s."
84
"votre compte FranceConnect."
87 85

  
88
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:13
86
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:12
89 87
msgid ""
90 88
"\n"
91
"        Delete this link won't delete those accounts. However, once all the "
92
"links with FranceConnect\n"
89
"        Deleting this link won't delete those accounts. However, once all "
90
"the links with FranceConnect\n"
93 91
"        are deleted, it's no more possible to use the FranceConnect to "
94 92
"automatically login to your\n"
95 93
"        user account. It is still possible to link those accounts once again "
......
104 102
"utilisant FranceConnect. Il sera toujours possible de lier à nouveau ces "
105 103
"comptes en utilisant le bouton FranceConnect sur la page de connexion."
106 104

  
107
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:22
105
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:21
108 106
msgid ""
109 107
"\n"
110
"        Your user account has no password and remove this link may make this "
111
"account inaccessible.\n"
108
"        Your user account has no password and removing this link may make "
109
"this account inaccessible.\n"
112 110
"        To avoid this situation, please provide a password.\n"
113 111
"        "
114 112
msgstr ""
......
117 115
"rendre votre compte usager inaccessible. Pour éviter cela, merci de fournir "
118 116
"un mot de passe."
119 117

  
120
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:31
118
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:30
121 119
msgid "Unlink"
122 120
msgstr "Délier"
123 121

  
124
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:32
122
#: authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html:31
125 123
msgid "Cancel"
126 124
msgstr "Annuler"
127 125

  
128
#: authentic2_auth_fc/views.py:211
126
#: authentic2_auth_fc/views.py:231
129 127
msgid "You refused the connection."
130 128
msgstr "Vous avez refusé la connexion."
131 129

  
132
#: authentic2_auth_fc/views.py:223 authentic2_auth_fc/views.py:232
133
#: authentic2_auth_fc/views.py:245
130
#: authentic2_auth_fc/views.py:243 authentic2_auth_fc/views.py:252
131
#: authentic2_auth_fc/views.py:261 authentic2_auth_fc/views.py:268
132
#: authentic2_auth_fc/views.py:279
134 133
msgid "Unable to connect to FranceConnect."
135 134
msgstr "Impossible de se connecter à FranceConnect."
136 135

  
137
#: authentic2_auth_fc/views.py:300
136
#: authentic2_auth_fc/views.py:335
138 137
msgid "The FranceConnect account {} is already linked with another account."
139 138
msgstr "Le compte FranceConnect {} est déjà associé à un autre compte."
140 139

  
141
#: authentic2_auth_fc/views.py:317
140
#: authentic2_auth_fc/views.py:353
142 141
msgid "Your FranceConnect account {} with email {} has been linked."
143 142
msgstr "Votre compte FranceConnect {} avec le courriel {} a été associé."
144 143

  
145
#: authentic2_auth_fc/views.py:320
144
#: authentic2_auth_fc/views.py:357
146 145
msgid "Your FranceConnect account {} has been linked."
147 146
msgstr "Votre compte FranceConnect {} a été associé."
148 147

  
149
#: authentic2_auth_fc/views.py:326
148
#: authentic2_auth_fc/views.py:363
150 149
msgid "Your local account has been updated."
151 150
msgstr "Votre compte local a été mis à jour."
152 151

  
153
#: authentic2_auth_fc/views.py:341
152
#: authentic2_auth_fc/views.py:399
154 153
msgid ""
155 154
"If you already have an account, please log in, else create your account."
156 155
msgstr ""
157 156
"Si vous avez déjà un compte saisissez vos identifiants sinon créez votre "
158 157
"compte."
159 158

  
160
#: authentic2_auth_fc/views.py:359
159
#: authentic2_auth_fc/views.py:429
161 160
msgid "FranceConnect didn't provide your email address, please do."
162 161
msgstr "FranceConnect n'a pas fourni votre adresse email, merci de le faire."
163 162

  
164
#: authentic2_auth_fc/views.py:420
165
#, python-brace-format
166
msgid "The link with the FranceConnect account {fc_account} has been deleted."
167
msgstr "La liaison avec le compte FranceConnect {fc_account} a été supprimée"
163
#: authentic2_auth_fc/views.py:478
164
msgid "The link with the FranceConnect account has been deleted."
165
msgstr "La liaison avec le compte FranceConnect a été supprimée."
src/authentic2_auth_fc/templates/authentic2_auth_fc/linking.html
11 11
          {% trans "Linked FranceConnect accounts" %}
12 12
        </p>
13 13
        <ul class="fond">
14
        {% for fc_account in user.fc_accounts.all %}
15
        <li class="picto utilisateur"><p class="lien">{{ fc_account }}{% if unlink %} <a href="{% url 'fc-unlink' pk=fc_account.pk %}">{% trans 'Delete link'%}</a>{% endif %}</p></li>
16
        {% endfor %}
14
        <li class="picto utilisateur"><p class="lien">{{ user.fc_accounts.all.0 }}{% if unlink %} <a href="{% url 'fc-unlink' %}">{% trans 'Delete link'%}</a>{% endif %}</p></li>
17 15
        </ul>
18 16
      {% else %}
19 17
        <p>
src/authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html
5 5
{% block content %}
6 6
    <p>
7 7
        {% blocktrans %}
8
        You're about to delete the link between your user account and your FranceConnect account :
9
        {{ fc_account }}.
8
        You're about to delete the link between your user account and your FranceConnect account.
10 9
        {% endblocktrans %}
11 10
    </p>
12 11
    <p>
13 12
        {% blocktrans %}
14
        Delete this link won't delete those accounts. However, once all the links with FranceConnect
13
        Deleting this link won't delete those accounts. However, once all the links with FranceConnect
15 14
        are deleted, it's no more possible to use the FranceConnect to automatically login to your
16 15
        user account. It is still possible to link those accounts once again by using the
17 16
        button FranceConnect on the login page.
......
20 19
    {% if no_password %}
21 20
    <p>
22 21
        {% blocktrans %}
23
        Your user account has no password and remove this link may make this account inaccessible.
22
        Your user account has no password and removing this link may make this account inaccessible.
24 23
        To avoid this situation, please provide a password.
25 24
        {% endblocktrans %}
26 25
    </p>
src/authentic2_auth_fc/urls.py
10 10
urlpatterns = patterns('',
11 11
        url(r'^fc/', include(fcpatterns)),
12 12
        url(r'^accounts/fc/register/$', views.registration, name='fc-registration'),
13
        url(r'^accounts/fc/unlink/(?P<pk>\d+)/$', views.unlink, name='fc-unlink'),
13
        url(r'^accounts/fc/unlink/$', views.unlink, name='fc-unlink'),
14 14
)
src/authentic2_auth_fc/views.py
437 437
        return HttpResponseRedirect(activation_url)
438 438

  
439 439

  
440
class UnlinkView(LoggerMixin, SingleObjectMixin, FormView):
441
    model = models.FcAccount
440
class UnlinkView(LoggerMixin, FormView):
442 441
    template_name = 'authentic2_auth_fc/unlink.html'
443 442

  
444 443
    def get_success_url(self):
......
449 448

  
450 449
    def get_form_class(self):
451 450
        form_class = Form
452
        if not self.fc_account.user.has_usable_password():
451
        if not self.request.user.has_usable_password():
453 452
            form_class = SET_PASSWORD_FORM_CLASS
454 453
        return form_class
455 454

  
456 455
    def get_form_kwargs(self, **kwargs):
457 456
        kwargs = super(UnlinkView, self).get_form_kwargs(**kwargs)
458
        if not self.fc_account.user.has_usable_password():
459
            kwargs['user'] = self.fc_account.user
457
        if not self.request.user.has_usable_password():
458
            kwargs['user'] = self.request.user
460 459
        return kwargs
461 460

  
462 461
    def dispatch(self, request, *args, **kwargs):
......
467 466
                a2_app_settings.A2_REGISTRATION_CAN_CHANGE_PASSWORD:
468 467
            # Prevent access to the view.
469 468
            raise Http404
470
        self.fc_account = self.object = self.get_object()
471
        self.check_access(self.fc_account)
472 469
        return super(UnlinkView, self).dispatch(request, *args, **kwargs)
473 470

  
474
    def check_access(self, fc_account):
475
        if self.request.user != fc_account.user:
476
            raise PermissionDenied
477

  
478 471
    def form_valid(self, form):
479
        if not self.fc_account.user.has_usable_password():
472
        if not self.request.user.has_usable_password():
480 473
            form.save()
481
            self.logger.info(u'user %s has set a password', self.fc_account.user)
482
            self.fc_account.user.backend = 'authentic2.backends.models_backend.ModelBackend'
483
        msg_tpl = _('The link with the FranceConnect account {fc_account} has been deleted.')
484
        msg = msg_tpl.format(fc_account=self.fc_account)
485
        self.logger.info(u'user %s unlinked from %s', self.fc_account.user, self.fc_account)
486
        self.fc_account.delete()
487
        messages.info(self.request, msg)
474
            self.logger.info(u'user %s has set a password', self.request.user)
475
        links = models.FcAccount.objects.filter(user=self.request.user)
476
        for link in links:
477
            self.logger.info(u'user %s unlinked from %s', self.request.user, link)
478
        messages.info(self.request, _('The link with the FranceConnect account has been deleted.'))
479
        links.delete()
488 480
        return super(UnlinkView, self).form_valid(form)
489 481

  
490 482
    def get_context_data(self, **kwargs):
491 483
        context = super(UnlinkView, self).get_context_data(**kwargs)
492
        context['fc_account'] = self.fc_account
493
        if not self.fc_account.user.has_usable_password():
484
        if not self.request.user.has_usable_password():
494 485
            context['no_password'] = True
495 486
        return context
496 487

  
tests/test_auth_fc.py
13 13

  
14 14
from authentic2.utils import timestamp_from_datetime
15 15

  
16
from authentic2_auth_fc import models
17

  
16 18

  
17 19
User = get_user_model()
18 20

  
......
99 101
        assert User.objects.count() == 0
100 102
    else:
101 103
        assert User.objects.count() == 1
104
    if User.objects.count():
105
        assert models.FcAccount.objects.count() == 1
106
        response = app.get('/accounts/')
107
        response = response.click('Delete link')
108
        response.form.set('new_password1', 'ikKL1234')
109
        response.form.set('new_password2', 'ikKL1234')
110
        response = response.form.submit(name='unlink')
111
        assert 'The link with the FranceConnect account has been deleted' in response.content
112
        assert models.FcAccount.objects.count() == 0
102 113

  
103 114

  
104 115
def test_login_email_is_unique(app, fc_settings, caplog):
105
-