Skip to content

Commit f092feb

Browse files
committed
Merge #721: Release Version 3.0.0-beta
5c0ae69 release: version 3.0.0-beta (Jose Celano) 1cec202 chore(deps): bump mockall from 0.12.1 to 0.13.0 (Mario) 10d9a10 deps: updated dependencies (Mario) ef72487 feat: added authorization logic to the get_site method (Mario) 2e4b8d6 refactor: removed unused Panic docs (Mario) 3e7fb4f refactor: added error handling for unsafe unwraps (Mario) a9ddb94 refactor: added error messages and panics section (Mario) 86c0fd5 refactor: removed clippy missing panics doc (Mario) 076566d develop: bump to version 3.0.0-beta-develop (Jose Celano) Pull request description: Release Version 3.0.0-beta ACKs for top commit: josecelano: ACK 5c0ae69 Tree-SHA512: 63a5d505628125762e6851469ed18c4cbf56300b00a66a97df5470162f54166f02baed830f5276f01067c9ccd9817f1ab2ef5db382dfc6cd79704ec6dacd0b30
2 parents 9da22d8 + 5c0ae69 commit f092feb

File tree

8 files changed

+200
-140
lines changed

8 files changed

+200
-140
lines changed

Cargo.lock

+157-118
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ license = "AGPL-3.0-only"
2727
publish = true
2828
repository = "https://github.com/torrust/torrust-tracker"
2929
rust-version = "1.72"
30-
version = "3.0.0-alpha.12"
30+
version = "3.0.0-beta"
3131

3232
[profile.dev.package.sqlx-macros]
3333
opt-level = 3
@@ -67,7 +67,7 @@ lettre = { version = "0", features = [
6767
"tokio1-rustls-tls",
6868
] }
6969
log = "0"
70-
mockall = "0.12.1"
70+
mockall = "0.13.0"
7171
pbkdf2 = { version = "0", features = ["simple"] }
7272
pin-project-lite = "0.2"
7373
rand = "0"
@@ -89,7 +89,7 @@ text-to-png = "0"
8989
thiserror = "1"
9090
tokio = { version = "1", features = ["fs", "io-util", "macros", "net", "rt-multi-thread", "signal", "sync", "time"] }
9191
toml = "0"
92-
torrust-index-located-error = { version = "3.0.0-alpha.12", path = "packages/located-error" }
92+
torrust-index-located-error = { version = "3.0.0-beta", path = "packages/located-error" }
9393
tower = { version = "0.4", features = ["timeout"] }
9494
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
9595
trace = "0.1.7"

