@@ -13,9 +13,8 @@ use crate::{
1313 conn_lock,
1414 context:: { DbConnection , Rbac } ,
1515 error:: {
16- assert_parentage, bad_request_error, conflict_error, forbidden_error,
17- resource_conflict_err, resource_not_found_err, resource_not_found_error,
18- unauthorized_error, BencherResource ,
16+ assert_parentage, bad_request_error, forbidden_error, resource_conflict_err,
17+ resource_not_found_error, unauthorized_error, BencherResource ,
1918 } ,
2019 macros:: {
2120 fn_get:: { fn_from_uuid, fn_get, fn_get_uuid} ,
@@ -75,23 +74,13 @@ impl QueryProject {
7574
7675 pub async fn get_or_create (
7776 context : & ApiContext ,
77+ auth_user : & AuthUser ,
7878 organization : & ResourceId ,
7979 project : & NameId ,
80- auth_user : & AuthUser ,
81- ) -> Result < ProjectId , HttpError > {
80+ ) -> Result < Self , HttpError > {
8281 let query_organization =
8382 QueryOrganization :: from_resource_id ( conn_lock ! ( context) , organization) ?;
84- let query_project =
85- Self :: get_or_create_inner ( context, & query_organization, project, auth_user) . await ?;
86- Ok ( query_project. id )
87- }
8883
89- async fn get_or_create_inner (
90- context : & ApiContext ,
91- query_organization : & QueryOrganization ,
92- project : & NameId ,
93- auth_user : & AuthUser ,
94- ) -> Result < Self , HttpError > {
9584 let Ok ( kind) = NameIdKind :: < ResourceName > :: try_from ( project) else {
9685 return Err ( bad_request_error ( format ! (
9786 "Project ({project}) must be a valid UUID, slug, or name"
@@ -115,7 +104,7 @@ impl QueryProject {
115104 url : None ,
116105 visibility : None ,
117106 } ;
118- Self :: create ( context, query_organization, new_project, auth_user) . await ?
107+ Self :: create ( context, & query_organization, new_project, auth_user) . await ?
119108 }
120109 } ,
121110 NameIdKind :: Name ( name) => {
@@ -132,7 +121,7 @@ impl QueryProject {
132121 url : None ,
133122 visibility : None ,
134123 } ;
135- Self :: create ( context, query_organization, new_project, auth_user) . await ?
124+ Self :: create ( context, & query_organization, new_project, auth_user) . await ?
136125 }
137126 } ,
138127 } ;
@@ -202,8 +191,7 @@ impl QueryProject {
202191 permission : Permission ,
203192 ) -> Result < Self , HttpError > {
204193 let query_project = Self :: from_resource_id ( conn, project) ?;
205- rbac. is_allowed_project ( auth_user, permission, & query_project)
206- . map_err ( forbidden_error) ?;
194+ query_project. try_allowed ( rbac, auth_user, permission) ?;
207195 Ok ( query_project)
208196 }
209197
@@ -234,14 +222,23 @@ impl QueryProject {
234222 } else if let Some ( auth_user) = auth_user {
235223 // If there is an `AuthUser` then validate access
236224 // Verify that the user is allowed
237- rbac. is_allowed_project ( auth_user, Permission :: View , & query_project)
238- . map_err ( forbidden_error) ?;
225+ query_project. try_allowed ( rbac, auth_user, Permission :: View ) ?;
239226 Ok ( query_project)
240227 } else {
241228 Err ( unauthorized_error ( project) )
242229 }
243230 }
244231
232+ pub fn try_allowed (
233+ & self ,
234+ rbac : & Rbac ,
235+ auth_user : & AuthUser ,
236+ permission : Permission ,
237+ ) -> Result < ( ) , HttpError > {
238+ rbac. is_allowed_project ( auth_user, permission, self )
239+ . map_err ( forbidden_error)
240+ }
241+
245242 #[ cfg( feature = "plus" ) ]
246243 pub fn perf_url ( & self , console_url : & url:: Url ) -> Result < Option < url:: Url > , HttpError > {
247244 if !self . is_public ( ) {
0 commit comments