-
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Management command to refresh permission objects.
Closes #1137
- Loading branch information
1 parent
e980a6f
commit b788a46
Showing
6 changed files
with
143 additions
and
28 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
7 changes: 7 additions & 0 deletions
7
docs/source/modules/tcms.core.management.commands.refresh_permissions.rst
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,7 @@ | ||
tcms.core.management.commands.refresh\_permissions module | ||
========================================================= | ||
|
||
.. automodule:: tcms.core.management.commands.refresh_permissions | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from django.apps import apps | ||
from django.contrib.auth.models import Group, Permission | ||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ('Refresh permissions for Tester group and remove ' | ||
'stale ones. Use -v 0 to suppress most of the ' | ||
'console output' | ||
) | ||
|
||
@staticmethod | ||
def get_all_permissions(): | ||
""" | ||
Returns a set of all available permissions in format | ||
app_label.perm_codename | ||
""" | ||
permissions = list() | ||
for perm in Permission.objects.all(): | ||
permissions.append("%s.%s" % | ||
(perm.content_type.app_label, perm.codename)) | ||
return set(permissions) | ||
|
||
@staticmethod | ||
def get_all_installed_apps(): | ||
""" | ||
Returns a set of currently installed applications as app_labels | ||
""" | ||
applications = list() | ||
for app_config in apps.get_app_configs(): | ||
applications.append(app_config.label) | ||
return set(applications) | ||
|
||
@staticmethod | ||
def kiwi_apps(): | ||
""" | ||
Returns a list of Kiwi TCMS related applications with permissions | ||
to be assigned to Tester group | ||
""" | ||
kiwi_apps = ['bugs', 'django_comments', 'linkreference', 'management', | ||
'testcases', 'testplans', 'testruns', 'attachments'] | ||
return kiwi_apps | ||
|
||
@staticmethod | ||
def permissions_exclude(): | ||
""" | ||
Returns a set of permissions NOT to be assigned to Tester group | ||
""" | ||
perm_exclude = {'attachments.delete_foreign_attachments', } | ||
return perm_exclude | ||
|
||
@staticmethod | ||
def assign_default_group_permissions(output=None, assign_if_none=False): | ||
""" | ||
Assigns default permissions for Kiwi TCMS apps to Tester group | ||
""" | ||
if assign_if_none: | ||
admin = Group.objects.get(name='Administrator') | ||
if admin.permissions.count() == 0: | ||
all_perms = Permission.objects.all() | ||
admin.permissions.add(*all_perms) | ||
|
||
tester = Group.objects.get(name='Tester') | ||
|
||
for perm in Command.get_all_permissions().difference( | ||
Command.permissions_exclude()): | ||
app, permission = perm.split('.') | ||
if app in Command.kiwi_apps(): | ||
perm_obj = Permission.objects.filter(content_type__app_label=app, | ||
codename=permission) | ||
if tester.permissions.filter(content_type__app_label=app, | ||
codename=permission).count() != 0: | ||
if output: | ||
output.write('Tester already has %s \n' % perm) | ||
else: | ||
tester.permissions.add(*perm_obj) | ||
if output: | ||
output.write('%s added' % perm) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--auto', '--noinput', '--no-input', action='store_true', | ||
dest='noinput', | ||
help='Automatic mode. Does not require user confirmation', | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
output = None | ||
loglevel = 0 | ||
if kwargs['verbosity']: | ||
output = self.stdout | ||
loglevel = 2 | ||
call_command('update_permissions', '--verbosity=%i' % loglevel) | ||
|
||
# Adding permissions to Tester group | ||
self.stdout.write('Adding permissions to Tester group') | ||
self.assign_default_group_permissions(output=output) | ||
self.stdout.write('Done.') | ||
|
||
# Removing stale permissions | ||
self.stdout.write('\nRemoving stale permissions') | ||
for perm in self.get_all_permissions(): | ||
app, permission = perm.split('.') | ||
if app not in self.get_all_installed_apps(): | ||
confirm = '' | ||
if not kwargs['noinput']: | ||
confirm = input('%s seems stale. Delete? y/N: ' % perm) # nosec | ||
if confirm.lower() == 'y' or kwargs['noinput']: | ||
perm_obj = Permission.objects.filter(content_type__app_label=app, | ||
codename=permission) | ||
perm_obj.delete() | ||
if kwargs['verbosity']: | ||
self.stdout.write('%s removed\n' % perm) | ||
self.stdout.write('Done.\n') |
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