src/services/authorization.rs

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub enum ACTION {
4040
GetSettings,
4141
GetSettingsSecret,
4242
GetPublicSettings,
43+
GetSiteName,
4344
AddTag,
4445
DeleteTag,
4546
GetTags,
@@ -236,6 +237,7 @@ impl Default for CasbinConfiguration {
236237
admin, GetSettings
237238
admin, GetSettingsSecret
238239
admin, GetPublicSettings
240+
admin, GetSiteName
239241
admin, AddTag
240242
admin, DeleteTag
241243
admin, GetTags
@@ -252,6 +254,7 @@ impl Default for CasbinConfiguration {
252254
registered, GetCategories
253255
registered, GetImageByUrl
254256
registered, GetPublicSettings
257+
registered, GetSiteName
255258
registered, GetTags
256259
registered, AddTorrent
257260
registered, GetTorrent
@@ -263,6 +266,7 @@ impl Default for CasbinConfiguration {
263266
guest, GetLicensePage
264267
guest, GetCategories
265268
guest, GetPublicSettings
269+
guest, GetSiteName
266270
guest, GetTags
267271
guest, GetTorrent
268272
guest, GetTorrentInfo

src/services/proxy.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ impl Service {
3838
/// * The image URL is not an image.
3939
/// * The image is too big.
4040
/// * The user quota is met.
41-
#[allow(clippy::missing_panics_doc)]
4241
pub async fn get_image_by_url(&self, url: &str, maybe_user_id: Option<UserId>) -> Result<Bytes, Error> {
42+
let Some(user_id) = maybe_user_id else {
43+
return Err(Error::Unauthenticated);
44+
};
45+
4346
self.authorization_service
4447
.authorize(ACTION::GetImageByUrl, maybe_user_id)
4548
.await
4649
.map_err(|_| Error::Unauthenticated)?;
4750

48-
// The unwrap should never panic as if the maybe_user_id is none, an authorization error will be returned and handled at the method above
49-
self.image_cache_service.get_image_by_url(url, maybe_user_id.unwrap()).await
51+
self.image_cache_service.get_image_by_url(url, user_id).await
5052
}
5153
}

src/services/settings.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ impl Service {
7676
/// # Errors
7777
///
7878
/// It returns an error if the user does not have the required permissions.
79-
pub async fn get_site_name(&self) -> String {
80-
self.configuration.get_site_name().await
79+
pub async fn get_site_name(&self, maybe_user_id: Option<UserId>) -> Result<String, ServiceError> {
80+
self.authorization_service
81+
.authorize(ACTION::GetSiteName, maybe_user_id)
82+
.await?;
83+
84+
Ok(self.configuration.get_site_name().await)
8185
}
8286
}
8387

src/services/torrent.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ impl Index {
134134
add_torrent_req: AddTorrentRequest,
135135
maybe_user_id: Option<UserId>,
136136
) -> Result<AddTorrentResponse, ServiceError> {
137+
let Some(user_id) = maybe_user_id else {
138+
return Err(ServiceError::UnauthorizedActionForGuests);
139+
};
140+
137141
self.authorization_service
138142
.authorize(ACTION::AddTorrent, maybe_user_id)
139143
.await?;
@@ -149,7 +153,7 @@ impl Index {
149153

150154
let torrent_id = self
151155
.torrent_repository
152-
.add(&original_info_hash, &torrent, &metadata, maybe_user_id.unwrap())
156+
.add(&original_info_hash, &torrent, &metadata, user_id)
153157
.await?;
154158

155159
// Synchronous secondary tasks

src/services/user.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,26 @@ impl ProfileService {
227227
/// * An error if unable to successfully hash the password.
228228
/// * An error if unable to change the password in the database.
229229
/// * An error if it is not possible to authorize the action
230-
#[allow(clippy::missing_panics_doc)]
231230
pub async fn change_password(
232231
&self,
233232
maybe_user_id: Option<UserId>,
234233
change_password_form: &ChangePasswordForm,
235234
) -> Result<(), ServiceError> {
235+
let Some(user_id) = maybe_user_id else {
236+
return Err(ServiceError::UnauthorizedActionForGuests);
237+
};
238+
236239
self.authorization_service
237240
.authorize(ACTION::ChangePassword, maybe_user_id)
238241
.await?;
239242

240-
info!("changing user password for user ID: {}", maybe_user_id.unwrap());
243+
info!("changing user password for user ID: {}", user_id);
241244

242245
let settings = self.configuration.settings.read().await;
243246

244247
let user_authentication = self
245248
.user_authentication_repository
246-
.get_user_authentication_from_id(&maybe_user_id.unwrap())
249+
.get_user_authentication_from_id(&user_id)
247250
.await?;
248251

249252
verify_password(change_password_form.current_password.as_bytes(), &user_authentication)?;
@@ -262,7 +265,7 @@ impl ProfileService {
262265
let password_hash = hash_password(&change_password_form.password)?;
263266

264267
self.user_authentication_repository
265-
.change_password(maybe_user_id.unwrap(), &password_hash)
268+
.change_password(user_id, &password_hash)
266269
.await?;
267270

268271
Ok(())
@@ -298,15 +301,15 @@ impl BanService {
298301
/// * `ServiceError::InternalServerError` if unable get user from the request.
299302
/// * An error if unable to get user profile from supplied username.
300303
/// * An error if unable to set the ban of the user in the database.
301-
#[allow(clippy::missing_panics_doc)]
302304
pub async fn ban_user(&self, username_to_be_banned: &str, maybe_user_id: Option<UserId>) -> Result<(), ServiceError> {
303-
debug!(
304-
"user with ID {} banning username: {username_to_be_banned}",
305-
maybe_user_id.unwrap()
306-
);
305+
let Some(user_id) = maybe_user_id else {
306+
return Err(ServiceError::UnauthorizedActionForGuests);
307+
};
307308

308309
self.authorization_service.authorize(ACTION::BanUser, maybe_user_id).await?;
309310

311+
debug!("user with ID {} banning username: {username_to_be_banned}", user_id);
312+
310313
let user_profile = self
311314
.user_profile_repository
312315
.get_user_profile_from_username(username_to_be_banned)

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ pub async fn get_public_handler(
4242

4343
/// Get website name.
4444
#[allow(clippy::unused_async)]
45-
pub async fn get_site_name_handler(State(app_data): State<Arc<AppData>>) -> Response {
46-
let site_name = app_data.settings_service.get_site_name().await;
47-
48-
Json(responses::OkResponseData { data: site_name }).into_response()
45+
pub async fn get_site_name_handler(
46+
State(app_data): State<Arc<AppData>>,
47+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
48+
) -> Response {
49+
match app_data.settings_service.get_site_name(maybe_user_id).await {
50+
Ok(site_name) => Json(responses::OkResponseData { data: site_name }).into_response(),
51+
Err(error) => error.into_response(),
52+
}
4953
}

0 commit comments

Comments
 (0)