@@ -19,6 +19,7 @@ use crate::service::rating::{
1919 assert_user_is_rating_creator_and_organisation_member,
2020} ;
2121use crate :: service:: role:: user_is_role_admin;
22+ use crate :: service:: comment:: user_is_comment_author;
2223use axum:: extract:: { FromRef , FromRequestParts , Path } ;
2324use axum:: http:: request:: Parts ;
2425use axum:: response:: { IntoResponse , Redirect , Response } ;
@@ -629,6 +630,50 @@ where
629630 }
630631}
631632
633+ /// Comment author information for a specific comment.
634+ ///
635+ /// Ensures the request user is the author of the given comment.
636+ pub struct CommentAuthorGivenApplicationAndCommentId {
637+ /// ID of the comment author
638+ pub user_id : i64 ,
639+ }
640+
641+ /// Extractor for comment authors.
642+ ///
643+ /// This extractor validates that the authenticated user is the `comments.author_id`
644+ /// for the provided `(application_id, comment_id)` route parameters.
645+ #[ async_trait]
646+ impl < S > FromRequestParts < S > for CommentAuthorGivenApplicationAndCommentId
647+ where
648+ AppState : FromRef < S > ,
649+ S : Send + Sync ,
650+ {
651+ type Rejection = ChaosError ;
652+
653+ async fn from_request_parts ( parts : & mut Parts , state : & S ) -> Result < Self , Self :: Rejection > {
654+ let app_state = AppState :: from_ref ( state) ;
655+ let user_id = extract_user_id_from_request ( parts, & app_state) . await ?;
656+
657+ let Path ( ids) = parts
658+ . extract :: < Path < HashMap < String , i64 > > > ( )
659+ . await
660+ . map_err ( |_| ChaosError :: BadRequest ) ?;
661+
662+ let comment_id = ids. get ( "comment_id" ) . ok_or ( ChaosError :: BadRequest ) ?. clone ( ) ;
663+
664+ let mut tx = app_state. db . begin ( ) . await ?;
665+ let is_owner = user_is_comment_author ( user_id, comment_id, & mut tx) . await ?;
666+
667+ if !is_owner {
668+ return Err ( ChaosError :: Unauthorized ) ;
669+ }
670+
671+ tx. commit ( ) . await ?;
672+
673+ Ok ( CommentAuthorGivenApplicationAndCommentId { user_id } )
674+ }
675+ }
676+
632677/// Application owner or a reviewer (member of organisation that application was for).
633678///
634679/// Contains the user ID of a user who owns a specific application.
@@ -721,7 +766,7 @@ pub struct OfferAdmin {
721766}
722767
723768/// Extractor for offer administrators.
724- ///
769+ ///
725770/// This extractor is used in route handlers to ensure that the request
726771/// comes from a user with offer administrator privileges.
727772#[ async_trait]
0 commit comments