| 193 | } |
| 194 | |
| 195 | async fn list( |
| 196 | &self, |
| 197 | request: Request<api::ListTenantsRequest>, |
| 198 | ) -> Result<Response<api::ListTenantsResponse>, Status> { |
| 199 | self.validator |
| 200 | .validate( |
| 201 | request.extensions(), |
| 202 | validator::ValidateTenantsAccess::new(validator::Flag::List), |
| 203 | ) |
| 204 | .await?; |
| 205 | |
| 206 | let auth_id = request.extensions().get::<AuthID>().unwrap(); |
| 207 | let req = request.get_ref(); |
| 208 | let mut filters = tenant::Filters::default(); |
| 209 | |
| 210 | if !req.search.is_empty() { |
| 211 | filters.search = Some(req.search.clone()); |
| 212 | } |
| 213 | |
| 214 | match auth_id { |
| 215 | AuthID::User(id) => { |
| 216 | let u = user::get(id).await.map_err(|e| e.status())?; |
| 217 | |
| 218 | if !u.is_admin { |
| 219 | filters.user_id = Some(u.id.into()); |
| 220 | } |
| 221 | } |
| 222 | AuthID::Key(_) => { |
| 223 | // Nothing else to do as the validator function already validated that the |
| 224 | // API key must be a global admin key. |
| 225 | |
| 226 | if !req.user_id.is_empty() { |
| 227 | let user_id = Uuid::from_str(&req.user_id).map_err(|e| e.status())?; |
| 228 | filters.user_id = Some(user_id); |
| 229 | } |
| 230 | } |
| 231 | _ => { |
| 232 | // this should never happen |
| 233 | return Err(Status::internal( |
| 234 | "request authenticated but no AuthID in extensions", |
| 235 | )); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | let count = tenant::get_count(&filters).await.map_err(|e| e.status())?; |
| 240 | let results = tenant::list(req.limit as i64, req.offset as i64, &filters) |
| 241 | .await |
| 242 | .map_err(|e| e.status())?; |
| 243 | |
| 244 | Ok(Response::new(api::ListTenantsResponse { |
| 245 | total_count: count as u32, |
| 246 | result: results |
| 247 | .iter() |
| 248 | .map(|t| api::TenantListItem { |
| 249 | id: t.id.to_string(), |
| 250 | created_at: Some(helpers::datetime_to_prost_timestamp(&t.created_at)), |
| 251 | updated_at: Some(helpers::datetime_to_prost_timestamp(&t.updated_at)), |
| 252 | name: t.name.clone(), |