mz/command/
user.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Implementation of the `mz user` command.
17//!
18//! Consult the user-facing documentation for details.
19
20use mz_frontegg_client::client::user::{CreateUserRequest, RemoveUserRequest};
21use serde::{Deserialize, Serialize};
22use tabled::Tabled;
23
24use crate::{context::ProfileContext, error::Error};
25
26/// Represents the structure to create a user in the profile organization.
27pub struct CreateArgs<'a> {
28    /// Represents the new user email to add into the profile organization.
29    /// This value must be unique in the profile organization.
30    pub email: &'a str,
31    /// Represents the new user name to add into the profile organization.
32    pub name: &'a str,
33}
34
35/// Creates a user in the profile organization.
36pub async fn create(
37    cx: &ProfileContext,
38    CreateArgs { email, name }: CreateArgs<'_>,
39) -> Result<(), Error> {
40    let loading_spinner = cx.output_formatter().loading_spinner("Creating user...");
41    let roles = cx.admin_client().list_roles().await?;
42    let role_ids = roles.into_iter().map(|role| role.id).collect();
43
44    cx.admin_client()
45        .create_user(CreateUserRequest {
46            email: email.to_string(),
47            name: name.to_string(),
48            provider: "local".to_string(),
49            role_ids,
50        })
51        .await?;
52
53    loading_spinner.finish_with_message("User created.");
54    Ok(())
55}
56
57/// Lists all the users in the profile organization.
58pub async fn list(cx: &ProfileContext) -> Result<(), Error> {
59    let output_formatter = cx.output_formatter();
60
61    let loading_spinner = output_formatter.loading_spinner("Retrieving users...");
62    #[derive(Deserialize, Serialize, Tabled)]
63    pub struct User {
64        #[tabled(rename = "Email")]
65        email: String,
66        #[tabled(rename = "Name")]
67        name: String,
68    }
69
70    let users = cx.admin_client().list_users().await?;
71
72    loading_spinner.finish_and_clear();
73    output_formatter.output_table(users.into_iter().map(|x| User {
74        email: x.email,
75        name: x.name,
76    }))?;
77
78    Ok(())
79}
80
81/// Represents the args structure to remove a user from Materialize.
82pub struct RemoveArgs<'a> {
83    /// Represents the email of the user to remove.
84    pub email: &'a str,
85}
86
87/// Removes a user from the profile context using the admin client.
88pub async fn remove(
89    cx: &ProfileContext,
90    RemoveArgs { email }: RemoveArgs<'_>,
91) -> Result<(), Error> {
92    let loading_spinner = cx.output_formatter().loading_spinner("Removing user...");
93
94    let users = cx.admin_client().list_users().await?;
95    let user = users
96        .into_iter()
97        .find(|x| x.email == email)
98        .expect("email not found.");
99
100    cx.admin_client()
101        .remove_user(RemoveUserRequest { user_id: user.id })
102        .await?;
103
104    loading_spinner.finish_with_message(format!("User {} removed.", email));
105    Ok(())
106}