@@ -8,7 +8,7 @@ use bencher_json::{
88 DateTime , JsonNewOrganization , JsonOrganization , Jwt , OrganizationUuid , ResourceId ,
99 ResourceName , Slug ,
1010} ;
11- use bencher_rbac:: Organization ;
11+ use bencher_rbac:: { organization :: Permission , Organization } ;
1212use diesel:: { ExpressionMethods , QueryDsl , Queryable , RunQueryDsl } ;
1313use dropshot:: HttpError ;
1414use organization_role:: { InsertOrganizationRole , QueryOrganizationRole } ;
@@ -62,6 +62,7 @@ impl QueryOrganization {
6262 if let Ok ( query_organization) =
6363 Self :: from_resource_id ( conn_lock ! ( context) , & user_slug. clone ( ) . into ( ) )
6464 {
65+ query_organization. try_allowed ( & context. rbac , auth_user, Permission :: View ) ?;
6566 return Ok ( query_organization) ;
6667 }
6768
@@ -72,21 +73,17 @@ impl QueryOrganization {
7273 Self :: create ( context, auth_user, json_organization) . await
7374 }
7475
75- pub async fn get_or_create_from_context (
76+ pub async fn get_or_create_from_project (
7677 context : & ApiContext ,
7778 project_name : & ResourceName ,
7879 project_slug : & Slug ,
7980 ) -> Result < Self , HttpError > {
8081 if let Ok ( query_organization) =
8182 Self :: from_resource_id ( conn_lock ! ( context) , & project_slug. clone ( ) . into ( ) )
8283 {
83- // Get the total number of members for the organization
84- let total_members =
85- QueryOrganizationRole :: count ( conn_lock ! ( context) , query_organization. id ) ?;
86- // If the project is part of an organization that has zero members,
84+ // If the project is part of an organization that is unclaimed,
8785 // then the project can have anonymous reports.
88- // That is, the project has not yet been claimed.
89- return if total_members == 0 {
86+ return if query_organization. is_unclaimed ( conn_lock ! ( context) ) ? {
9087 Ok ( query_organization)
9188 } else {
9289 Err ( unauthorized_error ( format ! (
@@ -147,7 +144,7 @@ impl QueryOrganization {
147144 rbac : & Rbac ,
148145 organization : & ResourceId ,
149146 auth_user : & AuthUser ,
150- permission : bencher_rbac :: organization :: Permission ,
147+ permission : Permission ,
151148 ) -> Result < Self , HttpError > {
152149 // Do not leak information about organizations.
153150 // Always return the same error.
@@ -161,7 +158,7 @@ impl QueryOrganization {
161158 rbac : & Rbac ,
162159 organization : & ResourceId ,
163160 auth_user : & AuthUser ,
164- permission : bencher_rbac :: organization :: Permission ,
161+ permission : Permission ,
165162 ) -> Result < Self , HttpError > {
166163 let query_organization = Self :: from_resource_id ( conn, organization) ?;
167164 query_organization. try_allowed ( rbac, auth_user, permission) ?;
@@ -173,7 +170,7 @@ impl QueryOrganization {
173170 rbac : & Rbac ,
174171 organization_id : OrganizationId ,
175172 auth_user : & AuthUser ,
176- permission : bencher_rbac :: organization :: Permission ,
173+ permission : Permission ,
177174 ) -> Result < Self , HttpError > {
178175 // Do not leak information about organizations.
179176 // Always return the same error.
@@ -189,7 +186,7 @@ impl QueryOrganization {
189186 rbac : & Rbac ,
190187 organization_id : OrganizationId ,
191188 auth_user : & AuthUser ,
192- permission : bencher_rbac :: organization :: Permission ,
189+ permission : Permission ,
193190 ) -> Result < Self , HttpError > {
194191 let query_organization = Self :: get ( conn, organization_id) ?;
195192 query_organization. try_allowed ( rbac, auth_user, permission) ?;
@@ -200,12 +197,18 @@ impl QueryOrganization {
200197 & self ,
201198 rbac : & Rbac ,
202199 auth_user : & AuthUser ,
203- permission : bencher_rbac :: organization :: Permission ,
200+ permission : Permission ,
204201 ) -> Result < ( ) , HttpError > {
205202 rbac. is_allowed_organization ( auth_user, permission, self )
206203 . map_err ( forbidden_error)
207204 }
208205
206+ pub fn is_unclaimed ( & self , conn : & mut DbConnection ) -> Result < bool , HttpError > {
207+ let total_members = QueryOrganizationRole :: count ( conn, self . id ) ?;
208+ // If the organization that has zero members, then it is unclaimed.
209+ Ok ( total_members == 0 )
210+ }
211+
209212 pub fn into_json ( self ) -> JsonOrganization {
210213 let Self {
211214 uuid,
0 commit comments