@@ -325,7 +325,7 @@ impl DataStore {
325
325
///
326
326
/// This function does no authz checks because it is not possible to know
327
327
/// just by looking up an Organization's id what privileges are required.
328
- pub async fn organization_lookup_id (
328
+ pub async fn organization_lookup_path (
329
329
& self ,
330
330
name : & Name ,
331
331
) -> LookupResult < authz:: Organization > {
@@ -340,8 +340,6 @@ impl DataStore {
340
340
) -> LookupResult < ( authz:: Organization , Organization ) > {
341
341
let ( authz_org, db_org) =
342
342
self . organization_lookup_noauthz ( name) . await ?;
343
- // TODO-security See the note in authz::authorize(). This needs to
344
- // return a 404, not a 403.
345
343
opctx. authorize ( authz:: Action :: Read , & authz_org) . await ?;
346
344
Ok ( ( authz_org, db_org) )
347
345
}
@@ -475,7 +473,7 @@ impl DataStore {
475
473
) -> UpdateResult < Organization > {
476
474
use db:: schema:: organization:: dsl;
477
475
478
- let authz_org = self . organization_lookup_id ( name) . await ?;
476
+ let authz_org = self . organization_lookup_path ( name) . await ?;
479
477
opctx. authorize ( authz:: Action :: Modify , & authz_org) . await ?;
480
478
481
479
diesel:: update ( dsl:: organization)
@@ -528,29 +526,72 @@ impl DataStore {
528
526
} )
529
527
}
530
528
531
- /// Lookup a project by name.
532
- pub async fn project_fetch (
529
+ /// Fetches a Project from the database and returns both the database row
530
+ /// and an authz::Project for doing authz checks
531
+ ///
532
+ /// See [`DataStore::organization_lookup_noauthz()`] for intended use cases
533
+ /// and caveats.
534
+ // TODO-security See the note on organization_lookup_noauthz().
535
+ async fn project_lookup_noauthz (
533
536
& self ,
534
- organization_id : & Uuid ,
535
- name : & Name ,
536
- ) -> LookupResult < Project > {
537
+ authz_org : & authz :: Organization ,
538
+ project_name : & Name ,
539
+ ) -> LookupResult < ( authz :: Project , Project ) > {
537
540
use db:: schema:: project:: dsl;
538
541
dsl:: project
539
542
. filter ( dsl:: time_deleted. is_null ( ) )
540
- . filter ( dsl:: organization_id. eq ( * organization_id ) )
541
- . filter ( dsl:: name. eq ( name . clone ( ) ) )
543
+ . filter ( dsl:: organization_id. eq ( authz_org . id ( ) ) )
544
+ . filter ( dsl:: name. eq ( project_name . clone ( ) ) )
542
545
. select ( Project :: as_select ( ) )
543
546
. first_async ( self . pool ( ) )
544
547
. await
545
548
. map_err ( |e| {
546
549
public_error_from_diesel_pool (
547
550
e,
548
551
ResourceType :: Project ,
549
- LookupType :: ByName ( name. as_str ( ) . to_owned ( ) ) ,
552
+ LookupType :: ByName ( project_name. as_str ( ) . to_owned ( ) ) ,
553
+ )
554
+ } )
555
+ . map ( |p| {
556
+ (
557
+ authz_org
558
+ . project ( p. id ( ) , LookupType :: from ( & project_name. 0 ) ) ,
559
+ p,
550
560
)
551
561
} )
552
562
}
553
563
564
+ /// Look up the id for a Project based on its name
565
+ ///
566
+ /// Returns an [`authz::Project`] (which makes the id available).
567
+ ///
568
+ /// This function does no authz checks because it is not possible to know
569
+ /// just by looking up an Project's id what privileges are required.
570
+ pub async fn project_lookup_path (
571
+ & self ,
572
+ organization_name : & Name ,
573
+ project_name : & Name ,
574
+ ) -> LookupResult < authz:: Project > {
575
+ let authz_org =
576
+ self . organization_lookup_path ( organization_name) . await ?;
577
+ self . project_lookup_noauthz ( & authz_org, project_name)
578
+ . await
579
+ . map ( |( p, _) | p)
580
+ }
581
+
582
+ /// Lookup a project by name.
583
+ pub async fn project_fetch (
584
+ & self ,
585
+ opctx : & OpContext ,
586
+ authz_org : & authz:: Organization ,
587
+ name : & Name ,
588
+ ) -> LookupResult < ( authz:: Project , Project ) > {
589
+ let ( authz_org, db_org) =
590
+ self . project_lookup_noauthz ( authz_org, name) . await ?;
591
+ opctx. authorize ( authz:: Action :: Read , & authz_org) . await ?;
592
+ Ok ( ( authz_org, db_org) )
593
+ }
594
+
554
595
/// Delete a project
555
596
/*
556
597
* TODO-correctness This needs to check whether there are any resources that
@@ -582,29 +623,6 @@ impl DataStore {
582
623
Ok ( ( ) )
583
624
}
584
625
585
- /// Look up the id for a project based on its name
586
- pub async fn project_lookup_id_by_name (
587
- & self ,
588
- organization_id : & Uuid ,
589
- name : & Name ,
590
- ) -> Result < Uuid , Error > {
591
- use db:: schema:: project:: dsl;
592
- dsl:: project
593
- . filter ( dsl:: time_deleted. is_null ( ) )
594
- . filter ( dsl:: organization_id. eq ( * organization_id) )
595
- . filter ( dsl:: name. eq ( name. clone ( ) ) )
596
- . select ( dsl:: id)
597
- . get_result_async :: < Uuid > ( self . pool ( ) )
598
- . await
599
- . map_err ( |e| {
600
- public_error_from_diesel_pool (
601
- e,
602
- ResourceType :: Project ,
603
- LookupType :: ByName ( name. as_str ( ) . to_owned ( ) ) ,
604
- )
605
- } )
606
- }
607
-
608
626
pub async fn projects_list_by_id (
609
627
& self ,
610
628
opctx : & OpContext ,
0 commit comments