server: Disambiguate list_users query

The confusion of display_name caused every user to be called like the
first group they belonged to.
This commit is contained in:
Valentin Tolmer 2022-06-30 10:12:08 +02:00 committed by nitnelave
parent 5e2eea0d97
commit a512b1844a

View File

@ -2,7 +2,7 @@ use super::{error::*, handler::*, sql_tables::*};
use crate::infra::configuration::Configuration; use crate::infra::configuration::Configuration;
use async_trait::async_trait; use async_trait::async_trait;
use futures_util::StreamExt; use futures_util::StreamExt;
use sea_query::{Cond, Expr, Iden, Order, Query, SimpleExpr}; use sea_query::{Alias, Cond, Expr, Iden, Order, Query, SimpleExpr};
use sea_query_binder::SqlxBinder; use sea_query_binder::SqlxBinder;
use sqlx::{query_as_with, query_with, FromRow, Row}; use sqlx::{query_as_with, query_with, FromRow, Row};
use std::collections::HashSet; use std::collections::HashSet;
@ -144,8 +144,11 @@ impl BackendHandler for SqlBackendHandler {
add_join_group_tables(&mut query_builder); add_join_group_tables(&mut query_builder);
query_builder query_builder
.column((Groups::Table, Groups::GroupId)) .column((Groups::Table, Groups::GroupId))
.column((Groups::Table, Groups::DisplayName)) .expr_as(
.order_by((Groups::Table, Groups::DisplayName), Order::Asc); Expr::col((Groups::Table, Groups::DisplayName)),
Alias::new("group_display_name"),
)
.order_by(Alias::new("group_display_name"), Order::Asc);
} }
if let Some(filter) = filters { if let Some(filter) = filters {
if filter == UserRequestFilter::Not(Box::new(UserRequestFilter::And(Vec::new()))) { if filter == UserRequestFilter::Not(Box::new(UserRequestFilter::And(Vec::new()))) {
@ -156,18 +159,8 @@ impl BackendHandler for SqlBackendHandler {
{ {
let (RequiresGroup(requires_group), condition) = get_user_filter_expr(filter); let (RequiresGroup(requires_group), condition) = get_user_filter_expr(filter);
query_builder.cond_where(condition); query_builder.cond_where(condition);
if requires_group { if requires_group && !get_groups {
query_builder add_join_group_tables(&mut query_builder);
.left_join(
Memberships::Table,
Expr::tbl(Users::Table, Users::UserId)
.equals(Memberships::Table, Memberships::UserId),
)
.left_join(
Groups::Table,
Expr::tbl(Memberships::Table, Memberships::GroupId)
.equals(Groups::Table, Groups::GroupId),
);
} }
} }
} }
@ -195,7 +188,7 @@ impl BackendHandler for SqlBackendHandler {
rows.map(|row| { rows.map(|row| {
GroupIdAndName( GroupIdAndName(
GroupId(row.get::<i32, _>(&*Groups::GroupId.to_string())), GroupId(row.get::<i32, _>(&*Groups::GroupId.to_string())),
row.get::<String, _>(&*Groups::DisplayName.to_string()), row.get::<String, _>("group_display_name"),
) )
}) })
.filter(|g| !g.1.is_empty()) .filter(|g| !g.1.is_empty())
@ -693,6 +686,7 @@ mod tests {
.map(|u| { .map(|u| {
( (
u.user.user_id.to_string(), u.user.user_id.to_string(),
u.user.display_name.to_string(),
u.groups u.groups
.unwrap() .unwrap()
.into_iter() .into_iter()
@ -704,9 +698,9 @@ mod tests {
assert_eq!( assert_eq!(
users, users,
vec![ vec![
("bob".to_string(), vec![group_1]), ("bob".to_string(), String::new(), vec![group_1]),
("john".to_string(), vec![group_2]), ("john".to_string(), String::new(), vec![group_2]),
("patrick".to_string(), vec![group_1, group_2]), ("patrick".to_string(), String::new(), vec![group_1, group_2]),
] ]
); );
} }