use std::sync::Arc;
use crate::actix_web::HttpResponse;
use crate::biome::profile::store::UserProfileStore;
use crate::futures::IntoFuture;
#[cfg(feature = "authorization")]
use crate::rest_api::auth::authorization::Permission;
use crate::rest_api::{
actix_web_1::{HandlerFunction, Method, ProtocolVersionRangeGuard, Resource},
auth::identity::Identity,
ErrorResponse, SPLINTER_PROTOCOL_VERSION,
};
const BIOME_FETCH_PROFILE_PROTOCOL_MIN: u32 = 1;
pub fn make_profile_route(profile_store: Arc<dyn UserProfileStore>) -> Resource {
let resource = Resource::build("/biome/profile").add_request_guard(
ProtocolVersionRangeGuard::new(BIOME_FETCH_PROFILE_PROTOCOL_MIN, SPLINTER_PROTOCOL_VERSION),
);
#[cfg(feature = "authorization")]
{
resource.add_method(
Method::Get,
Permission::AllowAuthenticated,
handle_get(profile_store),
)
}
#[cfg(not(feature = "authorization"))]
{
resource.add_method(Method::Get, handle_get(profile_store))
}
}
fn handle_get(profile_store: Arc<dyn UserProfileStore>) -> HandlerFunction {
Box::new(move |request, _| {
let profile_store = profile_store.clone();
let user = match request.extensions().get::<Identity>() {
Some(Identity::User(user)) => user.clone(),
_ => {
return Box::new(
HttpResponse::Unauthorized()
.json(ErrorResponse::unauthorized())
.into_future(),
)
}
};
match profile_store.get_profile(&user) {
Ok(profile) => Box::new(HttpResponse::Ok().json(profile).into_future()),
Err(err) => {
debug!("Failed to fetch profile {}", err);
Box::new(
HttpResponse::InternalServerError()
.json(ErrorResponse::internal_error())
.into_future(),
)
}
}
})
}