forked from kiwitcms/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 0
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 kiwitcms#1137
- Loading branch information
1 parent
108df51
commit aaa3040
Showing
6 changed files
with
141 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,113 @@ | ||
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 ' | ||
'(set by DEFAULT_GROUPS setting) and remove stale ones.' | ||
) | ||
|
||
@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, call_on_usercreate=False): | ||
""" | ||
Assigns default permissions for Kiwi TCMS apps to Tester group | ||
""" | ||
admin = Group.objects.get(name='Administrator') | ||
all_perms = Permission.objects.all() | ||
if call_on_usercreate and admin.permissions.count() == 0: | ||
admin.permissions.add(*all_perms) | ||
|
||
tester = Group.objects.get(name='Tester') | ||
if output: | ||
output.write('Adding permissions to Tester group\n') | ||
|
||
permissions_to_add = list() | ||
modify_tester = call_on_usercreate and tester.permissions.count() == 0 | ||
if modify_tester or not call_on_usercreate: | ||
permissions_to_add = Command.get_all_permissions().difference( | ||
Command.permissions_exclude()) | ||
for perm in permissions_to_add: | ||
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) | ||
if output: | ||
output.write('Done.\n') | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--noinput', '--no-input', action='store_false', | ||
dest='interactive', | ||
help='Automatic mode. Does not require user input', | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
output = None | ||
loglevel = 0 | ||
if kwargs['verbosity']: | ||
output = self.stdout | ||
loglevel = 2 | ||
|
||
call_command('update_permissions', '--verbosity=%i' % loglevel) | ||
|
||
# Assign permissions to Tester group | ||
self.assign_default_group_permissions(output=output) | ||
|
||
# Removing stale permissions | ||
if output: | ||
self.stdout.write('\nRemoving stale permissions\n') | ||
call_command('remove_stale_contenttypes', '--include-stale-apps', | ||
'--verbosity=%i' % loglevel, | ||
interactive=kwargs['interactive']) | ||
if output: | ||
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