Skip to content

Commit e13f579

Browse files
committed
feat: [#615] authorization service implemented for ban user handler
1 parent ba0ad37 commit e13f579

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

casbin/policy.csv

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ p, true, GetSettings
44
p, true, GetSettingsSecret
55
p, true, AddTag
66
p, true, DeleteTag
7-
p, true, DeleteTorrent
7+
p, true, DeleteTorrent
8+
p, true, BanUser

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
129129
user_authentication_repository.clone(),
130130
));
131131
let ban_service = Arc::new(user::BanService::new(
132-
user_repository.clone(),
133132
user_profile_repository.clone(),
134133
banned_user_list.clone(),
134+
authorization_service.clone(),
135135
));
136136
let authentication_service = Arc::new(Service::new(
137137
configuration.clone(),

src/services/user.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use pbkdf2::password_hash::rand_core::OsRng;
1111
use tracing::{debug, info};
1212

1313
use super::authentication::DbUserAuthenticationRepository;
14+
use super::authorization::{self, ACTION};
1415
use crate::config::{Configuration, EmailOnSignup, PasswordConstraints};
1516
use crate::databases::database::{Database, Error};
1617
use crate::errors::ServiceError;
@@ -237,22 +238,22 @@ impl ProfileService {
237238
}
238239

239240
pub struct BanService {
240-
user_repository: Arc<Box<dyn Repository>>,
241241
user_profile_repository: Arc<DbUserProfileRepository>,
242242
banned_user_list: Arc<DbBannedUserList>,
243+
authorization_service: Arc<authorization::Service>,
243244
}
244245

245246
impl BanService {
246247
#[must_use]
247248
pub fn new(
248-
user_repository: Arc<Box<dyn Repository>>,
249249
user_profile_repository: Arc<DbUserProfileRepository>,
250250
banned_user_list: Arc<DbBannedUserList>,
251+
authorization_service: Arc<authorization::Service>,
251252
) -> Self {
252253
Self {
253-
user_repository,
254254
user_profile_repository,
255255
banned_user_list,
256+
authorization_service,
256257
}
257258
}
258259

@@ -268,12 +269,7 @@ impl BanService {
268269
pub async fn ban_user(&self, username_to_be_banned: &str, user_id: &UserId) -> Result<(), ServiceError> {
269270
debug!("user with ID {user_id} banning username: {username_to_be_banned}");
270271

271-
let user = self.user_repository.get_compact(user_id).await?;
272-
273-
// Check if user is administrator
274-
if !user.administrator {
275-
return Err(ServiceError::Unauthorized);
276-
}
272+
self.authorization_service.authorize(ACTION::BanUser, Some(*user_id)).await?;
277273

278274
let user_profile = self
279275
.user_profile_repository

0 commit comments

Comments
 (0)