more modular management of permission codenames

This commit is contained in:
Alex Barcelo 2015-12-16 17:05:25 +01:00
parent 830fcbe796
commit 92d8ca3eff
2 changed files with 35 additions and 17 deletions

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import migrations
from django.conf import settings
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _
@ -24,14 +25,20 @@ def create_and_assign_permissions(apps, schema_editor):
Queue = apps.get_model('helpdesk', 'Queue')
for q in Queue.objects.all():
# Prepare the permission associated to this Queue
basename = "queue_access_%s" % q.slug
q.permission_name = "helpdesk.%s" % basename
Permission.objects.create(
name=_("Permission for queue: ") + q.title,
content_type=ContentType.objects.get(model="queue"),
codename=basename,
)
if not q.permission_name:
basename = q.prepare_permission_name
else:
basename = q.permission_name[9:]
try:
Permission.objects.create(
name=_("Permission for queue: ") + q.title,
content_type=ContentType.objects.get(model="queue"),
codename=basename,
)
except IntegrityError:
# Seems that it already existed, safely ignore it
pass
q.save()
# Second step: map the permissions according to QueueMembership

View File

@ -240,6 +240,15 @@ class Queue(models.Model):
return u'%s <%s>' % (self.title, self.email_address)
from_address = property(_from_address)
def prepare_permission_name(self):
"""Prepare internally the codename for the permission and store it in permission_name.
:return: The codename that can be used to create a new Permission object.
"""
# Prepare the permission associated to this Queue
basename = "queue_access_%s" % self.slug
self.permission_name = "helpdesk.%s" % basename
return basename
def save(self, *args, **kwargs):
if self.email_box_type == 'imap' and not self.email_box_imap_folder:
self.email_box_imap_folder = 'INBOX'
@ -263,15 +272,17 @@ class Queue(models.Model):
elif self.email_box_type == 'pop3' and not self.email_box_ssl:
self.email_box_port = 110
if not self.id and helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION:
# Prepare the permission associated to this Queue
basename = "queue_access_%s" % self.slug
self.permission_name = "helpdesk.%s" % basename
Permission.objects.create(
name=_("Permission for queue: ") + self.title,
content_type=ContentType.objects.get(model="queue"),
codename=basename,
)
if not self.id:
# Always prepare the permission codename
basename = self.prepare_permission_name()
# Create the permission only if the flag is active
if helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION:
Permission.objects.create(
name=_("Permission for queue: ") + self.title,
content_type=ContentType.objects.get(model="queue"),
codename=basename,
)
super(Queue, self).save(*args, **kwargs)