From 4f08f7ddcbaecbfca8898db5c2c1889493d1b9d0 Mon Sep 17 00:00:00 2001 From: Alejandra Buznego <51131939+ambuznego@users.noreply.github.com> Date: Wed, 31 Aug 2022 02:48:11 -0500 Subject: [PATCH] Add user_id filter when listing tenants with global API key. (#34) --- api/proto/api/tenant.proto | 4 ++++ .../proto/chirpstack-api/api/tenant.proto | 4 ++++ api/rust/proto/chirpstack/api/tenant.proto | 4 ++++ chirpstack/src/api/tenant.rs | 8 ++++++- chirpstack/src/storage/tenant.rs | 22 +++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/api/proto/api/tenant.proto b/api/proto/api/tenant.proto index 1bcfeb05..3a53cc9d 100644 --- a/api/proto/api/tenant.proto +++ b/api/proto/api/tenant.proto @@ -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 { diff --git a/api/python/proto/chirpstack-api/api/tenant.proto b/api/python/proto/chirpstack-api/api/tenant.proto index 1bcfeb05..3a53cc9d 100644 --- a/api/python/proto/chirpstack-api/api/tenant.proto +++ b/api/python/proto/chirpstack-api/api/tenant.proto @@ -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 { diff --git a/api/rust/proto/chirpstack/api/tenant.proto b/api/rust/proto/chirpstack/api/tenant.proto index 1bcfeb05..3a53cc9d 100644 --- a/api/rust/proto/chirpstack/api/tenant.proto +++ b/api/rust/proto/chirpstack/api/tenant.proto @@ -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 { diff --git a/chirpstack/src/api/tenant.rs b/chirpstack/src/api/tenant.rs index 8bce45e4..01ebfb24 100644 --- a/chirpstack/src/api/tenant.rs +++ b/chirpstack/src/api/tenant.rs @@ -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())); diff --git a/chirpstack/src/storage/tenant.rs b/chirpstack/src/storage/tenant.rs index cbdfae06..1d211e23 100644 --- a/chirpstack/src/storage/tenant.rs +++ b/chirpstack/src/storage/tenant.rs @@ -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;