Skip to content

Commit

Permalink
[server] Extend info on admin user list #1302 (#1309)
Browse files Browse the repository at this point in the history
add login to user status
  • Loading branch information
michaelvlach authored Oct 12, 2024
1 parent 29845e7 commit cf5f7e0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
7 changes: 6 additions & 1 deletion agdb_api/rust/src/api_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub struct UserLogin {
#[derive(Debug, Deserialize, Serialize, ToSchema, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserStatus {
pub name: String,
pub login: bool,
}

impl From<&str> for DbType {
Expand Down Expand Up @@ -217,7 +218,8 @@ mod tests {
let _ = format!(
"{:?}",
UserStatus {
name: "user".to_string()
name: "user".to_string(),
login: true
}
);
let _ = format!(
Expand Down Expand Up @@ -282,9 +284,11 @@ mod tests {
assert!(db < other);
let status = UserStatus {
name: "user".to_string(),
login: true,
};
let other = UserStatus {
name: "user2".to_string(),
login: true,
};
assert!(status < other);
}
Expand Down Expand Up @@ -319,6 +323,7 @@ mod tests {

let status = UserStatus {
name: "user".to_string(),
login: false,
};

assert_eq!(status.cmp(&status), std::cmp::Ordering::Equal);
Expand Down
12 changes: 7 additions & 5 deletions agdb_server/src/db_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use agdb_api::DbUserRole;
use agdb_api::Queries;
use agdb_api::QueryAudit;
use agdb_api::ServerDatabase;
use agdb_api::UserStatus;
use axum::http::StatusCode;
use server_db::ServerDb;
use server_db::ServerDbImpl;
Expand Down Expand Up @@ -693,27 +694,28 @@ impl DbPool {
Ok(databases)
}

pub(crate) async fn find_users(&self) -> ServerResult<Vec<String>> {
pub(crate) async fn find_users(&self) -> ServerResult<Vec<UserStatus>> {
Ok(self
.db()
.await
.exec(
QueryBuilder::select()
.values("username")
.values(["username", "token"])
.ids(
QueryBuilder::search()
.from("users")
.where_()
.distance(CountComparison::Equal(2))
.and()
.keys("username")
.query(),
)
.query(),
)?
.elements
.into_iter()
.map(|e| e.values[0].value.to_string())
.map(|e| UserStatus {
name: e.values[0].value.to_string(),
login: !e.values[1].value.to_string().is_empty(),
})
.collect())
}

Expand Down
7 changes: 1 addition & 6 deletions agdb_server/src/routes/admin/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ pub(crate) async fn list(
_admin: AdminId,
State(db_pool): State<DbPool>,
) -> ServerResponse<(StatusCode, Json<Vec<UserStatus>>)> {
let users = db_pool
.find_users()
.await?
.into_iter()
.map(|name| UserStatus { name })
.collect();
let users = db_pool.find_users().await?;
Ok((StatusCode::OK, Json(users)))
}

Expand Down
9 changes: 6 additions & 3 deletions agdb_server/tests/routes/admin_user_list_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ async fn user_list() -> anyhow::Result<()> {
let (status, list) = server.api.admin_user_list().await?;
assert_eq!(status, 200);
assert!(list.contains(&UserStatus {
name: "admin".to_string()
name: "admin".to_string(),
login: true
}));
assert!(list.contains(&UserStatus {
name: user1.to_string()
name: user1.to_string(),
login: false,
}));
assert!(list.contains(&UserStatus {
name: user2.to_string()
name: user2.to_string(),
login: false,
}));
Ok(())
}
Expand Down

0 comments on commit cf5f7e0

Please sign in to comment.