Skip to main content

outfox_openai/admin/
project_user_roles.rs

1use crate::config::Config;
2use crate::error::OpenAIError;
3use crate::spec::admin::groups::PublicAssignOrganizationGroupRoleBody;
4use crate::spec::admin::roles::{DeletedRoleAssignmentResource, RoleListResource};
5use crate::spec::admin::users::UserRoleAssignment;
6use crate::{Client, RequestOptions};
7
8/// Manage role assignments for users in a project.
9pub struct ProjectUserRoles<'c, C: Config> {
10    client: &'c Client<C>,
11    pub project_id: String,
12    pub user_id: String,
13    pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> ProjectUserRoles<'c, C> {
17    pub fn new(client: &'c Client<C>, project_id: &str, user_id: &str) -> Self {
18        Self {
19            client,
20            project_id: project_id.into(),
21            user_id: user_id.into(),
22            request_options: RequestOptions::new(),
23        }
24    }
25
26    /// Lists all role assignments for a user in the project.
27    #[crate::byot(R = serde::de::DeserializeOwned)]
28    pub async fn list(&self) -> Result<RoleListResource, OpenAIError> {
29        self.client
30            .get(
31                format!("/projects/{}/users/{}/roles", self.project_id, self.user_id).as_str(),
32                &self.request_options,
33            )
34            .await
35    }
36
37    /// Assigns a role to a user in the project.
38    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
39    pub async fn assign(
40        &self,
41        request: PublicAssignOrganizationGroupRoleBody,
42    ) -> Result<UserRoleAssignment, OpenAIError> {
43        self.client
44            .post(
45                format!("/projects/{}/users/{}/roles", self.project_id, self.user_id).as_str(),
46                request,
47                &self.request_options,
48            )
49            .await
50    }
51
52    /// Unassigns a role from a user in the project.
53    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
54    pub async fn unassign(
55        &self,
56        role_id: &str,
57    ) -> Result<DeletedRoleAssignmentResource, OpenAIError> {
58        self.client
59            .delete(
60                format!(
61                    "/projects/{}/users/{}/roles/{}",
62                    self.project_id, self.user_id, role_id
63                )
64                .as_str(),
65                &self.request_options,
66            )
67            .await
68    }
69}