Skip to content

Commit ad7573c

Browse files
committed
user_org
1 parent d49fdbb commit ad7573c

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

lib/api_organizations/src/organizations.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bencher_schema::{
1212
context::ApiContext,
1313
error::{resource_conflict_err, resource_not_found_err},
1414
model::{
15-
organization::{QueryOrganization, UpdateOrganization},
15+
organization::{InsertOrganization, QueryOrganization, UpdateOrganization},
1616
user::auth::{AuthUser, BearerToken},
1717
},
1818
schema,
@@ -180,8 +180,10 @@ async fn post_inner(
180180
json_organization: JsonNewOrganization,
181181
auth_user: &AuthUser,
182182
) -> Result<JsonOrganization, HttpError> {
183+
let insert_organization =
184+
InsertOrganization::from_json(conn_lock!(context), json_organization)?;
183185
let query_organization =
184-
QueryOrganization::create(context, auth_user, json_organization).await?;
186+
QueryOrganization::create(context, auth_user, insert_organization).await?;
185187
Ok(query_organization.into_json())
186188
}
187189

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use crate::{
2828
ApiContext,
2929
};
3030

31+
use super::user::QueryUser;
32+
3133
pub mod member;
3234
pub mod organization_role;
3335
pub mod plan;
@@ -66,11 +68,9 @@ impl QueryOrganization {
6668
return Ok(query_organization);
6769
}
6870

69-
let json_organization = JsonNewOrganization {
70-
name: auth_user.user.name.clone().into(),
71-
slug: Some(user_slug.clone()),
72-
};
73-
Self::create(context, auth_user, json_organization).await
71+
let insert_organization =
72+
InsertOrganization::new(auth_user.user.name.clone().into(), user_slug.clone());
73+
Self::create(context, auth_user, insert_organization).await
7474
}
7575

7676
pub async fn get_or_create_from_project(
@@ -92,19 +92,32 @@ impl QueryOrganization {
9292
};
9393
}
9494

95-
let json_organization = JsonNewOrganization {
96-
name: project_name.clone(),
97-
slug: Some(project_slug.clone()),
98-
};
99-
Self::create_inner(context, json_organization).await
95+
let insert_organization =
96+
InsertOrganization::new(project_name.clone(), project_slug.clone());
97+
Self::create_inner(context, insert_organization).await
10098
}
10199

102100
pub async fn create(
103101
context: &ApiContext,
104102
auth_user: &AuthUser,
105-
json_organization: JsonNewOrganization,
103+
insert_organization: InsertOrganization,
106104
) -> Result<Self, HttpError> {
107-
let query_organization = Self::create_inner(context, json_organization).await?;
105+
// Don't allow other users to create an organization with the same slug as another user.
106+
// This is needed to make on-the-fly projects for an authenticated user work.
107+
if insert_organization.slug != auth_user.user.slug
108+
&& !auth_user.is_admin(&context.rbac)
109+
&& QueryUser::from_resource_id(
110+
conn_lock!(context),
111+
&insert_organization.slug.clone().into(),
112+
)
113+
.is_ok()
114+
{
115+
return Err(forbidden_error(
116+
"You cannot create an organization with the same slug as your user.",
117+
));
118+
}
119+
120+
let query_organization = Self::create_inner(context, insert_organization).await?;
108121

109122
let timestamp = DateTime::now();
110123
// Connect the user to the organization as a `Leader`
@@ -125,10 +138,8 @@ impl QueryOrganization {
125138

126139
async fn create_inner(
127140
context: &ApiContext,
128-
json_organization: JsonNewOrganization,
141+
insert_organization: InsertOrganization,
129142
) -> Result<Self, HttpError> {
130-
let insert_organization =
131-
InsertOrganization::from_json(conn_lock!(context), json_organization)?;
132143
diesel::insert_into(schema::organization::table)
133144
.values(&insert_organization)
134145
.execute(conn_lock!(context))
@@ -243,31 +254,29 @@ pub struct InsertOrganization {
243254
}
244255

245256
impl InsertOrganization {
246-
pub fn from_json(
247-
conn: &mut DbConnection,
248-
organization: JsonNewOrganization,
249-
) -> Result<Self, HttpError> {
250-
let JsonNewOrganization { name, slug } = organization;
251-
let slug = ok_slug!(conn, &name, slug, organization, QueryOrganization)?;
257+
fn new(name: ResourceName, slug: Slug) -> Self {
252258
let timestamp = DateTime::now();
253-
Ok(Self {
259+
Self {
254260
uuid: OrganizationUuid::new(),
255261
name,
256262
slug,
257263
created: timestamp,
258264
modified: timestamp,
259-
})
265+
}
266+
}
267+
268+
pub fn from_json(
269+
conn: &mut DbConnection,
270+
organization: JsonNewOrganization,
271+
) -> Result<Self, HttpError> {
272+
let JsonNewOrganization { name, slug } = organization;
273+
let slug = ok_slug!(conn, &name, slug, organization, QueryOrganization)?;
274+
Ok(Self::new(name, slug))
260275
}
261276

262277
pub fn from_user(insert_user: &InsertUser) -> Self {
263-
let timestamp = DateTime::now();
264-
Self {
265-
uuid: OrganizationUuid::new(),
266-
name: insert_user.name.clone().into(),
267-
slug: insert_user.slug.clone(),
268-
created: timestamp,
269-
modified: timestamp,
270-
}
278+
let InsertUser { name, slug, .. } = insert_user;
279+
Self::new(name.clone().into(), slug.clone())
271280
}
272281
}
273282

0 commit comments

Comments
 (0)