mz_frontegg_client/client/
role.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! This module implements the client's functions for interacting with the
11//! Frontegg users API.
12
13use reqwest::Method;
14use serde::{Deserialize, Serialize};
15use uuid::Uuid;
16
17use crate::client::Client;
18use crate::error::Error;
19use crate::parse::Paginated;
20
21const ROLES_PATH: [&str; 5] = ["frontegg", "identity", "resources", "roles", "v2"];
22
23/// `Role` represents the Frontegg role structure.
24#[derive(Debug, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct Role {
27    /// A unique identifier for the role.
28    pub id: Uuid,
29    /// The identifier of the vendor associated with the role.
30    pub vendor_id: String,
31    /// The optional identifier of the tenant associated with the role.
32    pub tenant_id: Option<String>,
33    /// A unique key associated with the role.
34    pub key: String,
35    /// The name of the role.
36    pub name: String,
37    /// A description of the role.
38    pub description: String,
39    /// A boolean indicating whether this role is a default one.
40    pub is_default: bool,
41    /// A boolean indicating whether this role was the first one assigned to a user.
42    pub first_user_role: bool,
43    /// A string representing the date and time in ISO 8601 when the role was created.
44    ///
45    /// E.g.: 2023-04-26T08:37:03.000Z
46    pub created_at: String,
47    /// A string representing the date and time in ISO 8601 when the role was last updated.
48    ///
49    /// E.g.: 2023-04-26T08:37:03.000Z
50    pub updated_at: String,
51    /// A vector of strings representing the permissions associated with the role.
52    pub permissions: Vec<String>,
53    /// An integer representing the level of the role.
54    pub level: i32,
55}
56
57impl Client {
58    /// Lists all available roles.
59    pub async fn list_roles(&self) -> Result<Vec<Role>, Error> {
60        let mut roles = vec![];
61        let mut page = 0;
62
63        loop {
64            let req = self.build_request(Method::GET, ROLES_PATH);
65            let req = req.query(&[("_limit", "50"), ("_offset", &*page.to_string())]);
66            let res: Paginated<Role> = self.send_request(req).await?;
67            for role in res.items {
68                roles.push(role);
69            }
70            page += 1;
71            if page >= res.metadata.total_pages {
72                break;
73            }
74        }
75        Ok(roles)
76    }
77}