Skip to main content

junobuild_satellite/controllers/
store.rs

1use crate::memory::state::STATE;
2use junobuild_shared::segments::controllers::{
3    delete_controllers as delete_controllers_impl, filter_admin_controllers,
4    set_controllers as set_controllers_impl,
5};
6use junobuild_shared::types::interface::SetController;
7use junobuild_shared::types::state::{ControllerId, Controllers};
8
9// ---------------------------------------------------------
10// Controllers
11// ---------------------------------------------------------
12
13pub fn set_controllers(new_controllers: &[ControllerId], controller: &SetController) {
14    STATE.with(|state| {
15        set_controllers_impl(
16            new_controllers,
17            controller,
18            &mut state.borrow_mut().heap.controllers,
19        )
20    })
21}
22
23pub fn delete_controllers(remove_controllers: &[ControllerId]) {
24    STATE.with(|state| {
25        delete_controllers_impl(remove_controllers, &mut state.borrow_mut().heap.controllers)
26    })
27}
28
29/// Retrieve the controllers of the Satellite.
30///
31/// This function retrieves the list of controllers currently set for the application.
32/// A controller is represented as a key-value pair where the key is the `ControllerId`
33/// (a `Principal`) and the value is a `Controller` struct containing metadata and scope
34/// details about the controller.
35///
36/// # Returns
37/// - `Controllers`: A `HashMap` where:
38///   - The key is a `ControllerId` (type alias for `Principal`).
39///   - The value is a `Controller` struct that includes:
40///     - `metadata`: Additional metadata about the controller, such as name or description.
41///     - `created_at`: The timestamp when the controller was created.
42///     - `updated_at`: The timestamp when the controller was last updated.
43///     - `expires_at`: An optional timestamp indicating when the controller expires.
44///     - `scope`: The `ControllerScope`, which specifies the access level (`Write` or `Admin`).
45///
46/// # Example
47/// ```
48/// let controllers = get_controllers();
49/// for (controller_id, controller) in controllers.iter() {
50///     println!("Controller ID: {}", controller_id);
51///     println!("Scope: {:?}", controller.scope);
52///     if let Some(expires_at) = controller.expires_at {
53///         println!("Expires at: {}", expires_at);
54///     }
55/// }
56/// ```
57///
58/// This function is useful for checking the current permissions.
59pub fn get_controllers() -> Controllers {
60    STATE.with(|state| state.borrow().heap.controllers.clone())
61}
62
63/// Retrieve the current admin controllers for a Satellite.
64///
65/// This function filters the list of controllers to include only those with the `Admin` scope.
66///
67/// # Returns
68/// - `Controllers`: A `HashMap` where:
69///   - The key is a `ControllerId` (type alias for `Principal`).
70///   - The value is a `Controller` struct that includes:
71///     - `metadata`: Additional metadata about the controller.
72///     - `created_at`: The timestamp when the controller was created.
73///     - `updated_at`: The timestamp when the controller was last updated.
74///     - `expires_at`: An optional timestamp indicating when the controller expires.
75///     - `scope`: Always `ControllerScope::Admin` for admin controllers.
76///
77/// # Example
78/// ```
79/// let admin_controllers = get_admin_controllers();
80/// for (controller_id, controller) in admin_controllers.iter() {
81///     println!("Admin Controller ID: {}", controller_id);
82///     println!("Created at: {}", controller.created_at);
83/// }
84/// ```
85///
86/// This function is useful for auditing or managing permissions for admin-level access.
87pub fn get_admin_controllers() -> Controllers {
88    STATE.with(|state| filter_admin_controllers(&state.borrow().heap.controllers))
89}