Add a method to get a single user's details

This commit is contained in:
Valentin Tolmer 2021-06-30 20:43:18 +02:00 committed by nitnelave
parent 5a5db13cc2
commit b0c721bb28
3 changed files with 45 additions and 0 deletions

View File

@ -13,6 +13,7 @@ pub trait LoginHandler: Clone + Send {
pub trait BackendHandler: Clone + Send { pub trait BackendHandler: Clone + Send {
async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>; async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>;
async fn list_groups(&self) -> Result<Vec<Group>>; async fn list_groups(&self) -> Result<Vec<Group>>;
async fn get_user_details(&self, request: UserDetailsRequest) -> Result<User>;
async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>;
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>; async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
@ -30,6 +31,7 @@ mockall::mock! {
impl BackendHandler for TestBackendHandler { impl BackendHandler for TestBackendHandler {
async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>; async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>;
async fn list_groups(&self) -> Result<Vec<Group>>; async fn list_groups(&self) -> Result<Vec<Group>>;
async fn get_user_details(&self, request: UserDetailsRequest) -> Result<User>;
async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>;
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>; async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;

View File

@ -117,6 +117,24 @@ impl BackendHandler for SqlBackendHandler {
Ok(groups) Ok(groups)
} }
async fn get_user_details(&self, request: UserDetailsRequest) -> Result<User> {
let query = Query::select()
.column(Users::UserId)
.column(Users::Email)
.column(Users::DisplayName)
.column(Users::FirstName)
.column(Users::LastName)
.column(Users::Avatar)
.column(Users::CreationDate)
.from(Users::Table)
.and_where(Expr::col(Users::UserId).eq(request.user_id))
.to_string(DbQueryBuilder {});
Ok(sqlx::query_as::<_, User>(&query)
.fetch_one(&self.sql_pool)
.await?)
}
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>> { async fn get_user_groups(&self, user: String) -> Result<HashSet<String>> {
if user == self.config.ldap_user_dn { if user == self.config.ldap_user_dn {
let mut groups = HashSet::new(); let mut groups = HashSet::new();
@ -450,6 +468,30 @@ mod tests {
); );
} }
#[tokio::test]
async fn test_get_user_details() {
let sql_pool = get_initialized_db().await;
let config = get_default_config();
let handler = SqlBackendHandler::new(config, sql_pool);
insert_user(&handler, "bob", "bob00").await;
{
let user = handler
.get_user_details(UserDetailsRequest {
user_id: String::from("bob"),
})
.await
.unwrap();
assert_eq!(user.user_id, "bob".to_string());
}
{
handler
.get_user_details(UserDetailsRequest {
user_id: String::from("John"),
})
.await
.unwrap_err();
}
}
#[tokio::test] #[tokio::test]
async fn test_get_user_groups() { async fn test_get_user_groups() {
let sql_pool = get_initialized_db().await; let sql_pool = get_initialized_db().await;

View File

@ -28,6 +28,7 @@ mockall::mock! {
impl BackendHandler for TestTcpBackendHandler { impl BackendHandler for TestTcpBackendHandler {
async fn list_users(&self, request: ListUsersRequest) -> DomainResult<Vec<User>>; async fn list_users(&self, request: ListUsersRequest) -> DomainResult<Vec<User>>;
async fn list_groups(&self) -> DomainResult<Vec<Group>>; async fn list_groups(&self) -> DomainResult<Vec<Group>>;
async fn get_user_details(&self, request: UserDetailsRequest) -> DomainResult<User>;
async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>; async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>;
async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>; async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> DomainResult<()>; async fn delete_user(&self, request: DeleteUserRequest) -> DomainResult<()>;