Skip to content

Commit 957475a

Browse files
committed
reload_auth_user
1 parent 7eaf227 commit 957475a

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

lib/api_run/src/run.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ async fn post_inner(
6666
};
6767

6868
let query_organization = query_project.organization(conn_lock!(context))?;
69+
let is_claimed = query_organization.is_claimed(conn_lock!(context))?;
6970
// If the organization is claimed, check permissions
70-
if query_organization.is_claimed(conn_lock!(context))? {
71+
if is_claimed {
7172
if let Some(auth_user) = auth_user.as_ref() {
72-
query_project.try_allowed(&context.rbac, auth_user, Permission::Create)?;
73+
// If the user is authenticated, then we may have created a new role for them.
74+
// If so then we need to reload the permissions.
75+
let auth_user = auth_user.reload(conn_lock!(context))?;
76+
query_project.try_allowed(&context.rbac, &auth_user, Permission::Create)?;
7377
} else {
7478
return Err(unauthorized_error(format!(
7579
"This project ({}) has already been claimed. Provide a valid API token (`--token`) to authenticate.",

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ impl QueryProject {
159159
let insert_project =
160160
InsertProject::new(query_organization.id, name, project_slug, None, None);
161161
if let Some(auth_user) = auth_user {
162-
Self::create(log, context, auth_user, &query_organization, insert_project).await
162+
// If the user is authenticated, then we may have created a new personal organization for them.
163+
// If so then we need to reload the permissions.
164+
// This is unlikely to be the case going forward, but it is needed for backwards compatibility.
165+
let auth_user = auth_user.reload(conn_lock!(context))?;
166+
Self::create(
167+
log,
168+
context,
169+
&auth_user,
170+
&query_organization,
171+
insert_project,
172+
)
173+
.await
163174
} else {
164175
Self::create_inner(log, context, &query_organization, insert_project).await
165176
}

lib/bencher_schema/src/model/user/auth.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Deref;
33
use async_trait::async_trait;
44
#[cfg(feature = "plus")]
55
use bencher_json::system::payment::JsonCustomer;
6-
use bencher_json::{Email, Jwt, Sanitize};
6+
use bencher_json::{Jwt, Sanitize};
77
use bencher_rbac::{
88
server::Permission,
99
user::{OrganizationRoles, ProjectRoles},
@@ -24,7 +24,7 @@ use crate::{
2424
schema,
2525
};
2626

27-
use super::{QueryUser, UserId};
27+
use super::QueryUser;
2828

2929
#[derive(Debug, Clone)]
3030
pub struct AuthUser {
@@ -71,10 +71,18 @@ impl AuthUser {
7171
// Hold the connection for all permissions related queries
7272
let conn = conn_lock!(context);
7373
let query_user = QueryUser::get_with_email(conn, email)?;
74+
Self::load(conn, query_user)
75+
}
76+
77+
pub fn reload(&self, conn: &mut DbConnection) -> Result<Self, HttpError> {
78+
Self::load(conn, self.user.clone())
79+
}
80+
81+
fn load(conn: &mut DbConnection, query_user: QueryUser) -> Result<Self, HttpError> {
7482
query_user.check_is_locked()?;
7583

76-
let (org_ids, org_roles) = Self::organization_roles(conn, query_user.id, email)?;
77-
let (proj_ids, proj_roles) = Self::project_roles(conn, query_user.id, email)?;
84+
let (org_ids, org_roles) = Self::organization_roles(conn, &query_user)?;
85+
let (proj_ids, proj_roles) = Self::project_roles(conn, &query_user)?;
7886

7987
let rbac = RbacUser {
8088
admin: query_user.admin,
@@ -93,11 +101,10 @@ impl AuthUser {
93101

94102
fn organization_roles(
95103
conn: &mut DbConnection,
96-
user_id: UserId,
97-
email: &Email,
104+
query_user: &QueryUser,
98105
) -> Result<(Vec<OrganizationId>, OrganizationRoles), HttpError> {
99106
let roles = schema::organization_role::table
100-
.filter(schema::organization_role::user_id.eq(user_id))
107+
.filter(schema::organization_role::user_id.eq(query_user.id))
101108
.order(schema::organization_role::organization_id)
102109
.select((
103110
schema::organization_role::organization_id,
@@ -107,7 +114,10 @@ impl AuthUser {
107114
.map_err(|e| {
108115
crate::error::issue_error(
109116
"User can't query organization roles",
110-
&format!("My user ({email}) on Bencher failed to query organization roles."),
117+
&format!(
118+
"My user ({email}) on Bencher failed to query organization roles.",
119+
email = query_user.email
120+
),
111121
e,
112122
)
113123
})?;
@@ -120,7 +130,7 @@ impl AuthUser {
120130
Err(e) => {
121131
let _err = crate::error::issue_error(
122132
"Failed to parse organization role",
123-
&format!("My user ({email}) on Bencher has an invalid organization role ({role})."),
133+
&format!("My user ({email}) on Bencher has an invalid organization role ({role}).", email = query_user.email),
124134
e,
125135
);
126136
None
@@ -133,11 +143,10 @@ impl AuthUser {
133143

134144
fn project_roles(
135145
conn: &mut DbConnection,
136-
user_id: UserId,
137-
email: &Email,
146+
query_user: &QueryUser,
138147
) -> Result<(Vec<OrgProjectId>, ProjectRoles), HttpError> {
139148
let roles = schema::project_role::table
140-
.filter(schema::project_role::user_id.eq(user_id))
149+
.filter(schema::project_role::user_id.eq(query_user.id))
141150
.inner_join(schema::project::table)
142151
.order(schema::project_role::project_id)
143152
.select((
@@ -149,7 +158,10 @@ impl AuthUser {
149158
.map_err(|e| {
150159
crate::error::issue_error(
151160
"User can't query project roles",
152-
&format!("My user ({email}) on Bencher failed to query project roles."),
161+
&format!(
162+
"My user ({email}) on Bencher failed to query project roles.",
163+
email = query_user.email
164+
),
153165
e,
154166
)
155167
})?;
@@ -169,7 +181,8 @@ impl AuthUser {
169181
let _err = crate::error::issue_error(
170182
"Failed to parse project role",
171183
&format!(
172-
"My user ({email}) on Bencher has an invalid project role ({role})."
184+
"My user ({email}) on Bencher has an invalid project role ({role}).",
185+
email = query_user.email
173186
),
174187
e,
175188
);

0 commit comments

Comments
 (0)