Add user_id filter when listing tenants with global API key. (#34)

This commit is contained in:
Alejandra Buznego 2022-08-31 02:48:11 -05:00 committed by GitHub
parent 0a5c38d322
commit 4f08f7ddcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 1 deletions

View File

@ -189,6 +189,10 @@ message ListTenantsRequest {
// If set, the given string will be used to search on name.
string search = 3;
// If set, filters the result set to the tenants of the user.
// Only global API keys are able to filter by this field.
string user_id = 4;
}
message ListTenantsResponse {

View File

@ -189,6 +189,10 @@ message ListTenantsRequest {
// If set, the given string will be used to search on name.
string search = 3;
// If set, filters the result set to the tenants of the user.
// Only global API keys are able to filter by this field.
string user_id = 4;
}
message ListTenantsResponse {

View File

@ -189,6 +189,10 @@ message ListTenantsRequest {
// If set, the given string will be used to search on name.
string search = 3;
// If set, filters the result set to the tenants of the user.
// Only global API keys are able to filter by this field.
string user_id = 4;
}
message ListTenantsResponse {

View File

@ -172,8 +172,13 @@ impl TenantService for Tenant {
}
}
AuthID::Key(_) => {
// Nothing to do as the validator function already validated that the
// Nothing else to do as the validator function already validated that the
// API key must be a global admin key.
if !req.user_id.is_empty() {
let user_id = Uuid::from_str(&req.user_id).map_err(|e| e.status())?;
filters.user_id = Some(user_id);
}
}
_ => {
// this should never happen
@ -492,6 +497,7 @@ pub mod test {
search: "update".into(),
offset: 0,
limit: 10,
user_id: "".into(),
};
let mut list_req = Request::new(list_req);
list_req.extensions_mut().insert(AuthID::User(u.id.clone()));

View File

@ -429,6 +429,18 @@ pub mod test {
let t_get = get(&t.id).await.unwrap();
assert_eq!(t, t_get);
// add tenant user for filter by user_id test
let user = create_user().await;
let tu = TenantUser {
tenant_id: t.id,
user_id: user.id,
is_admin: true,
..Default::default()
};
add_user(tu).await.unwrap();
// get_count and list
let tests = vec![
FilterTest {
@ -481,6 +493,16 @@ pub mod test {
limit: 10,
offset: 10,
},
FilterTest {
filter: Filters {
user_id: Some(user.id),
search: None,
},
ts: vec![&t],
count: 1,
limit: 10,
offset: 0,
},
];
for tst in tests {
let count = get_count(&tst.filter).await.unwrap() as usize;