1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! `PUT/DELETE /api/permissions/{vhost}/{user}` endpoints.
use crate::api::encode_segment;
use crate::{Client, Result};
/// Request body for `PUT /api/permissions/{vhost}/{user}`.
#[derive(Debug, Clone, serde::Serialize)]
struct PermissionSet {
/// Regular expression controlling which resources the user may
/// configure (declare/delete).
configure: String,
/// Regular expression controlling which resources the user may
/// write (publish) to.
write: String,
/// Regular expression controlling which resources the user may
/// read (consume) from.
read: String,
}
impl Client {
/// `PUT /api/permissions/{vhost}/{user}` — grants a user configure,
/// write, and read permissions within a vhost, each as a regular
/// expression matched against resource names. Both segments are
/// percent-encoded.
pub async fn set_permission(
&self,
vhost: &str,
user: &str,
configure: &str,
write: &str,
read: &str,
) -> Result<()> {
self.put(
&permission_path(vhost, user),
&PermissionSet {
configure: configure.to_owned(),
write: write.to_owned(),
read: read.to_owned(),
},
)
.await
}
/// `DELETE /api/permissions/{vhost}/{user}` — revokes all of a user's
/// permissions within a vhost. Both segments are percent-encoded.
pub async fn delete_permission(&self, vhost: &str, user: &str) -> Result<()> {
self.delete_ctx(
&permission_path(vhost, user),
&format!("permission for user '{user}' in vhost '{vhost}'"),
)
.await
}
}
/// Percent-encode vhost and user into a `permissions/{vhost}/{user}`
/// relative path.
fn permission_path(vhost: &str, user: &str) -> String {
format!(
"permissions/{}/{}",
encode_segment(vhost),
encode_segment(user)
)
}