Skip to content

Commit

Permalink
Add dump_view_permissions management command
Browse files Browse the repository at this point in the history
  • Loading branch information
kluchrj committed Jan 9, 2024
1 parent 6a26e0f commit 356a831
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
max-parallel: 5
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: '3.10'
python-version: '3.12'

- name: Get pip cache dir
id: pip-cache
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

v1.2.0 (Released 1/9/2024)
--------------------------

- Added ``dump_view_permissions`` management command.
- Added Python 3.12 and Django 5.0 to the test matrix. Removed unsupported Python 3.7.


v1.1.0 (Released 4/12/2023)
---------------------------

Expand Down
4 changes: 2 additions & 2 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Installation

Requirements:

* Django 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2
* Python 3.6, 3.7, 3.8, 3.9, 3.10, 3.11
* Django 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2, 5.0
* Python 3.8, 3.9, 3.10, 3.11, 3.12


To install::
Expand Down
28 changes: 27 additions & 1 deletion docs/management_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,35 @@ that exist within the database. Useful for catching typos when specifying permis


Example Usage
-------------
^^^^^^^^^^^^^

::

$ python manage.py check_view_permissions



dump_view_permissions
----------------------

:synopsis: Dumps all detected view permissions to the specified output format.

If no parameters are provided, outputs JSON formatted results to ``stdout``.

Supports ``csv`` and ``json`` formats using ``--format`` or ``-f`` parameter.
Output to a file using ``--output`` or ``-o``, similar to Django's ``dumpdata`` command.

Example Usage
^^^^^^^^^^^^^

::

$ python manage.py dump_view_permissions

$ python manage.py dump_view_permissions --format csv

$ python manage.py dump_view_permissions --format csv --output example_file.csv

$ python manage.py dump_view_permissions --format json

$ python manage.py dump_view_permissions --format csv --output example_file.json
2 changes: 1 addition & 1 deletion permissions_auditor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Permissions auditing for Django."

__version__ = '1.1.0'
__version__ = '1.2.0'

# This can be removed once Django 3.1 and below support is dropped.
default_app_config = 'permissions_auditor.apps.PermissionsAuditorConfig'
52 changes: 52 additions & 0 deletions permissions_auditor/management/commands/dump_view_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import csv
import json

from django.core.management.base import BaseCommand

from permissions_auditor.core import get_views


class Command(BaseCommand):
help = 'Dumps all detected view permissions to the specified output format.'

def add_arguments(self, parser):
parser.add_argument(
"--format",
default="json",
help="Specifies the output serialization format for permissions. Options: csv, json",
)
parser.add_argument(
"-o", "--output", help="Specifies file to which the output is written."
)

def handle(self, *args, **options):
format = options["format"]
output = options["output"]
views = get_views()

if format == 'csv':
if output:
with open(output, 'w', newline='') as file:
writer = csv.writer(file, dialect='excel')
self._write_csv(views, writer)
else:
writer = csv.writer(self.stdout)
self._write_csv(views, writer)

elif format == 'json':
data = [v._asdict() for v in views]

if output:
with open(output, 'w') as file:
json.dump(data, file, indent=4)
else:
self.stdout.write(json.dumps(data))
else:
raise NotImplementedError('Output format `{}` is not implemented.'.format(format))

def _write_csv(self, views, writer):
# Header
writer.writerow(['module', 'name', 'url', 'permissions', 'login_required', 'docstring'])

for view in views:
writer.writerow(view)
24 changes: 24 additions & 0 deletions permissions_auditor/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,27 @@ def test_missing_perms(self):
call_command('check_view_permissions', stdout=out)
self.assertIn('`tests.test_perm`', out.getvalue())
self.assertIn('`tests.test_perm2`', out.getvalue())


class CheckDumpViewPermsTest(TestCase):

def test_dump_perms_no_args(self):
out = StringIO()
call_command('dump_view_permissions', stdout=out)

# Default output should be JSON formatted
self.assertIn('[{"module": "permissions_auditor.tests.fixtures.views"', out.getvalue())

def test_dump_perms_json(self):
out = StringIO()
call_command('dump_view_permissions', format='json', stdout=out)

# Output should be JSON formatted
self.assertIn('[{"module": "permissions_auditor.tests.fixtures.views"', out.getvalue())

def test_dump_perms_csv(self):
out = StringIO()
call_command('dump_view_permissions', format='csv', stdout=out)

# CSV output header should be present
self.assertIn('module,name,url,permissions,login_required,docstring', out.getvalue())
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Framework :: Django',
'Framework :: Django :: 2.2',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.1',
'Framework :: Django :: 4.2',
'Framework :: Django :: 5.0',
'Topic :: Internet :: WWW/HTTP :: Site Management',
'Topic :: Internet :: WWW/HTTP :: Site Management :: Link Checking',
],
Expand Down

0 comments on commit 356a831

Please sign in to comment.