Skip to content

Commit e614e2f

Browse files
committed
feat: [#615] authorization layer added to the change password method of the user service
1 parent 543d804 commit e614e2f

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
130130
let profile_service = Arc::new(user::ProfileService::new(
131131
configuration.clone(),
132132
user_authentication_repository.clone(),
133+
authorization_service.clone(),
133134
));
134135
let ban_service = Arc::new(user::BanService::new(
135136
user_profile_repository.clone(),

src/services/authorization.rs

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub enum ACTION {
5050
GetCanonicalInfoHash,
5151
GenerateTorrentInfoListing,
5252
GetTorrentInfo,
53+
ChangePassword,
5354
}
5455

5556
pub struct Service {
@@ -194,6 +195,7 @@ impl CasbinConfiguration {
194195
admin, GetCanonicalInfoHash
195196
admin, GenerateTorrentInfoListing
196197
admin, GetTorrentInfo
198+
admin, ChangePassword
197199
registered, GetCategories
198200
registered, GetImageByUrl
199201
registered, GetPublicSettings
@@ -203,6 +205,7 @@ impl CasbinConfiguration {
203205
registered, GetCanonicalInfoHash
204206
registered, GenerateTorrentInfoListing
205207
registered, GetTorrentInfo
208+
registered, ChangePassword
206209
guest, GetCategories
207210
guest, GetTags
208211
guest, GetAboutPage

src/services/user.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,20 @@ impl RegistrationService {
200200
pub struct ProfileService {
201201
configuration: Arc<Configuration>,
202202
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
203+
authorization_service: Arc<authorization::Service>,
203204
}
204205

205206
impl ProfileService {
206207
#[must_use]
207-
pub fn new(configuration: Arc<Configuration>, user_repository: Arc<DbUserAuthenticationRepository>) -> Self {
208+
pub fn new(
209+
configuration: Arc<Configuration>,
210+
user_repository: Arc<DbUserAuthenticationRepository>,
211+
authorization_service: Arc<authorization::Service>,
212+
) -> Self {
208213
Self {
209214
configuration,
210215
user_authentication_repository: user_repository,
216+
authorization_service,
211217
}
212218
}
213219

@@ -223,7 +229,16 @@ impl ProfileService {
223229
/// * `ServiceError::PasswordTooLong` if the supplied password is too long.
224230
/// * An error if unable to successfully hash the password.
225231
/// * An error if unable to change the password in the database.
226-
pub async fn change_password(&self, user_id: UserId, change_password_form: &ChangePasswordForm) -> Result<(), ServiceError> {
232+
pub async fn change_password(
233+
&self,
234+
user_id: UserId,
235+
change_password_form: &ChangePasswordForm,
236+
maybe_user_id: Option<UserId>,
237+
) -> Result<(), ServiceError> {
238+
self.authorization_service
239+
.authorize(ACTION::ChangePassword, maybe_user_id)
240+
.await?;
241+
227242
info!("changing user password for user ID: {user_id}");
228243

229244
let settings = self.configuration.settings.read().await;

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::Deserialize;
1010
use super::forms::{ChangePasswordForm, JsonWebToken, LoginForm, RegistrationForm};
1111
use super::responses::{self};
1212
use crate::common::AppData;
13+
use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLoggedInUser;
1314
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1415
use crate::web::api::server::v1::responses::OkResponseData;
1516

@@ -133,10 +134,15 @@ pub async fn renew_token_handler(
133134
#[allow(clippy::unused_async)]
134135
pub async fn change_password_handler(
135136
State(app_data): State<Arc<AppData>>,
137+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
136138
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
137139
extract::Json(change_password_form): extract::Json<ChangePasswordForm>,
138140
) -> Response {
139-
match app_data.profile_service.change_password(user_id, &change_password_form).await {
141+
match app_data
142+
.profile_service
143+
.change_password(user_id, &change_password_form, maybe_user_id)
144+
.await
145+
{
140146
Ok(()) => Json(OkResponseData {
141147
data: format!("Password changed for user with ID: {user_id}"),
142148
})

0 commit comments

Comments
 (0)