Skip to content

Commit cfb1051

Browse files
authored
Create roles page (#323)
* Create roles page * Address comments * Replicate across versions
1 parent e5ab4d2 commit cfb1051

File tree

8 files changed

+736
-144
lines changed

8 files changed

+736
-144
lines changed
Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,85 @@
11
---
2-
title: Defining Roles
2+
title: Defining Application Roles
33
---
44

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`:
614

715
```yaml
816
roles:
917
files: roles.yaml
1018
```
1119
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:
1321

1422
```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:
2027
read: true
2128
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
3229
```
3330

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).
3532

36-
The structure of the roles.yaml file is:
33+
## Step 2: Create a User for the Role
3734

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+
}'
5446
```
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).
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Defining Application Roles
3+
---
4+
5+
# Defining Application Roles
6+
7+
Roles in Harper are part of the application’s role-based access control (RBAC) system. You can declare roles in your application and manage their permissions through a roles configuration file. When the application starts, Harper will ensure all declared roles exist with the specified permissions, updating them if necessary.
8+
9+
## Configuring Roles
10+
11+
Point to a roles configuration file from your application’s `config.yaml`:
12+
13+
```yaml
14+
roles:
15+
files: roles.yaml
16+
```
17+
18+
You can declare one or more files. Each file should define one or more roles in YAML format.
19+
20+
## Roles File Structure
21+
22+
A roles file (`roles.yaml`) contains role definitions keyed by role name. Each role may contain:
23+
24+
- **super_user** – a boolean that grants all permissions.
25+
- **databases** – one or more databases the role has access to.
26+
- **tables** – within each database, table-level and attribute-level permissions.
27+
28+
**Full Example**
29+
30+
```yaml
31+
<role-name>:
32+
super_user: <boolean> # optional
33+
<database-name>:
34+
<table-name>:
35+
read: <boolean>
36+
insert: <boolean>
37+
update: <boolean>
38+
delete: <boolean>
39+
attributes:
40+
<attribute-name>:
41+
read: <boolean>
42+
insert: <boolean>
43+
update: <boolean>
44+
```
45+
46+
## Role Flags
47+
48+
- `super_user: true` — grants full system access.
49+
- `super_user: false` — the role only has the explicit permissions defined in the role.
50+
51+
## Database and Table Permissions
52+
53+
Within each role, you may specify one or more databases. Each database can declare permissions for tables.
54+
55+
Example:
56+
57+
```yaml
58+
analyst:
59+
super_user: false
60+
data:
61+
Sales:
62+
read: true
63+
insert: false
64+
update: false
65+
delete: false
66+
```
67+
68+
In this example, the `analyst` role has read-only access to the `Sales` table in the `data` database.
69+
70+
## Attribute-Level Permissions
71+
72+
You can also grant or deny access at the attribute level within a table.
73+
74+
Example:
75+
76+
```yaml
77+
editor:
78+
data:
79+
Articles:
80+
read: true
81+
insert: true
82+
update: true
83+
attributes:
84+
title:
85+
read: true
86+
update: true
87+
author:
88+
read: true
89+
update: false
90+
```
91+
92+
Here, the `editor` role can update the `title` of an article but cannot update the `author`.
93+
94+
## Multiple Roles
95+
96+
Roles can be defined side by side in a single file:
97+
98+
```yaml
99+
reader:
100+
super_user: false
101+
data:
102+
Dog:
103+
read: true
104+
105+
writer:
106+
super_user: false
107+
data:
108+
Dog:
109+
insert: true
110+
update: true
111+
```
112+
113+
## Behavior on Startup
114+
115+
- If a declared role does not exist, Harper creates it.
116+
- If a declared role already exists, Harper updates its permissions to match the definition.
117+
- Roles are enforced consistently across deployments, keeping access control in sync with your application code.
Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,85 @@
11
---
2-
title: Defining Roles
2+
title: Defining Application Roles
33
---
44

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`:
614

715
```yaml
816
roles:
917
files: roles.yaml
1018
```
1119
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:
1321

1422
```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:
2027
read: true
2128
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
3229
```
3330

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).
3532

36-
The structure of the roles.yaml file is:
33+
## Step 2: Create a User for the Role
3734

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+
}'
5446
```
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

Comments
 (0)