Simplify get_user_groups to take a &str

This commit is contained in:
Valentin Tolmer 2021-08-26 10:00:49 +02:00 committed by nitnelave
parent a51965a61a
commit d2617e08a7
5 changed files with 11 additions and 17 deletions

View File

@ -18,7 +18,7 @@ pub trait BackendHandler: Clone + Send {
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>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>;
} }
#[cfg(test)] #[cfg(test)]
@ -35,7 +35,7 @@ mockall::mock! {
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>;
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
} }
#[async_trait] #[async_trait]

View File

@ -135,7 +135,7 @@ impl BackendHandler for SqlBackendHandler {
.await?) .await?)
} }
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>> { async fn get_user_groups(&self, user: &str) -> 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();
groups.insert("lldap_admin".to_string()); groups.insert("lldap_admin".to_string());
@ -510,19 +510,13 @@ mod tests {
let mut patrick_groups = HashSet::new(); let mut patrick_groups = HashSet::new();
patrick_groups.insert("Group1".to_string()); patrick_groups.insert("Group1".to_string());
patrick_groups.insert("Group2".to_string()); patrick_groups.insert("Group2".to_string());
assert_eq!(handler.get_user_groups("bob").await.unwrap(), bob_groups);
assert_eq!( assert_eq!(
handler.get_user_groups("bob".to_string()).await.unwrap(), handler.get_user_groups("patrick").await.unwrap(),
bob_groups
);
assert_eq!(
handler
.get_user_groups("patrick".to_string())
.await
.unwrap(),
patrick_groups patrick_groups
); );
assert_eq!( assert_eq!(
handler.get_user_groups("John".to_string()).await.unwrap(), handler.get_user_groups("John").await.unwrap(),
HashSet::new() HashSet::new()
); );
} }

View File

@ -89,7 +89,7 @@ where
match res_found { match res_found {
Ok(found) => { Ok(found) => {
if found { if found {
backend_handler.get_user_groups(user.to_string()).await backend_handler.get_user_groups(&user).await
} else { } else {
Err(DomainError::AuthenticationError( Err(DomainError::AuthenticationError(
"Invalid refresh token".to_string(), "Invalid refresh token".to_string(),
@ -191,7 +191,7 @@ where
// The authentication was successful, we need to fetch the groups to create the JWT // The authentication was successful, we need to fetch the groups to create the JWT
// token. // token.
data.backend_handler data.backend_handler
.get_user_groups(name.to_string()) .get_user_groups(name)
.and_then(|g| async { Ok((g, data.backend_handler.create_refresh_token(name).await?)) }) .and_then(|g| async { Ok((g, data.backend_handler.create_refresh_token(name).await?)) })
.await .await
.map(|(groups, (refresh_token, max_age))| { .map(|(groups, (refresh_token, max_age))| {

View File

@ -149,7 +149,7 @@ impl<Handler: BackendHandler + Sync> User<Handler> {
async fn groups(&self, context: &Context<Handler>) -> FieldResult<Vec<Group<Handler>>> { async fn groups(&self, context: &Context<Handler>) -> FieldResult<Vec<Group<Handler>>> {
Ok(context Ok(context
.handler .handler
.get_user_groups(self.user.user_id.clone()) .get_user_groups(&self.user.user_id)
.await .await
.map(|set| set.into_iter().map(Into::into).collect())?) .map(|set| set.into_iter().map(Into::into).collect())?)
} }
@ -243,7 +243,7 @@ mod tests {
let mut groups = HashSet::<String>::new(); let mut groups = HashSet::<String>::new();
groups.insert("Bobbersons".to_string()); groups.insert("Bobbersons".to_string());
mock.expect_get_user_groups() mock.expect_get_user_groups()
.with(eq("bob".to_string())) .with(eq("bob"))
.return_once(|_| Ok(groups)); .return_once(|_| Ok(groups));
let context = Context::<MockTestBackendHandler> { let context = Context::<MockTestBackendHandler> {

View File

@ -29,7 +29,7 @@ mockall::mock! {
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_details(&self, request: UserDetailsRequest) -> DomainResult<User>;
async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>; async fn get_user_groups(&self, user: &str) -> 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<()>;
async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>; async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>;