Skip to content

Commit 1c16300

Browse files
committedFeb 20, 2024··
Merge torrust#486: fix: [torrust#445] refactor handlers with new user id extractor
21f533f refactor: [torrust#445] fix formatting warning (Mario) 61388de fix: [torrust#445] refactor handlers with new user id extractor (Mario) Pull request description: Fixes torrust#447, some changes to the code were lost in a merge, this PR restores those changes. ACKs for top commit: josecelano: ACK 21f533f Tree-SHA512: ae2cd790d6f9d1d92d91ef3e052d21314b773b32a77ad1f8373add804661bf83b7dc55a9535dc74bd73bc33b41b9d808154bc9dddcfc9f88c00ff679f4b86467
2 parents c96013c + 21f533f commit 1c16300

File tree

5 files changed

+17
-58
lines changed

5 files changed

+17
-58
lines changed
 

‎src/web/api/server/v1/contexts/category/handlers.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::response::{IntoResponse, Json, Response};
88
use super::forms::{AddCategoryForm, DeleteCategoryForm};
99
use super::responses::{added_category, deleted_category};
1010
use crate::common::AppData;
11-
use crate::web::api::server::v1::extractors::bearer_token::Extract;
11+
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1212
use crate::web::api::server::v1::responses::{self};
1313

1414
/// It handles the request to get all the categories.
@@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
4343
#[allow(clippy::unused_async)]
4444
pub async fn add_handler(
4545
State(app_data): State<Arc<AppData>>,
46-
Extract(maybe_bearer_token): Extract,
46+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
4747
extract::Json(category_form): extract::Json<AddCategoryForm>,
4848
) -> Response {
49-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
50-
Ok(user_id) => user_id,
51-
Err(error) => return error.into_response(),
52-
};
53-
5449
match app_data.category_service.add_category(&category_form.name, &user_id).await {
5550
Ok(_) => added_category(&category_form.name).into_response(),
5651
Err(error) => error.into_response(),
@@ -68,18 +63,13 @@ pub async fn add_handler(
6863
#[allow(clippy::unused_async)]
6964
pub async fn delete_handler(
7065
State(app_data): State<Arc<AppData>>,
71-
Extract(maybe_bearer_token): Extract,
66+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
7267
extract::Json(category_form): extract::Json<DeleteCategoryForm>,
7368
) -> Response {
7469
// code-review: why do we need to send the whole category object to delete it?
7570
// And we should use the ID instead of the name, because the name could change
7671
// or we could add support for multiple languages.
7772

78-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
79-
Ok(user_id) => user_id,
80-
Err(error) => return error.into_response(),
81-
};
82-
8373
match app_data.category_service.delete_category(&category_form.name, &user_id).await {
8474
Ok(()) => deleted_category(&category_form.name).into_response(),
8575
Err(error) => error.into_response(),

‎src/web/api/server/v1/contexts/settings/handlers.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use axum::extract::State;
66
use axum::response::{IntoResponse, Json, Response};
77

88
use crate::common::AppData;
9-
use crate::web::api::server::v1::extractors::bearer_token::Extract;
9+
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1010
use crate::web::api::server::v1::responses;
1111

1212
/// Get all settings.
@@ -16,12 +16,10 @@ use crate::web::api::server::v1::responses;
1616
/// This function will return an error if the user does not have permission to
1717
/// view all the settings.
1818
#[allow(clippy::unused_async)]
19-
pub async fn get_all_handler(State(app_data): State<Arc<AppData>>, Extract(maybe_bearer_token): Extract) -> Response {
20-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
21-
Ok(user_id) => user_id,
22-
Err(error) => return error.into_response(),
23-
};
24-
19+
pub async fn get_all_handler(
20+
State(app_data): State<Arc<AppData>>,
21+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
22+
) -> Response {
2523
let all_settings = match app_data.settings_service.get_all_masking_secrets(&user_id).await {
2624
Ok(all_settings) => all_settings,
2725
Err(error) => return error.into_response(),

‎src/web/api/server/v1/contexts/tag/handlers.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::response::{IntoResponse, Json, Response};
88
use super::forms::{AddTagForm, DeleteTagForm};
99
use super::responses::{added_tag, deleted_tag};
1010
use crate::common::AppData;
11-
use crate::web::api::server::v1::extractors::bearer_token::Extract;
11+
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1212
use crate::web::api::server::v1::responses::{self};
1313

1414
/// It handles the request to get all the tags.
@@ -43,14 +43,9 @@ pub async fn get_all_handler(State(app_data): State<Arc<AppData>>) -> Response {
4343
#[allow(clippy::unused_async)]
4444
pub async fn add_handler(
4545
State(app_data): State<Arc<AppData>>,
46-
Extract(maybe_bearer_token): Extract,
46+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
4747
extract::Json(add_tag_form): extract::Json<AddTagForm>,
4848
) -> Response {
49-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
50-
Ok(user_id) => user_id,
51-
Err(error) => return error.into_response(),
52-
};
53-
5449
match app_data.tag_service.add_tag(&add_tag_form.name, &user_id).await {
5550
Ok(_) => added_tag(&add_tag_form.name).into_response(),
5651
Err(error) => error.into_response(),
@@ -68,14 +63,9 @@ pub async fn add_handler(
6863
#[allow(clippy::unused_async)]
6964
pub async fn delete_handler(
7065
State(app_data): State<Arc<AppData>>,
71-
Extract(maybe_bearer_token): Extract,
66+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
7267
extract::Json(delete_tag_form): extract::Json<DeleteTagForm>,
7368
) -> Response {
74-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
75-
Ok(user_id) => user_id,
76-
Err(error) => return error.into_response(),
77-
};
78-
7969
match app_data.tag_service.delete_tag(&delete_tag_form.tag_id, &user_id).await {
8070
Ok(()) => deleted_tag(delete_tag_form.tag_id).into_response(),
8171
Err(error) => error.into_response(),

‎src/web/api/server/v1/contexts/torrent/handlers.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::services::torrent_file::generate_random_torrent;
2323
use crate::utils::parse_torrent;
2424
use crate::web::api::server::v1::auth::get_optional_logged_in_user;
2525
use crate::web::api::server::v1::extractors::bearer_token::Extract;
26+
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
2627
use crate::web::api::server::v1::responses::OkResponseData;
2728
use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;
2829

@@ -37,14 +38,9 @@ use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;
3738
#[allow(clippy::unused_async)]
3839
pub async fn upload_torrent_handler(
3940
State(app_data): State<Arc<AppData>>,
40-
Extract(maybe_bearer_token): Extract,
41+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
4142
multipart: Multipart,
4243
) -> Response {
43-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
44-
Ok(user_id) => user_id,
45-
Err(error) => return error.into_response(),
46-
};
47-
4844
let add_torrent_form = match build_add_torrent_request_from_payload(multipart).await {
4945
Ok(torrent_request) => torrent_request,
5046
Err(error) => return error.into_response(),
@@ -220,19 +216,14 @@ async fn redirect_to_details_url_using_canonical_info_hash_if_needed(
220216
#[allow(clippy::unused_async)]
221217
pub async fn update_torrent_info_handler(
222218
State(app_data): State<Arc<AppData>>,
223-
Extract(maybe_bearer_token): Extract,
219+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
224220
Path(info_hash): Path<InfoHashParam>,
225221
extract::Json(update_torrent_info_form): extract::Json<UpdateTorrentInfoForm>,
226222
) -> Response {
227223
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
228224
return errors::Request::InvalidInfoHashParam.into_response();
229225
};
230226

231-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
232-
Ok(user_id) => user_id,
233-
Err(error) => return error.into_response(),
234-
};
235-
236227
match app_data
237228
.torrent_service
238229
.update_torrent_info(
@@ -262,18 +253,13 @@ pub async fn update_torrent_info_handler(
262253
#[allow(clippy::unused_async)]
263254
pub async fn delete_torrent_handler(
264255
State(app_data): State<Arc<AppData>>,
265-
Extract(maybe_bearer_token): Extract,
256+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
266257
Path(info_hash): Path<InfoHashParam>,
267258
) -> Response {
268259
let Ok(info_hash) = InfoHash::from_str(&info_hash.lowercase()) else {
269260
return errors::Request::InvalidInfoHashParam.into_response();
270261
};
271262

272-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
273-
Ok(user_id) => user_id,
274-
Err(error) => return error.into_response(),
275-
};
276-
277263
match app_data.torrent_service.delete_torrent(&info_hash, &user_id).await {
278264
Ok(deleted_torrent_response) => Json(OkResponseData {
279265
data: deleted_torrent_response,

‎src/web/api/server/v1/contexts/user/handlers.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::Deserialize;
1010
use super::forms::{JsonWebToken, LoginForm, RegistrationForm};
1111
use super::responses::{self};
1212
use crate::common::AppData;
13-
use crate::web::api::server::v1::extractors::bearer_token::Extract;
13+
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1414
use crate::web::api::server::v1::responses::OkResponseData;
1515

1616
// Registration
@@ -135,15 +135,10 @@ pub async fn renew_token_handler(
135135
pub async fn ban_handler(
136136
State(app_data): State<Arc<AppData>>,
137137
Path(to_be_banned_username): Path<UsernameParam>,
138-
Extract(maybe_bearer_token): Extract,
138+
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
139139
) -> Response {
140140
// todo: add reason and `date_expiry` parameters to request
141141

142-
let user_id = match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await {
143-
Ok(user_id) => user_id,
144-
Err(error) => return error.into_response(),
145-
};
146-
147142
match app_data.ban_service.ban_user(&to_be_banned_username.0, &user_id).await {
148143
Ok(()) => Json(OkResponseData {
149144
data: format!("Banned user: {}", to_be_banned_username.0),

0 commit comments

Comments
 (0)
Please sign in to comment.