11use crate :: model:: additional_resource:: {
22 AdditionalResource , NewAdditionalResource , PatchAdditionalResource ,
33} ;
4+ use crate :: model:: file:: File ;
45use crate :: model:: work:: { Work , WorkType } ;
56use crate :: model:: Crud ;
67use crate :: policy:: { CreatePolicy , DeletePolicy , MovePolicy , PolicyContext , UpdatePolicy } ;
78use thoth_errors:: { ThothError , ThothResult } ;
89
910/// Write policies for `AdditionalResource`.
1011///
11- /// These policies enforce publisher scoping and prevent attachment to chapter records.
12+ /// These policies are responsible for:
13+ /// - enforcing publisher scoping
14+ /// - preventing attachment to chapter records
15+ /// - preventing manual update of auto-generated Thoth Hosting URLs
1216pub struct AdditionalResourcePolicy ;
1317
1418fn ensure_work_is_book ( db : & crate :: db:: PgPool , work_id : uuid:: Uuid ) -> ThothResult < ( ) > {
@@ -20,6 +24,18 @@ fn ensure_work_is_book(db: &crate::db::PgPool, work_id: uuid::Uuid) -> ThothResu
2024 }
2125}
2226
27+ fn ensure_no_hosted_file (
28+ db : & crate :: db:: PgPool ,
29+ additional_resource_id : uuid:: Uuid ,
30+ ) -> ThothResult < ( ) > {
31+ let file = File :: from_additional_resource_id ( db, & additional_resource_id) ?;
32+ if file. is_some ( ) {
33+ Err ( ThothError :: HostedFileUrlEditError )
34+ } else {
35+ Ok ( ( ) )
36+ }
37+ }
38+
2339impl CreatePolicy < NewAdditionalResource > for AdditionalResourcePolicy {
2440 fn can_create < C : PolicyContext > (
2541 ctx : & C ,
@@ -41,7 +57,12 @@ impl UpdatePolicy<AdditionalResource, PatchAdditionalResource> for AdditionalRes
4157 ctx. require_publisher_for ( current) ?;
4258 ctx. require_publisher_for ( patch) ?;
4359 ensure_work_is_book ( ctx. db ( ) , current. work_id ) ?;
44- ensure_work_is_book ( ctx. db ( ) , patch. work_id )
60+ ensure_work_is_book ( ctx. db ( ) , patch. work_id ) ?;
61+
62+ if patch. url != current. url {
63+ ensure_no_hosted_file ( ctx. db ( ) , current. additional_resource_id ) ?;
64+ }
65+ Ok ( ( ) )
4566 }
4667}
4768
0 commit comments