Skip to content

Commit

Permalink
chore: add roles and permissions scripts
Browse files Browse the repository at this point in the history
Signed-off-by: maxwellgithinji <maxwellgithinji@gmail.com>
  • Loading branch information
maxwellgithinji committed Feb 18, 2022
1 parent 762f4dd commit 1db699e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
38 changes: 38 additions & 0 deletions data/authority.json
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.
35 changes: 35 additions & 0 deletions mycarehub/authority/management/commands/load_authority.py
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 mycarehub/authority/management/tests/test_load_authority.py
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()

0 comments on commit 1db699e

Please sign in to comment.