@@ -33,6 +33,7 @@ import { StripeService } from "../billing/stripe-service";
3333import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution" ;
3434import { UsageService } from "./usage-service" ;
3535import { CostCenter_BillingStrategy } from "@gitpod/gitpod-protocol/lib/usage" ;
36+ import { UserAuthentication } from "../user/user-authentication" ;
3637
3738@injectable ( )
3839export class OrganizationService {
@@ -49,6 +50,7 @@ export class OrganizationService {
4950 @inject ( UsageService ) private readonly usageService : UsageService ,
5051 @inject ( DefaultWorkspaceImageValidator )
5152 private readonly validateDefaultWorkspaceImage : DefaultWorkspaceImageValidator ,
53+ @inject ( UserAuthentication ) private readonly userAuthentication : UserAuthentication ,
5254 ) { }
5355
5456 async listOrganizations (
@@ -145,6 +147,19 @@ export class OrganizationService {
145147 }
146148
147149 async createOrganization ( userId : string , name : string ) : Promise < Organization > {
150+ // TODO(gpl): Should we use the authorization layer to make this decision?
151+ const user = await this . userDB . findUserById ( userId ) ;
152+ if ( ! user ) {
153+ throw new ApplicationError ( ErrorCodes . NOT_AUTHENTICATED , `User not authenticated. Please login.` ) ;
154+ }
155+ const mayCreateOrganization = await this . userAuthentication . mayCreateOrJoinOrganization ( user ) ;
156+ if ( ! mayCreateOrganization ) {
157+ throw new ApplicationError (
158+ ErrorCodes . PERMISSION_DENIED ,
159+ "Organizational accounts are not allowed to create new organizations" ,
160+ ) ;
161+ }
162+
148163 let result : Organization ;
149164 try {
150165 result = await this . teamDB . transaction ( async ( db ) => {
0 commit comments