@@ -85,9 +85,9 @@ Suppose you have a `tasks` table with the following schema:
85
85
86
86
``` sql
87
87
CREATE TABLE tasks (
88
- id INTEGER PRIMARY KEY ,
88
+ id TEXT PRIMARY KEY NOT NULL ,
89
+ user_id TEXT ,
89
90
title TEXT ,
90
- owner_id INTEGER ,
91
91
status TEXT
92
92
);
93
93
```
@@ -98,39 +98,46 @@ Here are a few examples of RLS policies you can create:
98
98
99
99
``` sql
100
100
-- SELECT policy
101
- owner_id = auth_userid()
101
+ user_id = auth_userid()
102
102
```
103
103
104
104
** 2. Users can only insert tasks for themselves.**
105
105
106
106
``` sql
107
107
-- INSERT policy
108
- NEW .owner_id = auth_userid()
108
+ NEW .user_id = auth_userid()
109
109
```
110
110
111
111
** 3. Users can only update the status of their own tasks.**
112
112
113
113
``` sql
114
114
-- UPDATE policy
115
- OLD .owner_id = auth_userid()
115
+ OLD .user_id = auth_userid()
116
116
```
117
117
118
- ** 4. Users with the 'admin' group can see all tasks.**
118
+ ** 4. Users can only delete their own tasks.**
119
+
120
+ ``` sql
121
+ -- DELETE policy
122
+ OLD .user_id = auth_userid()
123
+ ```
124
+
125
+ ** 5. Users with the 'admin' group can see all tasks.**
119
126
120
127
``` sql
121
128
-- SELECT policy
122
129
json_extract(auth_json(), ' $.attributes.group' ) = ' admin'
123
130
```
124
131
125
- ** 5 . Role-Based Access within a Tenancy**
132
+ ** 6 . Role-Based Access within a Tenancy**
126
133
127
134
``` sql
128
135
-- SELECT policy
129
136
org_id = json_extract(auth_json(), ' $.attributes.org_id' ) AND
130
- (json_extract(auth_json(), ' $.attributes.role' ) = ' admin' OR owner_id = auth_userid())
137
+ (json_extract(auth_json(), ' $.attributes.role' ) = ' admin' OR user_id = auth_userid())
131
138
```
132
139
133
- ** 6 . Access via a Membership Linking Table**
140
+ ** 7 . Access via a Membership Linking Table**
134
141
135
142
``` sql
136
143
-- SELECT policy
@@ -141,11 +148,11 @@ EXISTS (
141
148
)
142
149
```
143
150
144
- ** 7 . Public vs. Private Record Visibility**
151
+ ** 8 . Public vs. Private Record Visibility**
145
152
146
153
``` sql
147
154
-- SELECT policy
148
- visibility = ' public' OR owner_id = auth_userid()
155
+ visibility = ' public' OR user_id = auth_userid()
149
156
```
150
157
151
158
With these policies, when a user executes a query, SQLite Cloud will automatically enforce the defined RLS rules, ensuring data security and compliance.
0 commit comments