-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from bgroff/feature/permissions
Feature: permissions
- Loading branch information
Showing
18 changed files
with
811 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
django_kala/organizations/forms/settings/manage_access.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from django import forms | ||
|
||
from auth.models import Permissions | ||
from django.contrib.auth.models import Permission | ||
|
||
|
||
def manage_access_forms(request, organization): | ||
add_organization_permission = Permission.objects.get(codename='add_organization') | ||
change_organization_permission = Permission.objects.get(codename='change_organization') | ||
delete_organization_permission = Permission.objects.get(codename='delete_organization') | ||
permissions = Permissions.objects.filter( | ||
object_uuid=organization.uuid | ||
).select_related( | ||
'permission', | ||
'user' | ||
) | ||
|
||
users = request.user.get_users() | ||
forms = [] | ||
for user in users: | ||
forms.append(ManageAccessForm( | ||
request.POST or None, | ||
organization=organization, | ||
user=user, | ||
add_organization=add_organization_permission, | ||
change_organization=change_organization_permission, | ||
delete_organization=delete_organization_permission, | ||
permissions=permissions, | ||
)) | ||
return forms | ||
|
||
|
||
class ManageAccessForm(forms.Form): | ||
def __init__(self, *args, **kwargs): | ||
|
||
self.add_organization_permission = kwargs.pop('add_organization') | ||
self.change_organization_permission = kwargs.pop('change_organization') | ||
self.delete_organization_permission = kwargs.pop('delete_organization') | ||
|
||
self.organization = kwargs.pop('organization') | ||
self.user = kwargs.pop('user') | ||
self.permissions_dict = {} | ||
for permission in kwargs.pop('permissions'): | ||
try: | ||
self.permissions_dict[permission.user.pk].append(permission.permission.codename) | ||
except KeyError: | ||
self.permissions_dict[permission.user.pk] = [permission.permission.codename] | ||
|
||
super(ManageAccessForm, self).__init__(*args, **kwargs) | ||
self.fields['add_organization_{0}'.format(self.user.pk)] = forms.BooleanField( | ||
required=False, | ||
label='', | ||
initial=True if 'add_organization' in self.permissions_dict.get(self.user.pk, []) else False, | ||
widget=forms.CheckboxInput( | ||
attrs={'pk': self.organization.pk} | ||
) | ||
) | ||
self.fields['change_organization_{0}'.format(self.user.pk)] = forms.BooleanField( | ||
required=False, | ||
label='', | ||
initial=True if 'change_organization' in self.permissions_dict.get(self.user.pk, []) else False, | ||
widget=forms.CheckboxInput( | ||
attrs={'pk': self.organization.pk} | ||
) | ||
) | ||
self.fields['delete_organization_{0}'.format(self.user.pk)] = forms.BooleanField( | ||
required=False, | ||
label='', | ||
initial=True if 'delete_organization' in self.permissions_dict.get(self.user.pk, []) else False, | ||
widget=forms.CheckboxInput( | ||
attrs={'pk': self.organization.pk} | ||
) | ||
) | ||
|
||
def save(self): | ||
# TODO, this can be sped up by using the permissions dict. | ||
if self.cleaned_data['add_organization_{0}'.format(self.user.pk)]: | ||
if 'add_organization' not in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.create( | ||
user=self.user, | ||
permission=self.add_organization_permission, | ||
object_uuid=self.organization.uuid | ||
) | ||
elif 'add_organization' in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.filter( | ||
user=self.user, | ||
permission=self.add_organization_permission, | ||
object_uuid=self.organization.uuid | ||
).delete() | ||
if self.cleaned_data['change_organization_{0}'.format(self.user.pk)]: | ||
if 'change_organization' not in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.create( | ||
user=self.user, | ||
permission=self.change_organization_permission, | ||
object_uuid=self.organization.uuid | ||
) | ||
elif 'change_organization' in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.filter( | ||
user=self.user, | ||
permission=self.change_organization_permission, | ||
object_uuid=self.organization.uuid | ||
).delete() | ||
if self.cleaned_data['delete_organization_{0}'.format(self.user.pk)]: | ||
if 'delete_organization' not in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.create( | ||
user=self.user, | ||
permission=self.delete_organization_permission, | ||
object_uuid=self.organization.uuid | ||
) | ||
elif 'delete_organization' in self.permissions_dict.get(self.user.pk, []): | ||
Permissions.objects.filter( | ||
user=self.user, | ||
permission=self.delete_organization_permission, | ||
object_uuid=self.organization.uuid | ||
).delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
django_kala/organizations/templates/organizations/settings/manage_access.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
{% extends 'base.html' %} | ||
{% load static %} | ||
{% load kala_tags %} | ||
|
||
{% block content %} | ||
<div class="repo ribbon"> | ||
<div class="ui container"> | ||
<div class="ui big breadcrumb"> | ||
<a class="section">{{ organization }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="ui tabular menu"> | ||
<div class="ui container"> | ||
<a class="item" href="{% url 'organizations:organization' organization.pk %}"> | ||
<i class="book icon"></i> | ||
Document | ||
</a> | ||
{# <a class="item">#} | ||
{# <i class="issue opened icon"></i>#} | ||
{# Issues#} | ||
{# <span class="ui small label">593</span>#} | ||
{# </a>#} | ||
<a class="active item" href="{% url 'organizations:details' organization.pk %}"> | ||
<i class="gear icon"></i> | ||
Settings | ||
</a> | ||
</div> | ||
</div> | ||
|
||
|
||
<div class="ui two column grid container"> | ||
<div class="four wide column"> | ||
<div class="ui vertical menu"> | ||
<div class="item"> | ||
<div class="header">Organization</div> | ||
<div class="menu"> | ||
<a class="item" href="{% url 'organizations:details' organization.pk %}">Details</a> | ||
<a class="item" href="">Archive</a> | ||
</div> | ||
</div> | ||
<div class="item"> | ||
<div class="header">People</div> | ||
<div class="menu"> | ||
<a class="active item" href="{% url 'organizations:manage_access' organization.pk %}">Manage access</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="twelve wide column"> | ||
<form id="form" class="ui form" method="post"> | ||
{% csrf_token %} | ||
<table class="ui very basic collapsing celled table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Create projects</th> | ||
<th>Manage organization</th> | ||
<th>Delete organization</th> | ||
</tr> | ||
</thead> | ||
{% for form in forms %} | ||
<tr> | ||
<td> | ||
<h4 class="ui header"> | ||
<div class="content"> | ||
{{ form.user }} | ||
</div> | ||
</h4> | ||
</td> | ||
<td> | ||
<div class="ui toggle checkbox"> | ||
{% for field in form %} | ||
{% if 'add_organization' in field.name %} | ||
{{ field }} | ||
{% endif %} | ||
{% endfor %} | ||
</div> | ||
</td> | ||
<td> | ||
<div class="ui toggle checkbox"> | ||
{% for field in form %} | ||
{% if 'change_organization' in field.name %} | ||
{{ field }} | ||
{% endif %} | ||
{% endfor %} | ||
</div> | ||
</td> | ||
<td> | ||
<div class="ui toggle checkbox"> | ||
{% for field in form %} | ||
{% if 'delete_organization' in field.name %} | ||
{{ field }} | ||
{% endif %} | ||
{% endfor %} | ||
</div> | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
<div class="field"></div> | ||
<button class="ui button primary" type="submit">Manage access</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
{% endblock content %} | ||
|
||
{% block scripts %} | ||
<script> | ||
{# $('.ui.styled.fluid.accordion')#} | ||
{# .accordion()#} | ||
{# ;#} | ||
$('.ui.checkbox') | ||
.checkbox() | ||
; | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .organizations import OrganizationsView | ||
from .new_organization import NewOrganizationView | ||
from .settings.details import DetailsView | ||
from .settings.manage_access import ManageAccessView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from django.contrib import messages | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.core.exceptions import PermissionDenied | ||
from django.shortcuts import get_object_or_404, redirect | ||
from django.urls import reverse | ||
from django.views.generic import TemplateView | ||
|
||
from organizations.forms.settings.manage_access import manage_access_forms | ||
from organizations.models import Organization | ||
|
||
|
||
class ManageAccessView(LoginRequiredMixin, TemplateView): | ||
template_name = 'organizations/settings/manage_access.html' | ||
|
||
def get_context_data(self, **kwargs): | ||
return { | ||
'forms': self.forms, | ||
'organization': self.organization, | ||
} | ||
|
||
def dispatch(self, request, pk, *args, **kwargs): | ||
self.organization = get_object_or_404( | ||
Organization.objects.active(), | ||
pk=pk | ||
) | ||
if not self.organization.has_change(request.user): | ||
raise PermissionDenied('You do not have permission to edit this project') | ||
|
||
self.forms = manage_access_forms(request, self.organization) | ||
return super(ManageAccessView, self).dispatch(request, *args, **kwargs) | ||
|
||
def post(self, request, *args, **kwargs): | ||
all_valid = True | ||
for form in self.forms: | ||
if form.is_valid(): | ||
form.save() | ||
else: | ||
all_valid = False | ||
if all_valid: | ||
messages.success(request, 'The permissions have been updated.') | ||
return redirect( | ||
reverse( | ||
'organizations:manage_access', | ||
args=[ | ||
self.organization.pk | ||
] | ||
) | ||
) | ||
return self.render_to_response(self.get_context_data()) |
Oops, something went wrong.