mirror of
https://github.com/chirpstack/chirpstack.git
synced 2025-05-06 10:58:27 +00:00
Add user_id filter when listing tenants with global API key. (#34)
This commit is contained in:
parent
0a5c38d322
commit
4f08f7ddcb
4
api/proto/api/tenant.proto
vendored
4
api/proto/api/tenant.proto
vendored
@ -189,6 +189,10 @@ message ListTenantsRequest {
|
|||||||
|
|
||||||
// If set, the given string will be used to search on name.
|
// If set, the given string will be used to search on name.
|
||||||
string search = 3;
|
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 {
|
message ListTenantsResponse {
|
||||||
|
@ -189,6 +189,10 @@ message ListTenantsRequest {
|
|||||||
|
|
||||||
// If set, the given string will be used to search on name.
|
// If set, the given string will be used to search on name.
|
||||||
string search = 3;
|
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 {
|
message ListTenantsResponse {
|
||||||
|
4
api/rust/proto/chirpstack/api/tenant.proto
vendored
4
api/rust/proto/chirpstack/api/tenant.proto
vendored
@ -189,6 +189,10 @@ message ListTenantsRequest {
|
|||||||
|
|
||||||
// If set, the given string will be used to search on name.
|
// If set, the given string will be used to search on name.
|
||||||
string search = 3;
|
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 {
|
message ListTenantsResponse {
|
||||||
|
@ -172,8 +172,13 @@ impl TenantService for Tenant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
AuthID::Key(_) => {
|
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.
|
// 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
|
// this should never happen
|
||||||
@ -492,6 +497,7 @@ pub mod test {
|
|||||||
search: "update".into(),
|
search: "update".into(),
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
|
user_id: "".into(),
|
||||||
};
|
};
|
||||||
let mut list_req = Request::new(list_req);
|
let mut list_req = Request::new(list_req);
|
||||||
list_req.extensions_mut().insert(AuthID::User(u.id.clone()));
|
list_req.extensions_mut().insert(AuthID::User(u.id.clone()));
|
||||||
|
@ -429,6 +429,18 @@ pub mod test {
|
|||||||
let t_get = get(&t.id).await.unwrap();
|
let t_get = get(&t.id).await.unwrap();
|
||||||
assert_eq!(t, t_get);
|
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
|
// get_count and list
|
||||||
let tests = vec![
|
let tests = vec![
|
||||||
FilterTest {
|
FilterTest {
|
||||||
@ -481,6 +493,16 @@ pub mod test {
|
|||||||
limit: 10,
|
limit: 10,
|
||||||
offset: 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 {
|
for tst in tests {
|
||||||
let count = get_count(&tst.filter).await.unwrap() as usize;
|
let count = get_count(&tst.filter).await.unwrap() as usize;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user