@@ -28,6 +28,8 @@ use crate::{
2828 ApiContext ,
2929} ;
3030
31+ use super :: user:: QueryUser ;
32+
3133pub mod member;
3234pub mod organization_role;
3335pub 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
245256impl 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