Skip to content

Commit

Permalink
refactor(client)!: AuthClient::user_revoke_role
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <[email protected]>
  • Loading branch information
lxl66566 committed Aug 1, 2024
1 parent 2a4ef60 commit 87f7fe7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 58 deletions.
13 changes: 3 additions & 10 deletions crates/xline-client/examples/auth_user.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anyhow::Result;
use xline_client::{
types::auth::{AuthUserChangePasswordRequest, AuthUserRevokeRoleRequest},
Client, ClientOptions,
};
use xline_client::{types::auth::AuthUserChangePasswordRequest, Client, ClientOptions};

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -39,12 +36,8 @@ async fn main() -> Result<()> {
}

// revoke role from user
client
.user_revoke_role(AuthUserRevokeRoleRequest::new("user1", "role1"))
.await?;
client
.user_revoke_role(AuthUserRevokeRoleRequest::new("user2", "role2"))
.await?;
client.user_revoke_role("user1", "role1").await?;
client.user_revoke_role("user2", "role2").await?;

// delete users
client.user_delete("user1").await?;
Expand Down
20 changes: 13 additions & 7 deletions crates/xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
types::auth::{
AuthRoleAddRequest, AuthRoleDeleteRequest, AuthRoleGetRequest,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
AuthUserChangePasswordRequest, AuthUserRevokeRoleRequest,
AuthUserChangePasswordRequest,
},
AuthService, CurpClient,
};
Expand Down Expand Up @@ -470,7 +470,7 @@ impl AuthClient {
/// # Examples
///
/// ```no_run
/// use xline_client::{types::auth::AuthUserRevokeRoleRequest, Client, ClientOptions};
/// use xline_client::{Client, ClientOptions};
/// use anyhow::Result;
///
/// #[tokio::main]
Expand All @@ -483,19 +483,25 @@ impl AuthClient {
///
/// // grant role
///
/// client
/// .user_revoke_role(AuthUserRevokeRoleRequest::new("user", "role"))
/// .await?;
/// client.user_revoke_role("user", "role").await?;
///
/// Ok(())
/// }
///```
#[inline]
pub async fn user_revoke_role(
&self,
request: AuthUserRevokeRoleRequest,
name: impl Into<String>,
role: impl Into<String>,
) -> Result<AuthUserRevokeRoleResponse> {
self.handle_req(request.inner, false).await
self.handle_req(
xlineapi::AuthUserRevokeRoleRequest {
name: name.into(),
role: role.into(),
},
false,
)
.await
}

/// Adds role.
Expand Down
30 changes: 0 additions & 30 deletions crates/xline-client/src/types/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,6 @@ impl From<AuthUserChangePasswordRequest> for xlineapi::AuthUserChangePasswordReq
}
}

/// Request for `AuthUserRevokeRole`
#[derive(Debug, PartialEq)]
pub struct AuthUserRevokeRoleRequest {
/// Inner request
pub(crate) inner: xlineapi::AuthUserRevokeRoleRequest,
}

impl AuthUserRevokeRoleRequest {
/// Creates a new `AuthUserRevokeRoleRequest`
///
/// `user_name` is the name of the user to revoke role,
/// `role` is the role name to revoke.
#[inline]
pub fn new(user_name: impl Into<String>, role: impl Into<String>) -> Self {
Self {
inner: xlineapi::AuthUserRevokeRoleRequest {
name: user_name.into(),
role: role.into(),
},
}
}
}

impl From<AuthUserRevokeRoleRequest> for xlineapi::AuthUserRevokeRoleRequest {
#[inline]
fn from(req: AuthUserRevokeRoleRequest) -> Self {
req.inner
}
}

/// Request for `AuthRoleAdd`
#[derive(Debug, PartialEq)]
pub struct AuthRoleAddRequest {
Expand Down
10 changes: 3 additions & 7 deletions crates/xline-client/tests/it/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use xline_client::{
types::auth::{
AuthRoleAddRequest, AuthRoleDeleteRequest, AuthRoleGetRequest,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
AuthUserChangePasswordRequest, AuthUserRevokeRoleRequest, Permission, PermissionType,
AuthUserChangePasswordRequest, Permission, PermissionType,
},
};

Expand Down Expand Up @@ -165,12 +165,8 @@ async fn user_role_operations_should_success_in_normal_path() -> Result<()> {
vec![role1.to_owned(), role2.to_owned()]
);

client
.user_revoke_role(AuthUserRevokeRoleRequest::new(name1, role1))
.await?;
client
.user_revoke_role(AuthUserRevokeRoleRequest::new(name1, role2))
.await?;
client.user_revoke_role(name1, role1).await?;
client.user_revoke_role(name1, role2).await?;

Ok(())
}
11 changes: 7 additions & 4 deletions crates/xlinectl/src/command/user/revoke_role.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use clap::{arg, ArgMatches, Command};
use xline_client::{error::Result, types::auth::AuthUserRevokeRoleRequest, Client};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

/// Temporary struct for testing, indicates `(user_name, role)`
type AuthUserRevokeRoleRequest = (String, String);

/// Definition of `revoke_role` command
pub(super) fn command() -> Command {
Command::new("revoke_role")
Expand All @@ -15,13 +18,13 @@ pub(super) fn command() -> Command {
pub(super) fn build_request(matches: &ArgMatches) -> AuthUserRevokeRoleRequest {
let name = matches.get_one::<String>("name").expect("required");
let role = matches.get_one::<String>("role").expect("required");
AuthUserRevokeRoleRequest::new(name, role)
(name.to_owned(), role.to_owned())
}

/// Execute the command
pub(super) async fn execute(client: &mut Client, matches: &ArgMatches) -> Result<()> {
let req = build_request(matches);
let resp = client.auth_client().user_revoke_role(req).await?;
let resp = client.auth_client().user_revoke_role(req.0, req.1).await?;
resp.print();

Ok(())
Expand All @@ -38,7 +41,7 @@ mod tests {
fn command_parse_should_be_valid() {
let test_cases = vec![TestCase::new(
vec!["revoke_role", "JohnDoe", "Admin"],
Some(AuthUserRevokeRoleRequest::new("JohnDoe", "Admin")),
Some(("JohnDoe".to_owned(), "Admin".to_owned())),
)];

for case in test_cases {
Expand Down

0 comments on commit 87f7fe7

Please sign in to comment.