@@ -53,74 +53,232 @@ async fn main() -> Result<(), Error> {
5353
5454## Authorization API
5555
56- ### Authorization Checks
57-
5856``` rust
59- let vault = client . organization (" org_..." ). vault (" vlt_..." );
57+ let org = client . organization (" org_..." );
58+ let vault = org . vault (" vlt_..." );
59+ ```
60+
61+ ### Permission Checks
6062
61- // Simple check
63+ ``` rust
64+ // Simple check - returns bool
6265let allowed = vault . check (" user:alice" , " view" , " doc:1" ). await ? ;
6366
6467// With ABAC context
65- vault . check (" user:alice" , " view" , " doc:confidential" )
68+ let allowed = vault . check (" user:alice" , " view" , " doc:confidential" )
6669 . with_context (Context :: new ()
6770 . with (" ip_address" , " 10.0.0.50" )
6871 . with (" mfa_verified" , true ))
6972 . await ? ;
7073
74+ // Guard clause - returns Err(AccessDenied) if denied
75+ vault . check (" user:alice" , " edit" , " doc:1" ). require (). await ? ;
76+
7177// Batch checks - single round-trip
72- let results : Vec <bool > = vault
73- . check_batch ([
74- (" user:alice" , " view" , " doc:1" ),
75- (" user:alice" , " edit" , " doc:1" ),
76- ])
78+ let results = vault . check_batch ([
79+ (" user:alice" , " view" , " doc:1" ),
80+ (" user:alice" , " edit" , " doc:1" ),
81+ ]). await ? ;
82+ ```
83+
84+ ### Relationships
85+
86+ #### List Relationships
87+
88+ ``` rust
89+ let rels = vault . relationships ()
90+ . list ()
91+ . resource (" document:readme" )
92+ . collect ()
7793 . await ? ;
7894```
7995
80- ### Relationship Management
96+ #### Write a Relationship
97+
98+ ``` rust
99+ vault . relationships ()
100+ . write (Relationship :: new (" document:readme" , " viewer" , " user:alice" ))
101+ . await ? ;
102+ ```
103+
104+ #### Write Multiple Relationships
105+
106+ ``` rust
107+ vault . relationships (). write_batch ([
108+ Relationship :: new (" folder:docs" , " viewer" , " group:engineering#member" ),
109+ Relationship :: new (" document:readme" , " parent" , " folder:docs" ),
110+ ]). await ? ;
111+ ```
112+
113+ #### Delete a Relationship
81114
82115``` rust
83- // Write a single relationship
84- vault
85- . relationships ()
86- . write (Relationship :: new (
87- " document:readme" ,
88- " viewer" ,
89- " user:alice" ,
90- ))
116+ vault . relationships ()
117+ . delete (Relationship :: new (" document:readme" , " viewer" , " user:alice" ))
91118 . await ? ;
119+ ```
120+
121+ #### Delete Multiple Relationships
92122
93- // Batch write
94- vault
95- . relationships ()
96- . write_batch ([
97- Relationship :: new (" folder:docs" , " viewer" , " group:engineering#member" ),
98- Relationship :: new (" document:readme" , " parent" , " folder:docs" ),
99- ])
123+ ``` rust
124+ vault . relationships ()
125+ . delete_where ()
126+ . resource (" document:readme" )
127+ . execute ()
100128 . await ? ;
101129```
102130
103131### Lookups
104132
105133``` rust
106- // Resources a user can access
134+ // What can a user access?
107135let docs = vault . resources ()
108136 . accessible_by (" user:alice" )
109137 . with_permission (" view" )
110138 . resource_type (" document" )
111139 . collect ()
112140 . await ? ;
113141
114- // Users who can access a resource
142+ // Who can access a resource?
115143let users = vault . subjects ()
116144 . with_permission (" view" )
117145 . on_resource (" document:readme" )
118146 . collect ()
119147 . await ? ;
120148```
121149
150+ ### Explain & Simulate
151+
152+ ``` rust
153+ // Debug why a permission was granted or denied
154+ let explanation = vault . explain_permission ()
155+ . subject (" user:alice" )
156+ . permission (" edit" )
157+ . resource (" document:readme" )
158+ . execute ()
159+ . await ? ;
160+ println! (" {}" , explanation . summary ());
161+
162+ // Test what-if scenarios without persisting changes
163+ let result = vault . simulate ()
164+ . add_relationship (Relationship :: new (" doc:1" , " editor" , " user:bob" ))
165+ . check (" user:bob" , " edit" , " doc:1" )
166+ . await ? ;
167+ ```
168+
169+ ### Watch for Changes
170+
171+ ``` rust
172+ // Real-time stream of relationship changes
173+ let mut stream = vault . watch ()
174+ . filter (WatchFilter :: resource_type (" document" ))
175+ . run ()
176+ . await ? ;
177+
178+ while let Some (event ) = stream . next (). await {
179+ let event = event ? ;
180+ println! (" {}: {} {} {}" ,
181+ event . operation, event . resource, event . relation, event . subject);
182+ }
183+ ```
184+
122185## Management API
123186
187+ ``` rust
188+ let org = client . organization (" org_..." );
189+ let vault = org . vault (" vlt_..." );
190+ ```
191+
192+ ### Organizations & Vaults
193+
194+ ### Get Current Organization
195+
196+ ``` rust
197+ let info = org . control (). get (). await ? ;
198+ ```
199+
200+ ### Vaults
201+
202+ #### Create a Vault
203+
204+ ``` rust
205+ let vault = org . vaults (). create (CreateVaultRequest :: new (" production" )). await ? ;
206+ ```
207+
208+ #### List Vaults
209+
210+ ``` rust
211+ let vaults = org . vaults (). list (). collect (). await ? ;
212+ ```
213+
214+ ### Schemas
215+
216+ ``` rust
217+ // Push a new schema version
218+ let result = org . vault (" vlt_..." ). schemas (). push (r # "
219+ type user {}
220+ type document {
221+ relation viewer: user
222+ relation editor: user
223+ permission view = viewer + editor
224+ permission edit = editor
225+ }
226+ " # ). await ? ;
227+
228+ // Validate without persisting
229+ let validation = org . vault (" vlt_..." ). schemas (). validate (schema_content ). await ? ;
230+
231+ // Activate a version
232+ org . vault (" vlt_..." ). schemas (). activate (" v2" ). await ? ;
233+
234+ // Compare versions
235+ let diff = org . vault (" vlt_..." ). schemas (). diff (" v1" , " v2" ). await ? ;
236+ ```
237+
238+ ### Members & Teams
239+
240+ ``` rust
241+ // Invite a member
242+ org . members (). invite (InviteMemberRequest :: new (" alice@example.com" , OrgRole :: Admin )). await ? ;
243+
244+ // Create a team
245+ org . teams (). create (CreateTeamRequest :: new (" Engineering" )). await ? ;
246+
247+ // Add member to team
248+ org . teams (). add_member (" team_..." , " user_..." , TeamRole :: Member ). await ? ;
249+ ```
250+
251+ ### API Clients
252+
253+ ``` rust
254+ // Create an API client for service-to-service auth
255+ let api_client = org . clients (). create (
256+ CreateApiClientRequest :: new (" payment-service" )
257+ ). await ? ;
258+
259+ // Rotate credentials
260+ org . clients (). certificates (" client_..." ). rotate (
261+ RotateCertificateRequest :: new (public_key_pem )
262+ ). await ? ;
263+ ```
264+
265+ ### Audit Logs
266+
267+ ``` rust
268+ // Query audit events
269+ let events = org . audit (). list ()
270+ . action (AuditAction :: RelationshipCreated )
271+ . since (one_hour_ago )
272+ . collect ()
273+ . await ? ;
274+
275+ // Export to file
276+ org . audit (). export ()
277+ . format (ExportFormat :: Json )
278+ . write_to_file (" audit.json" )
279+ . await ? ;
280+ ```
281+
124282## Local Development
125283
126284[ Deploy a local instance of InferaDB] ( https://github.com/inferadb/deploy/ ) , then configure your client to connect to it.
0 commit comments