~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/scripts/import_permissions_and_roles.py -rwxr-xr-x 1.4 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python

"""Import permissions, roles, and their relations from a JSON file.

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

import json

import click

from byceps.services.authorization import service as authz_service
from byceps.util.system import get_config_filename_from_env_or_exit

from _util import app_context


@click.command()
@click.argument('json_file', type=click.File())
def execute(json_file):
    data = json.load(json_file)

    permissions = data['permissions']
    roles = data['roles']

    click.echo(f'Importing {len(permissions)} permissions ... ', nl=False)
    create_permissions(permissions)
    click.secho('done.', fg='green')

    click.echo(f'Importing {len(roles)} roles ... ', nl=False)
    create_roles(roles)
    click.secho('done.', fg='green')


def create_permissions(permissions):
    for permission in permissions:
        authz_service.create_permission(permission['id'], permission['title'])


def create_roles(roles):
    for role in roles:
        role_id = role['id']

        authz_service.create_role(role_id, role['title'])

        for permission_id in role['assigned_permissions']:
            authz_service.assign_permission_to_role(permission_id, role_id)


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()