Projet

Général

Profil

0006-auth_saml-move-add-role-action-to-authenticators-app.patch

Valentin Deniaud, 21 septembre 2022 12:47

Télécharger (7,8 ko)

Voir les différences:

Subject: [PATCH 06/10] auth_saml: move add role action to authenticators app
 (#53442)

 .../migrations/0005_addroleaction.py          | 60 +++++++++++++++++++
 src/authentic2/apps/authenticators/models.py  | 16 +++++
 src/authentic2/apps/authenticators/views.py   |  5 +-
 .../migrations/0012_move_add_role_action.py   | 20 +++++++
 src/authentic2_auth_saml/models.py            | 22 ++-----
 5 files changed, 105 insertions(+), 18 deletions(-)
 create mode 100644 src/authentic2/apps/authenticators/migrations/0005_addroleaction.py
 create mode 100644 src/authentic2_auth_saml/migrations/0012_move_add_role_action.py
src/authentic2/apps/authenticators/migrations/0005_addroleaction.py
1
# Generated by Django 2.2.26 on 2022-09-20 15:20
2

  
3
import django.db.models.deletion
4
from django.conf import settings
5
from django.db import migrations, models
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        migrations.swappable_dependency(settings.RBAC_ROLE_MODEL),
12
        ('authenticators', '0004_auto_20220726_1708'),
13
        ('authentic2_auth_saml', '0012_move_add_role_action'),
14
    ]
15

  
16
    state_operations = [
17
        migrations.CreateModel(
18
            name='AddRoleAction',
19
            fields=[
20
                (
21
                    'id',
22
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
23
                ),
24
                (
25
                    'condition',
26
                    models.CharField(
27
                        blank=True, editable=False, max_length=256, verbose_name='Condition (unused)'
28
                    ),
29
                ),
30
                (
31
                    'mandatory',
32
                    models.BooleanField(default=False, editable=False, verbose_name='Mandatory (unused)'),
33
                ),
34
                (
35
                    'authenticator',
36
                    models.ForeignKey(
37
                        on_delete=django.db.models.deletion.CASCADE,
38
                        related_name='add_role_actions',
39
                        to='authenticators.BaseAuthenticator',
40
                    ),
41
                ),
42
                (
43
                    'role',
44
                    models.ForeignKey(
45
                        on_delete=django.db.models.deletion.CASCADE,
46
                        related_name='add_role_actions',
47
                        to=settings.RBAC_ROLE_MODEL,
48
                        verbose_name='Role',
49
                    ),
50
                ),
51
            ],
52
            options={
53
                'verbose_name': 'Add a role',
54
                'verbose_name_plural': 'Add roles',
55
                'default_related_name': 'add_role_actions',
56
            },
57
        ),
58
    ]
59

  
60
    operations = [migrations.SeparateDatabaseAndState(state_operations=state_operations)]
src/authentic2/apps/authenticators/models.py
27 27
from django.utils.translation import ugettext_lazy as _
28 28

  
29 29
from authentic2 import views
30
from authentic2.a2_rbac.models import Role
31
from authentic2.manager.utils import label_from_role
30 32
from authentic2.utils.evaluate import condition_validator, evaluate_condition
31 33

  
32 34
from .query import AuthenticatorManager
......
159 161
        return self._meta.verbose_name_plural
160 162

  
161 163

  
164
class AddRoleAction(AuthenticatorRelatedObjectBase):
165
    role = models.ForeignKey(Role, verbose_name=_('Role'), on_delete=models.CASCADE)
166
    condition = models.CharField(_('Condition (unused)'), editable=False, max_length=256, blank=True)
167
    mandatory = models.BooleanField(_('Mandatory (unused)'), editable=False, default=False)
168

  
169
    class Meta:
170
        default_related_name = 'add_role_actions'
171
        verbose_name = _('Add a role')
172
        verbose_name_plural = _('Add roles')
173

  
174
    def __str__(self):
175
        return label_from_role(self.role)
176

  
177

  
162 178
class LoginPasswordAuthenticator(BaseAuthenticator):
163 179
    remember_me = models.PositiveIntegerField(
164 180
        _('Remember me duration'),
src/authentic2/apps/authenticators/views.py
228 228
        model_name = kwargs.get('model_name')
229 229
        if model_name not in (x._meta.model_name for x in self.authenticator.related_models):
230 230
            raise Http404()
231
        self.model = apps.get_model(self.authenticator._meta.app_label, model_name)
231
        try:
232
            self.model = apps.get_model(self.authenticator._meta.app_label, model_name)
233
        except LookupError:
234
            self.model = apps.get_model('authenticators', model_name)
232 235

  
233 236
        return super().dispatch(request, *args, **kwargs)
234 237

  
src/authentic2_auth_saml/migrations/0012_move_add_role_action.py
1
# Generated by Django 2.2.26 on 2022-09-20 15:17
2

  
3
from django.db import migrations
4

  
5

  
6
class Migration(migrations.Migration):
7

  
8
    dependencies = [
9
        ('authentic2_auth_saml', '0011_alter_authenticator_foreign_key'),
10
    ]
11

  
12
    database_operations = [migrations.AlterModelTable('AddRoleAction', 'authenticators_addroleaction')]
13

  
14
    state_operations = [migrations.DeleteModel('AddRoleAction')]
15

  
16
    operations = [
17
        migrations.SeparateDatabaseAndState(
18
            database_operations=database_operations, state_operations=state_operations
19
        )
20
    ]
src/authentic2_auth_saml/models.py
20 20
from django.db import models
21 21
from django.utils.translation import gettext_lazy as _
22 22

  
23
from authentic2.a2_rbac.models import Role
24
from authentic2.apps.authenticators.models import AuthenticatorRelatedObjectBase, BaseAuthenticator
25
from authentic2.manager.utils import label_from_role
23
from authentic2.apps.authenticators.models import (
24
    AddRoleAction,
25
    AuthenticatorRelatedObjectBase,
26
    BaseAuthenticator,
27
)
26 28
from authentic2.utils.misc import redirect_to_login
27 29

  
28 30

  
......
268 270
        from authentic2.forms.widgets import SelectAttributeWidget
269 271

  
270 272
        return SelectAttributeWidget.get_options().get(self.user_field, self.user_field)
271

  
272

  
273
class AddRoleAction(AuthenticatorRelatedObjectBase):
274
    role = models.ForeignKey(Role, verbose_name=_('Role'), on_delete=models.CASCADE)
275
    condition = models.CharField(_('Condition (unused)'), editable=False, max_length=256, blank=True)
276
    mandatory = models.BooleanField(_('Mandatory (unused)'), editable=False, default=False)
277

  
278
    class Meta:
279
        default_related_name = 'add_role_actions'
280
        verbose_name = _('Add a role')
281
        verbose_name_plural = _('Add roles')
282

  
283
    def __str__(self):
284
        return label_from_role(self.role)
285
-