group permission in control panel for better overview
This commit is contained in:
parent
7f06973b55
commit
ee2ff8f631
4 changed files with 154 additions and 48 deletions
|
@ -57,39 +57,97 @@
|
|||
{% endif %}
|
||||
|
||||
<h4>{% trans 'Access Permissions' %}</h4>
|
||||
{% trans 'Add' as button_label %}
|
||||
{% include 'control/fragment_access_permissions_form.html' with button_label=button_label %}
|
||||
{% if user.accesspermissions.all %}
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>{% trans 'Access Restriction' %}</th>
|
||||
<th>{% trans 'author' %}</th>
|
||||
<th>{% trans 'expires' %}</th>
|
||||
<th>{% trans 'can grant' %}</th>
|
||||
<th>{% trans 'key' %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
{% for access_permission in user.accesspermissions.all %}
|
||||
{% if access_restriction %}
|
||||
<p>
|
||||
{% with restriction_title=access_restriction.title %}
|
||||
{% blocktrans %}for {{ restriction_title }}{% endblocktrans %}
|
||||
{% endwith %} –
|
||||
<a href="?">« {% trans 'back' %}</a>
|
||||
</p>
|
||||
{% if access_permissions %}
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
<tr>
|
||||
<td>{{ access_permission.access_restriction.title }}</td>
|
||||
<td>
|
||||
{% if access_permission.author %}
|
||||
<a href="{% url 'control.users.detail' user=access_permission.author_id %}">{{ access_permission.author.username }}</a>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{% if access_permission.expire_date %}{{ access_permission.expire_date }}{% else %}{% trans 'never' %}{% endif %}</td>
|
||||
<td>{% if access_permission.can_grant %}{% trans 'Yes' %}{% else %}{% trans 'No' %}{% endif %}</td>
|
||||
<td>{% if access_permission.unique_key %}{{ access_permission.unique_key }}{% endif %}</td>
|
||||
{% if request.user_permissions.grant_all_access or request.user == access_permission.author %}
|
||||
<td class="button-cell"><button type="submit" name="delete_access_permission" value="{{ access_permission.pk }}">{% trans 'Delete' %}</button></td>
|
||||
{% endif %}
|
||||
<th>{% trans 'author' %}</th>
|
||||
<th>{% trans 'expires' %}</th>
|
||||
<th>{% trans 'can grant' %}</th>
|
||||
<th>{% trans 'key' %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</form>
|
||||
{% for permission in user.accesspermissions.all %}
|
||||
<tr>
|
||||
<td>
|
||||
{% if permission.author %}
|
||||
<a href="{% url 'control.users.detail' user=permission.author_id %}">{{ permission.author.username }}</a>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
<span class="{% if permission.expired %}red{% else %}green{% endif %}">
|
||||
{% if permission.expire_date %}
|
||||
{{ permission.expire_date }}
|
||||
{% else %}
|
||||
{% trans 'never' %}
|
||||
{% endif %}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
{% if permission.can_grant %}
|
||||
<strong class="green">{% trans 'Yes' %}</strong>
|
||||
{% else %}
|
||||
{% trans 'No' %}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{% if permission.unique_key %}{{ permission.unique_key }}{% endif %}</td>
|
||||
{% if request.user_permissions.grant_all_access or request.user == permission.author %}
|
||||
<td class="button-cell"><button type="submit" name="delete_access_permission" value="{{ permission.pk }}">{% trans 'Delete' %}</button></td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</form>
|
||||
{% else %}
|
||||
<p><em>{% trans 'none' %}</em></p>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<p><em>{% trans 'none' %}</em></p>
|
||||
{% trans 'Add' as button_label %}
|
||||
{% include 'control/fragment_access_permissions_form.html' with button_label=button_label %}
|
||||
{% if access_permissions %}
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>{% trans 'Access Restriction' %}</th>
|
||||
<th>{% trans 'expires' %}</th>
|
||||
<th>{% trans 'can grant' %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
{% for permission in access_permissions %}
|
||||
<tr>
|
||||
<td>{{ permission.title }}</td>
|
||||
<td>
|
||||
<span class="{% if permission.expired %}red{% else %}green{% endif %}">
|
||||
{% if permission.expire_date %}
|
||||
{{ permission.expire_date }}
|
||||
{% else %}
|
||||
{% trans 'never' %}
|
||||
{% endif %}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
{% if permission.can_grant %}
|
||||
<strong class="green">{% trans 'Yes' %}</strong>
|
||||
{% else %}
|
||||
{% trans 'No' %}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td class="button-cell"><a class="button" href="?restriction={{ permission.pk }}">{% trans 'Details' %}</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</form>
|
||||
{% else %}
|
||||
<p><em>{% trans 'none' %}</em></p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
|
|
@ -12,12 +12,13 @@ from django.db import transaction
|
|||
from django.db.models import Prefetch
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.crypto import get_random_string
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from c3nav.control.forms import AccessPermissionForm, AnnouncementForm, UserPermissionsForm
|
||||
from c3nav.control.models import UserPermissions
|
||||
from c3nav.mapdata.models.access import AccessPermission, AccessPermissionToken
|
||||
from c3nav.mapdata.models.access import AccessPermission, AccessPermissionToken, AccessRestriction
|
||||
from c3nav.site.models import Announcement
|
||||
|
||||
|
||||
|
@ -140,18 +141,52 @@ def user_detail(request, user):
|
|||
})
|
||||
|
||||
# access permissions
|
||||
if request.method == 'POST' and request.POST.get('submit_access_permissions'):
|
||||
form = AccessPermissionForm(request=request, data=request.POST)
|
||||
if form.is_valid():
|
||||
form.get_token().redeem(user)
|
||||
messages.success(request, _('Access permissions successfully granted.'))
|
||||
return redirect(request.path_info)
|
||||
now = timezone.now()
|
||||
restriction = request.GET.get('restriction')
|
||||
if restriction and restriction.isdigit():
|
||||
restriction = get_object_or_404(AccessRestriction, pk=restriction)
|
||||
permissions = user.accesspermissions.filter(access_restriction=restriction).order_by('expire_date')
|
||||
for permission in permissions:
|
||||
permission.expired = permission.expire_date and permission.expire_date >= now
|
||||
ctx.update({
|
||||
'access_restriction': restriction,
|
||||
'access_permissions': user.accesspermissions.filter(
|
||||
access_restriction=restriction
|
||||
).order_by('expire_date')
|
||||
})
|
||||
else:
|
||||
form = AccessPermissionForm(request=request)
|
||||
if request.method == 'POST' and request.POST.get('submit_access_permissions'):
|
||||
form = AccessPermissionForm(request=request, data=request.POST)
|
||||
if form.is_valid():
|
||||
form.get_token().redeem(user)
|
||||
messages.success(request, _('Access permissions successfully granted.'))
|
||||
return redirect(request.path_info)
|
||||
else:
|
||||
form = AccessPermissionForm(request=request)
|
||||
|
||||
ctx.update({
|
||||
'access_permission_form': form
|
||||
})
|
||||
access_permissions = {}
|
||||
for permission in user.accesspermissions.select_related('access_restriction'):
|
||||
access_permissions.setdefault(permission.access_restriction_id, []).append(permission)
|
||||
access_permissions = tuple(
|
||||
{
|
||||
'pk': pk,
|
||||
'title': permissions[0].access_restriction.title,
|
||||
'can_grant': any(item.can_grant for item in permissions),
|
||||
'expire_date': set(item.expire_date for item in permissions),
|
||||
} for pk, permissions in access_permissions.items()
|
||||
)
|
||||
for permission in access_permissions:
|
||||
permission['expire_date'] = None if None in permission['expire_date'] else max(permission['expire_date'])
|
||||
permission['expired'] = permission['expire_date'] and permission['expire_date'] >= now
|
||||
access_permissions = tuple(sorted(
|
||||
access_permissions,
|
||||
key=lambda permission: (1, 0) if permission['expire_date'] is None else (0, permission['expire_date']),
|
||||
reverse=True
|
||||
))
|
||||
ctx.update({
|
||||
'access_permissions': access_permissions,
|
||||
'access_permission_form': form
|
||||
})
|
||||
|
||||
return render(request, 'control/user.html', ctx)
|
||||
|
||||
|
|
|
@ -151,6 +151,14 @@ class AccessPermission(models.Model):
|
|||
def user_access_permission_key(user_id):
|
||||
return 'mapdata:user_access_permission:%d' % user_id
|
||||
|
||||
@classmethod
|
||||
def queryset_for_user(cls, user, can_grant=None):
|
||||
return user.accesspermissions.filter(
|
||||
Q(expire_date__isnull=True) | Q(expire_date__gt=timezone.now())
|
||||
).filter(
|
||||
Q(can_grant=True) if can_grant is not None else Q()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_for_request_with_expire_date(cls, request, can_grant=None):
|
||||
if not request.user.is_authenticated:
|
||||
|
@ -159,11 +167,9 @@ class AccessPermission(models.Model):
|
|||
if request.user_permissions.grant_all_access:
|
||||
return {pk: None for pk in cls.get_all_access_restrictions()}
|
||||
|
||||
result = tuple(request.user.accesspermissions.filter(
|
||||
Q(expire_date__isnull=True) | Q(expire_date__gt=timezone.now())
|
||||
).filter(
|
||||
Q(can_grant=True) if can_grant is not None else Q()
|
||||
).values_list('access_restriction_id', 'expire_date'))
|
||||
result = tuple(
|
||||
cls.queryset_for_user(request.user, can_grant).values_list('access_restriction_id', 'expire_date')
|
||||
)
|
||||
|
||||
# collect permissions (can be multiple for one restriction)
|
||||
permissions = {}
|
||||
|
|
|
@ -865,7 +865,7 @@ main.control form tr > * {
|
|||
padding-bottom: 4px;
|
||||
text-align: right;
|
||||
}
|
||||
.button-cell button {
|
||||
.button-cell button, .button-cell .button {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
|
@ -915,3 +915,10 @@ main .narrow form button {
|
|||
button + button {
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
.red {
|
||||
color: #9c0900;
|
||||
}
|
||||
.green {
|
||||
color: #28b62c;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue