|
| 1 | +--- |
| 2 | +title: 'Feature: Role-Based Access Control (RBAC)' |
| 3 | +description: An overview of the server's permission system for securing API endpoints and actions. |
| 4 | +--- |
| 5 | + |
| 6 | +The API server implements a granular Role-Based Access Control (RBAC) system to ensure that users can only access the data and perform the actions appropriate for their level of authorization. |
| 7 | + |
| 8 | +The system is built around two key concepts: **Roles** and **Permissions**. |
| 9 | + |
| 10 | +### Dual-Role System |
| 11 | + |
| 12 | +Each user in the system has two distinct roles, which combine to define their total permissions: |
| 13 | + |
| 14 | +1. **`appRole`**: This role governs a user's permissions within the mobile application. The possible values are: |
| 15 | + * `guestUser`: For anonymous users with limited access. |
| 16 | + * `standardUser`: For authenticated users with regular access. |
| 17 | + * `premiumUser`: For subscribed users with access to all app features. |
| 18 | + |
| 19 | +2. **`dashboardRole`**: This role governs a user's permissions for administrative and content management tasks, primarily through the web dashboard. The possible values are: |
| 20 | + * `none`: The default for regular app users with no dashboard access. |
| 21 | + * `publisher`: Can manage content like headlines and sources. |
| 22 | + * `admin`: Has full control over all system data and settings. |
| 23 | + |
| 24 | +### Permissions |
| 25 | + |
| 26 | +Permissions are specific action strings that follow a `resource.action` format (e.g., `headline.create`, `user.read_owned`). A user's combined roles grant them a set of these permissions. |
| 27 | + |
| 28 | +The `PermissionService` is the core component that checks if a user has a required permission before allowing an action to proceed. |
| 29 | + |
| 30 | +### How It Works in Practice |
| 31 | + |
| 32 | +When a user makes an API request, the following happens: |
| 33 | + |
| 34 | +1. The **Authentication Middleware** verifies the user's JWT and identifies the user. |
| 35 | +2. The **Authorization Middleware** determines what permission is required for the requested action (e.g., `headline.create` for a `POST` to `?model=headline`). |
| 36 | +3. It then asks the `PermissionService`: "Does this user have the `headline.create` permission?" |
| 37 | +4. The `PermissionService` checks the user's `appRole` and `dashboardRole`, looks up the permissions for those roles in the `role_permissions.dart` map, and returns `true` or `false`. |
| 38 | +5. If the check passes, the request continues. If it fails, the server returns a `403 Forbidden` error. |
| 39 | + |
| 40 | +This system provides a secure and centralized way to manage access control across the entire API. |
0 commit comments