|
1 | 1 | --- |
2 | | -title: Defining Roles |
| 2 | +title: Defining Application Roles |
3 | 3 | --- |
4 | 4 |
|
5 | | -In addition to [defining a database schema](./defining-schemas), you can also define roles in your application. Roles are a way to group permissions together and assign them to users as part of Harper's [role based access control](../security/users-and-roles). An application component may declare roles that should exist for the application in a roles configuration file. To use this, first specify your roles config file in the `config.yaml` in your application directory: |
| 5 | +# Defining Application Roles |
| 6 | + |
| 7 | +Applications are more than just tables and endpoints — they need access rules. Harper lets you define roles directly in your application so you can control who can do what, without leaving your codebase. |
| 8 | + |
| 9 | +Let’s walk through creating a role, assigning it, and seeing it in action. |
| 10 | + |
| 11 | +## Step 1: Declare a Role |
| 12 | + |
| 13 | +First, point Harper to a roles configuration file. Add this to your `config.yaml`: |
6 | 14 |
|
7 | 15 | ```yaml |
8 | 16 | roles: |
9 | 17 | files: roles.yaml |
10 | 18 | ``` |
11 | 19 |
|
12 | | -Now you can create a roles.yaml in your application directory: |
| 20 | +Then create a simple `roles.yaml` in your application directory. For example, here’s a role that can only read and insert data into the `Dog` table: |
13 | 21 |
|
14 | 22 | ```yaml |
15 | | -declared-role: |
16 | | - super_user: false # This is a boolean value that indicates if the role is a super user or not |
17 | | - # Now we can grant the permissions to databases, here we grant permissions to the default data database |
18 | | - data: # This is the same structure as role object that is used in the roles operations APIs |
19 | | - TableOne: |
| 23 | +dog-reader: |
| 24 | + super_user: false |
| 25 | + data: |
| 26 | + Dog: |
20 | 27 | read: true |
21 | 28 | insert: true |
22 | | - TableTwo: |
23 | | - read: true |
24 | | - insert: false |
25 | | - update: true |
26 | | - delete: true |
27 | | - attributes: |
28 | | - name: |
29 | | - read: true |
30 | | - insert: false |
31 | | - update: true |
32 | 29 | ``` |
33 | 30 |
|
34 | | -With this in place, where Harper starts up, it will create the roles in the roles.yaml file if they do not already exist. If they do exist, it will update the roles with the new permissions. This allows you to manage your roles in your application code and have them automatically created or updated when the application starts. |
| 31 | +When Harper starts up, it will create this role (or update it if it already exists). |
35 | 32 |
|
36 | | -The structure of the roles.yaml file is: |
| 33 | +## Step 2: Create a User for the Role |
37 | 34 |
|
38 | | -```yaml |
39 | | -<role-name>: |
40 | | - permission: # contains the permissions for the role, this structure is optional, and you can place flags like super_user here as a shortcut |
41 | | - super_user: <boolean> |
42 | | - <database-name>: # each database with permissions can be added as named properties on the role |
43 | | - tables: # this structure is optional, and table names can be placed directly under the database as a shortcut |
44 | | - <table-name>: |
45 | | - read: <boolean> # indicates if the role has read permission to this table |
46 | | - insert: <boolean> # indicates if the role has insert permission to this table |
47 | | - update: <boolean> # indicates if the role has update permission to this table |
48 | | - delete: <boolean> # indicates if the role has delete permission to this table |
49 | | - attributes: |
50 | | - <attribute-name>: # individual attributes can have permissions as well |
51 | | - read: <boolean> |
52 | | - insert: <boolean> |
53 | | - update: <boolean> |
| 35 | +Next, create a non-super_user user and assign them this role. You can do this with the [Users and Roles API](../security/users-and-roles) (requires a super_user to run): |
| 36 | + |
| 37 | +```bash |
| 38 | +curl -u admin:password -X POST http://localhost:9926 \ |
| 39 | + -H "Content-Type: application/json" \ |
| 40 | + -d '{ |
| 41 | + "operation": "add_user", |
| 42 | + "username": "alice", |
| 43 | + "password": "password", |
| 44 | + "role": "dog_reader" |
| 45 | + }' |
54 | 46 | ``` |
| 47 | + |
| 48 | +Now you have a user named `alice` with the `dog_reader` role. |
| 49 | + |
| 50 | +## Step 3: Make Requests as Different Users |
| 51 | + |
| 52 | +Authenticate requests as `alice` to see how her role works: |
| 53 | + |
| 54 | +```bash |
| 55 | +# allowed (insert, role permits insert) |
| 56 | +curl -u alice:password -X POST http://localhost:9926/Dog/ \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d '{"name": "Buddy", "breed": "Husky"}' |
| 59 | +
|
| 60 | +# not allowed (delete, role does not permit delete) |
| 61 | +curl -u alice:password -X DELETE http://localhost:9926/Dog/1 |
| 62 | +``` |
| 63 | + |
| 64 | +The first request succeeds with a `200 OK`. The second fails with a `403 Forbidden`. |
| 65 | + |
| 66 | +Now compare with a super_user: |
| 67 | + |
| 68 | +```bash |
| 69 | +# super_user can delete |
| 70 | +curl -u admin:password -X DELETE http://localhost:9926/Dog/1 |
| 71 | +``` |
| 72 | + |
| 73 | +This succeeds because the super_user role has full permissions. |
| 74 | + |
| 75 | +## Where to Go Next |
| 76 | + |
| 77 | +This page gave you the basics - declare a role, assign it, and see it work. |
| 78 | + |
| 79 | +For more advanced scenarios, including: |
| 80 | + |
| 81 | +- defining multiple databases per role, |
| 82 | +- granting fine-grained attribute-level permissions, |
| 83 | +- and the complete structure of `roles.yaml`, |
| 84 | + |
| 85 | +see the [Roles Reference](../../reference/Applications/defining-roles). |
0 commit comments