-
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.
chore: add roles and permissions scripts
Signed-off-by: maxwellgithinji <maxwellgithinji@gmail.com>
- Loading branch information
1 parent
762f4dd
commit 1db699e
Showing
5 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[ | ||
{ | ||
"role": "SYSTEM_ADMINISTRATOR", | ||
"permissions": [ | ||
"CAN_INVITE_USER", | ||
"CAN_RESET_USER_PASSWORD", | ||
"CAN_EDIT_USER_ROLE", | ||
"CAN_EDIT_OWN_ROLE", | ||
"CAN_TRIGGER_ANALYTICS_JOBS", | ||
"CAN_MANAGE_OPENMRS_INTEGRATION" | ||
] | ||
}, | ||
{ | ||
"role": "COMMUNITY_MANAGEMENT", | ||
"permissions": [ | ||
"CAN_CREATE_GROUP", | ||
"CAN_UPDATE_GROUP", | ||
"CAN_MODERATE_GROUP", | ||
"CAN_INVITE_CLIENT_TO_GROUP" | ||
] | ||
}, | ||
{ | ||
"role": "CONTENT_MANAGEMENT", | ||
"permissions": [ | ||
"CAN_CREATE_CONTENT_IN_CMS", | ||
"CAN_MANAGE_CONTENT" | ||
] | ||
}, | ||
{ | ||
"role": "CLIENT_MANAGEMENT", | ||
"permissions": [ | ||
"CAN_INVITE_CLIENT", | ||
"CAN_MANAGE_CLIENT", | ||
"CAN_MANAGE_SERVICE_REQUEST", | ||
"CAN_VIEW_CLIENT_HEALTH_RECORDS" | ||
] | ||
} | ||
] |
Empty file.
Empty file.
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,35 @@ | ||
import json | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
|
||
from mycarehub.authority.models import AuthorityPermission, AuthorityRole | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Loads the roles and permissions to the database" | ||
|
||
@transaction.atomic | ||
def handle(self, *args, **options): | ||
base_path = Path(__file__).parent.parent.parent.parent.parent.resolve() | ||
sys.path.append(str(base_path)) | ||
data_dir = os.path.join(base_path, "data") | ||
source_file_authority = os.path.join(data_dir, "authority.json") | ||
|
||
data_authority = json.load(open(file=source_file_authority)) | ||
count = len(data_authority) | ||
|
||
for role_permission in data_authority: | ||
role = role_permission["role"] | ||
r, created = AuthorityRole.objects.get_or_create( | ||
name=role, | ||
) | ||
for permission in role_permission["permissions"]: | ||
p, _ = AuthorityPermission.objects.get_or_create( | ||
name=permission, | ||
) | ||
r.permissions.add(p) | ||
print(f"role_permission: {role_permission}; Created: {created}; {count}") |
17 changes: 17 additions & 0 deletions
17
mycarehub/authority/management/tests/test_load_authority.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,17 @@ | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from mycarehub.authority.models import AuthorityPermission, AuthorityRole | ||
|
||
|
||
class CommandsTestCase(TestCase): | ||
def test_load_authority(self): | ||
"Test load roles and permissions" | ||
out = StringIO() | ||
assert not AuthorityPermission.objects.exists() | ||
assert not AuthorityRole.objects.exists() | ||
call_command("load_authority", stdout=out) | ||
assert AuthorityPermission.objects.exists() | ||
assert AuthorityRole.objects.exists() |