|
| 1 | +"""Permission commands: list, check. |
| 2 | +
|
| 3 | +Handles permission discovery and verification operations. |
| 4 | +""" |
| 5 | + |
| 6 | +import sys |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | +from unityauth_cli.cli import ( |
| 11 | + CLIContext, |
| 12 | + console, |
| 13 | + error, |
| 14 | + handle_error, |
| 15 | + info, |
| 16 | + pass_context, |
| 17 | + require_auth, |
| 18 | + success, |
| 19 | + warning, |
| 20 | +) |
| 21 | +from unityauth_cli.client import UnityAuthAPIClient |
| 22 | +from unityauth_cli.formatters.table import format_table |
| 23 | +from unityauth_cli.formatters.json_fmt import format_json |
| 24 | +from unityauth_cli.formatters.csv_fmt import format_csv |
| 25 | +from unityauth_cli.utils.errors import AuthorizationError, ValidationError |
| 26 | + |
| 27 | + |
| 28 | +@click.command('list') |
| 29 | +@click.option('--tenant-id', required=True, type=int, help='Tenant ID to check permissions for') |
| 30 | +@click.option('--service-id', required=True, type=int, help='Service ID to check permissions for') |
| 31 | +@pass_context |
| 32 | +@require_auth |
| 33 | +def list_permissions( |
| 34 | + ctx: CLIContext, |
| 35 | + tenant_id: int, |
| 36 | + service_id: int, |
| 37 | + client: UnityAuthAPIClient, |
| 38 | +) -> None: |
| 39 | + """List your permissions for a tenant and service. |
| 40 | +
|
| 41 | + Returns all permissions the authenticated user has for the specified |
| 42 | + tenant and service combination. |
| 43 | +
|
| 44 | + \b |
| 45 | + Examples: |
| 46 | + unityauth permissions list --tenant-id 1 --service-id 1 |
| 47 | + unityauth permissions list --tenant-id 1 --service-id 1 --format json |
| 48 | + """ |
| 49 | + try: |
| 50 | + # Validate IDs |
| 51 | + if tenant_id <= 0: |
| 52 | + raise ValidationError("Tenant ID must be a positive integer") |
| 53 | + if service_id <= 0: |
| 54 | + raise ValidationError("Service ID must be a positive integer") |
| 55 | + |
| 56 | + if ctx.verbose: |
| 57 | + info(f"Fetching permissions for tenant {tenant_id}, service {service_id}...") |
| 58 | + |
| 59 | + # Build request payload |
| 60 | + payload = { |
| 61 | + 'tenantId': tenant_id, |
| 62 | + 'serviceId': service_id, |
| 63 | + } |
| 64 | + |
| 65 | + # Make POST request to get permissions |
| 66 | + response = client.post('/api/principal/permissions', data=payload) |
| 67 | + |
| 68 | + # Handle error response (Failure case) |
| 69 | + if response and 'errorMessage' in response: |
| 70 | + error(f"Failed to get permissions: {response['errorMessage']}") |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + # Handle success response |
| 74 | + permissions = response.get('permissions', []) if response else [] |
| 75 | + |
| 76 | + if not permissions: |
| 77 | + warning("No permissions found for this tenant/service combination") |
| 78 | + return |
| 79 | + |
| 80 | + # Format and display output based on format option |
| 81 | + if ctx.output_format == 'json': |
| 82 | + console.print(format_json({'permissions': permissions})) |
| 83 | + elif ctx.output_format == 'csv': |
| 84 | + # For CSV, create a simple list format |
| 85 | + headers = ['permission'] |
| 86 | + rows = [{'permission': perm} for perm in permissions] |
| 87 | + console.print(format_csv(rows, headers)) |
| 88 | + else: |
| 89 | + # Table format (default) |
| 90 | + headers = ['Permission'] |
| 91 | + rows = [[perm] for perm in permissions] |
| 92 | + console.print(format_table(rows, headers)) |
| 93 | + |
| 94 | + if ctx.verbose: |
| 95 | + info(f"Total permissions: {len(permissions)}") |
| 96 | + |
| 97 | + except AuthorizationError as e: |
| 98 | + error( |
| 99 | + str(e), |
| 100 | + "You may not have access to this tenant/service combination.\n" |
| 101 | + "Contact your administrator for access." |
| 102 | + ) |
| 103 | + sys.exit(3) |
| 104 | + except Exception as e: |
| 105 | + handle_error(e) |
0 commit comments