-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dump_view_permissions management command
- Loading branch information
Showing
8 changed files
with
117 additions
and
6 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
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
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,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
52
permissions_auditor/management/commands/dump_view_permissions.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,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) |
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