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.
1516//! Implementation of the `mz user` command.
17//!
18//! Consult the user-facing documentation for details.
1920use mz_frontegg_client::client::user::{CreateUserRequest, RemoveUserRequest};
21use serde::{Deserialize, Serialize};
22use tabled::Tabled;
2324use crate::{context::ProfileContext, error::Error};
2526/// 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.
30pub email: &'a str,
31/// Represents the new user name to add into the profile organization.
32pub name: &'a str,
33}
3435/// Creates a user in the profile organization.
36pub async fn create(
37 cx: &ProfileContext,
38 CreateArgs { email, name }: CreateArgs<'_>,
39) -> Result<(), Error> {
40let loading_spinner = cx.output_formatter().loading_spinner("Creating user...");
41let roles = cx.admin_client().list_roles().await?;
42let role_ids = roles.into_iter().map(|role| role.id).collect();
4344 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?;
5253 loading_spinner.finish_with_message("User created.");
54Ok(())
55}
5657/// Lists all the users in the profile organization.
58pub async fn list(cx: &ProfileContext) -> Result<(), Error> {
59let output_formatter = cx.output_formatter();
6061let loading_spinner = output_formatter.loading_spinner("Retrieving users...");
62#[derive(Deserialize, Serialize, Tabled)]
63pub struct User {
64#[tabled(rename = "Email")]
65email: String,
66#[tabled(rename = "Name")]
67name: String,
68 }
6970let users = cx.admin_client().list_users().await?;
7172 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 }))?;
7778Ok(())
79}
8081/// Represents the args structure to remove a user from Materialize.
82pub struct RemoveArgs<'a> {
83/// Represents the email of the user to remove.
84pub email: &'a str,
85}
8687/// 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> {
92let loading_spinner = cx.output_formatter().loading_spinner("Removing user...");
9394let users = cx.admin_client().list_users().await?;
95let user = users
96 .into_iter()
97 .find(|x| x.email == email)
98 .expect("email not found.");
99100 cx.admin_client()
101 .remove_user(RemoveUserRequest { user_id: user.id })
102 .await?;
103104 loading_spinner.finish_with_message(format!("User {} removed.", email));
105Ok(())
106}