0001-manager-use-selected-ou-by-default-in-import-roles-f.patch
src/authentic2/manager/role_views.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import json |
18 | 18 | |
19 | 19 |
from django.core.exceptions import PermissionDenied, ValidationError |
20 | 20 |
from django.utils.translation import ugettext_lazy as _ |
21 | 21 |
from django.urls import reverse |
22 | 22 |
from django.views.generic import FormView, TemplateView |
23 | 23 |
from django.views.generic.detail import SingleObjectMixin |
24 |
from django.views.generic.edit import FormMixin |
|
24 | 25 |
from django.contrib import messages |
25 | 26 |
from django.contrib.contenttypes.models import ContentType |
26 | 27 |
from django.db import transaction |
27 | 28 |
from django.db.models.query import Q, Prefetch |
28 | 29 |
from django.db.models import Count, F |
29 | 30 |
from django.contrib.auth import get_user_model |
30 | 31 | |
31 | 32 |
from django_rbac.utils import get_role_model, get_permission_model, get_ou_model |
... | ... | |
509 | 510 |
hooks.call_hooks('event', name='remove-remove-admin-role-user', user=self.request.user, |
510 | 511 |
role=self.object, admin=self.user) |
511 | 512 |
return redirect(self.request, self.success_url) |
512 | 513 | |
513 | 514 |
remove_admin_user = RoleRemoveAdminUserView.as_view() |
514 | 515 | |
515 | 516 | |
516 | 517 |
class RolesImportView(views.PermissionMixin, views.TitleMixin, views.MediaMixin, views.FormNeedsRequest, |
517 |
FormView): |
|
518 |
FormView, FormMixin):
|
|
518 | 519 |
form_class = forms.RolesImportForm |
519 | 520 |
model = get_role_model() |
520 | 521 |
template_name = 'authentic2/manager/import_form.html' |
521 | 522 |
title = _('Roles Import') |
522 | 523 | |
524 |
def get_initial(self): |
|
525 |
result = super().get_initial() |
|
526 |
selected_ou = self.request.GET.get('search-ou') |
|
527 |
if selected_ou: |
|
528 |
result.update({'ou': selected_ou}) |
|
529 |
return result |
|
530 | ||
523 | 531 |
def post(self, request, *args, **kwargs): |
524 | 532 |
if not self.can_add: |
525 | 533 |
raise PermissionDenied |
526 | 534 |
return super().post(request, *args, **kwargs) |
527 | 535 | |
528 | 536 |
def form_valid(self, form): |
529 | 537 |
self.ou = form.cleaned_data['ou'] |
530 | 538 |
try: |
src/authentic2/manager/templates/authentic2/manager/roles.html | ||
---|---|---|
10 | 10 |
{% if view.can_add %} |
11 | 11 |
<a href="{% url "a2-manager-role-add" %}" rel="popup">{% trans "Add role" %}</a> |
12 | 12 |
{% else %} |
13 | 13 |
<a href="#" class="disabled" rel="popup">{% trans "Add role" %}</a> |
14 | 14 |
{% endif %} |
15 | 15 |
<ul class="extra-actions-menu"> |
16 | 16 |
<li><a download href="{% url 'a2-manager-roles-export' format="json" %}?{{ request.GET.urlencode }}">{% trans 'Export' %}</a></li> |
17 | 17 |
{% if view.can_add %} |
18 |
<li><a href="{% url 'a2-manager-roles-import' %}" rel="popup">{% trans 'Import' %}</a></li> |
|
18 |
<li><a href="{% url 'a2-manager-roles-import' %}?{{ request.GET.urlencode }}" rel="popup">{% trans 'Import' %}</a></li>
|
|
19 | 19 |
{% endif %} |
20 | 20 |
</ul> |
21 | 21 |
</span> |
22 | 22 |
{% endblock %} |
23 | 23 | |
24 | 24 |
{% block sidebar %} |
25 | 25 |
<aside id="sidebar"> |
26 | 26 |
{% include "authentic2/manager/search_form.html" %} |
tests/test_role_manager.py | ||
---|---|---|
156 | 156 |
assert Role.objects.filter(ou=ou1).count() == 4 |
157 | 157 | |
158 | 158 |
# in case ous are present in export file, they must not be imported |
159 | 159 |
export['ous'] = [{ |
160 | 160 |
"uuid": "27255f404cb140df9a577da76b59f285", |
161 | 161 |
"slug": "should_not_exist", |
162 | 162 |
"name": "should_not_exist", |
163 | 163 |
}] |
164 |
resp = app.get('/manage/roles/') # unselect ou1 |
|
164 | 165 |
resp = resp.click('Import') |
165 | 166 |
resp.form['site_json'] = Upload('export.json', json.dumps(export).encode(), 'application/json') |
166 | 167 |
resp = resp.form.submit().follow() |
167 | 168 | |
168 | 169 |
assert not OrganizationalUnit.objects.filter(slug="should_not_exist").exists() |
169 | 170 | |
170 | 171 | |
171 | 172 |
def test_manager_role_import_single_ou(app, admin, simple_role): |
... | ... | |
183 | 184 |
assert not 'Organizational unit' in resp.text |
184 | 185 |
assert resp.form['ou'].attrs['type'] == 'hidden' |
185 | 186 | |
186 | 187 |
resp.form['site_json'] = Upload('export.json', json.dumps(export).encode(), 'application/json') |
187 | 188 |
resp = resp.form.submit().follow() |
188 | 189 | |
189 | 190 |
imported_role = Role.objects.get(slug=simple_role.slug) |
190 | 191 |
assert imported_role.ou == simple_role.ou |
192 | ||
193 | ||
194 |
def test_manager_role_import_selected_ou(app, admin, ou1, ou2): |
|
195 |
response = login(app, admin, 'a2-manager-roles') |
|
196 |
response.form.set('search-ou', ou2.pk) |
|
197 |
response = response.form.submit() |
|
198 |
response = response.click('Import') |
|
199 |
assert response.pyquery.find('select#id_ou option[selected]')[0].text == 'OU2' |
|
191 |
- |