-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add admin db user list
- Loading branch information
1 parent
0a3f3f7
commit 3002e5c
Showing
14 changed files
with
183 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::db::DbPool; | ||
use crate::routes::db::user::DbUser; | ||
use crate::routes::db::ServerDatabaseName; | ||
use crate::server_error::ServerResponse; | ||
use crate::user_id::AdminId; | ||
use axum::extract::Query; | ||
use axum::extract::State; | ||
use axum::http::StatusCode; | ||
use axum::Json; | ||
|
||
#[utoipa::path(get, | ||
path = "/api/v1/admin/db/user/list", | ||
security(("Token" = [])), | ||
params( | ||
ServerDatabaseName, | ||
), | ||
responses( | ||
(status = 200, description = "ok"), | ||
(status = 401, description = "unauthorized"), | ||
(status = 466, description = "db not found"), | ||
) | ||
)] | ||
pub(crate) async fn list( | ||
_admin: AdminId, | ||
State(db_pool): State<DbPool>, | ||
request: Query<ServerDatabaseName>, | ||
) -> ServerResponse<(StatusCode, Json<Vec<DbUser>>)> { | ||
let db = db_pool.find_db_id(&request.db)?; | ||
let users = db_pool | ||
.db_users(db)? | ||
.into_iter() | ||
.map(|(name, role)| DbUser { | ||
database: request.db.clone(), | ||
user: name, | ||
role: role.into(), | ||
}) | ||
.collect(); | ||
|
||
Ok((StatusCode::OK, Json(users))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::AddUser; | ||
use crate::TestServer; | ||
use crate::ADMIN_DB_USER_LIST_URI; | ||
use crate::DB_USER_ADD_URI; | ||
use crate::NO_TOKEN; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord)] | ||
struct DbUser { | ||
database: String, | ||
user: String, | ||
role: String, | ||
} | ||
|
||
#[tokio::test] | ||
async fn list_users() -> anyhow::Result<()> { | ||
let server = TestServer::new().await?; | ||
let user = server.init_user().await?; | ||
let other = server.init_user().await?; | ||
let db = server.init_db("memory", &user).await?; | ||
let role = AddUser { | ||
database: &db, | ||
user: &other.name, | ||
role: "read", | ||
}; | ||
assert_eq!( | ||
server.post(DB_USER_ADD_URI, &role, &user.token).await?.0, | ||
201 | ||
); | ||
let (_, list) = server | ||
.get::<Vec<DbUser>>( | ||
&format!("{ADMIN_DB_USER_LIST_URI}?db={}", &db), | ||
&server.admin_token, | ||
) | ||
.await?; | ||
let mut list = list?; | ||
list.sort(); | ||
let mut expected = vec![ | ||
DbUser { | ||
database: db.clone(), | ||
user: user.name, | ||
role: "admin".to_string(), | ||
}, | ||
DbUser { | ||
database: db, | ||
user: other.name, | ||
role: "read".to_string(), | ||
}, | ||
]; | ||
expected.sort(); | ||
assert_eq!(list, expected); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn no_token() -> anyhow::Result<()> { | ||
let server = TestServer::new().await?; | ||
assert_eq!( | ||
server | ||
.get::<Vec<DbUser>>(&format!("{ADMIN_DB_USER_LIST_URI}?db=some_db"), NO_TOKEN) | ||
.await? | ||
.0, | ||
401 | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.