dinio_sdk/api/zone_members.rs
1//! `/api/v1/zone-members` (`../core/src/api/zone_members.ts`). Core takes the zone by uuid
2//! only; a member is added by email and removed by user uuid
3//! ([`ops::find_member`](crate::ops::find_member) resolves an email).
4
5use reqwest::Method;
6use serde_json::json;
7use uuid::Uuid;
8
9use crate::client::Client;
10use crate::error::Result;
11use crate::models::ZoneMember;
12
13/// Who has access to a zone, from [`Client::zone_members`].
14pub struct ZoneMembers<'a> {
15 client: &'a Client,
16}
17
18impl<'a> ZoneMembers<'a> {
19 pub(crate) fn new(client: &'a Client) -> Self {
20 Self { client }
21 }
22
23 /// The zone's Owner and Administrators, each with their email. Anyone with access to
24 /// the zone may read it.
25 ///
26 /// `GET /api/v1/zone-members?zone_id=...`
27 pub async fn list(&self, zone_id: Uuid) -> Result<Vec<ZoneMember>> {
28 let request = self
29 .client
30 .request(Method::GET, &["zone-members"])?
31 .query(&[("zone_id", zone_id.to_string())]);
32 Ok(self.client.send(request).await?.0)
33 }
34
35 /// Gives the registered user with this email (case-insensitive) `Administrator` access
36 /// to the zone. Owner, Admin or the zone key only.
37 ///
38 /// Core answers the same way whether the user was added, already was a member (the
39 /// role stays as it was) or the email is not registered (dropped, no invitation), so it
40 /// never reveals which emails exist. Read the outcome with [`ZoneMembers::list`].
41 ///
42 /// `POST /api/v1/zone-members`
43 ///
44 /// # Errors
45 ///
46 /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) for a malformed email.
47 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) without Owner rights.
48 pub async fn add(&self, zone_id: Uuid, email: &str) -> Result<()> {
49 let request = self
50 .client
51 .request(Method::POST, &["zone-members"])?
52 .json(&json!({ "zone_id": zone_id, "email": email }));
53 self.client.send_empty(request).await
54 }
55
56 /// Takes away a user's access to the zone. Owner, Admin or the zone key only; the
57 /// Owner cannot be removed.
58 ///
59 /// `DELETE /api/v1/zone-members/:zone_id/:user_id`
60 ///
61 /// # Errors
62 ///
63 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if the user is not a member.
64 /// * [`ErrorCode::Forbidden`](crate::ErrorCode::Forbidden) without Owner rights, or for
65 /// the Owner's own membership.
66 pub async fn remove(&self, zone_id: Uuid, user_id: Uuid) -> Result<()> {
67 let request = self.client.request(
68 Method::DELETE,
69 &["zone-members", &zone_id.to_string(), &user_id.to_string()],
70 )?;
71 self.client.send_empty(request).await
72 }
73}