Projet

Général

Profil

0001-import_site-allow-creating-roles-in-default-ou-51464.patch

Valentin Deniaud, 08 décembre 2021 16:23

Télécharger (3,6 ko)

Voir les différences:

Subject: [PATCH 1/2] import_site: allow creating roles in default ou (#51464)

 src/authentic2/data_transfer.py                   |  6 ++++++
 src/authentic2/management/commands/import_site.py |  2 ++
 tests/test_import_export_site_cmd.py              | 11 +++++++++++
 3 files changed, 19 insertions(+)
src/authentic2/data_transfer.py
25 25
from django.utils.translation import ugettext_lazy as _
26 26

  
27 27
from authentic2.a2_rbac.models import OrganizationalUnit, Permission, Role, RoleAttribute, RoleParenting
28
from authentic2.a2_rbac.utils import get_default_ou
28 29
from authentic2.decorators import errorcollector
29 30
from authentic2.utils.lazy import lazy_join
30 31
from django_rbac.models import Operation
......
159 160
        ou_delete_orphans=False,
160 161
        set_ou=None,
161 162
        allowed_ous=None,
163
        set_absent_ou_to_default=None,
162 164
    ):
163 165
        self.import_roles = import_roles
164 166
        self.import_ous = import_ous
......
169 171
        self.role_attributes_update = role_attributes_update
170 172
        self.set_ou = set_ou
171 173
        self.allowed_ous = allowed_ous
174
        self.set_absent_ou_to_default = set_absent_ou_to_default
172 175

  
173 176

  
174 177
def wraps_validationerror(func):
......
222 225
                raise ValidationError(
223 226
                    _("Can't import role because missing permissions on Organizational Unit: %s") % ou_d
224 227
                )
228
        elif self._import_context.set_absent_ou_to_default:
229
            ou = get_default_ou()
230
            has_ou = True
225 231
        else:
226 232
            name = self._role_d.get('name') or self._role_d.get('slug') or self._role_d.get('uuid')
227 233
            raise ValidationError(_("Missing Organizational Unit for role: %s") % name)
src/authentic2/management/commands/import_site.py
60 60
                'no-role-permissions-update',
61 61
                'no-role-attributes-update',
62 62
                'no-role-parentings-update',
63
                'no-role-parentings-update',
64
                'set-absent-ou-to-default',
63 65
            ],
64 66
        )
65 67

  
tests/test_import_export_site_cmd.py
24 24
from django.core.exceptions import ValidationError
25 25

  
26 26
from authentic2.a2_rbac.models import Role
27
from authentic2.a2_rbac.utils import get_default_ou
27 28

  
28 29

  
29 30
@pytest.fixture
......
214 215
                }
215 216
            ),
216 217
        )
218

  
219

  
220
def test_import_site_cmd_set_absent_ou_to_default(db, json_fixture):
221
    minimal_json_export = {'roles': [{'name': 'first'}, {'name': 'second'}]}
222

  
223
    management.call_command(
224
        'import_site', '-o', 'set-absent-ou-to-default', json_fixture(minimal_json_export)
225
    )
226
    assert Role.objects.get(name='first', ou=get_default_ou())
227
    assert Role.objects.get(name='second', ou=get_default_ou())
217
-