Skip to content

Commit 1342dce

Browse files
committed
feat: [#615] authorization layer implemented for the get torrents handler
1 parent f264e75 commit 1342dce

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/services/authorization.rs

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub enum ACTION {
4848
GetTorrent,
4949
AddTorrent,
5050
GetCanonicalInfoHash,
51+
GenerateTorrentInfoListing,
5152
}
5253

5354
pub struct Service {
@@ -190,20 +191,23 @@ impl CasbinConfiguration {
190191
admin, GetTorrent
191192
admin, AddTorrent
192193
admin, GetCanonicalInfoHash
194+
admin, GenerateTorrentInfoListing
193195
registered, GetCategories
194196
registered, GetImageByUrl
195197
registered, GetPublicSettings
196198
registered, GetTags
197199
registered, GetTorrent
198200
registered, AddTorrent
199201
registered, GetCanonicalInfoHash
202+
registered, GenerateTorrentInfoListing
200203
guest, GetCategories
201204
guest, GetTags
202205
guest, GetAboutPage
203206
guest, GetLicensePage
204207
guest, GetPublicSettings
205208
guest, GetTorrent
206209
guest, GetCanonicalInfoHash
210+
guest, GenerateTorrentInfoListing
207211
",
208212
),
209213
}

src/services/torrent.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,15 @@ impl Index {
347347
/// # Errors
348348
///
349349
/// Returns a `ServiceError::DatabaseError` if the database query fails.
350-
pub async fn generate_torrent_info_listing(&self, request: &ListingRequest) -> Result<TorrentsResponse, ServiceError> {
350+
pub async fn generate_torrent_info_listing(
351+
&self,
352+
request: &ListingRequest,
353+
opt_user_id: Option<UserId>,
354+
) -> Result<TorrentsResponse, ServiceError> {
355+
self.authorization_service
356+
.authorize(ACTION::GenerateTorrentInfoListing, opt_user_id)
357+
.await?;
358+
351359
let torrent_listing_specification = self.listing_specification_from_user_request(request).await;
352360

353361
let torrents_response = self

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ async fn redirect_to_download_url_using_canonical_info_hash_if_needed(
131131
///
132132
/// It returns an error if the database query fails.
133133
#[allow(clippy::unused_async)]
134-
pub async fn get_torrents_handler(State(app_data): State<Arc<AppData>>, Query(criteria): Query<ListingRequest>) -> Response {
135-
match app_data.torrent_service.generate_torrent_info_listing(&criteria).await {
134+
pub async fn get_torrents_handler(
135+
State(app_data): State<Arc<AppData>>,
136+
Query(criteria): Query<ListingRequest>,
137+
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
138+
) -> Response {
139+
match app_data
140+
.torrent_service
141+
.generate_torrent_info_listing(&criteria, opt_user_id)
142+
.await
143+
{
136144
Ok(torrents_response) => Json(OkResponseData { data: torrents_response }).into_response(),
137145
Err(error) => error.into_response(),
138146
}

0 commit comments

Comments
 (0)