From 49f878a466207fa58bcc94d7132eaba3f776c187 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Tue, 3 Jan 2023 14:25:03 +0100 Subject: [PATCH] feat(api,ui): allow to disable project creation (#6403) --- engine/api/api.go | 4 + engine/api/config.go | 4 +- engine/api/project.go | 4 + sdk/config.go | 4 +- ui/src/app/model/config.service.ts | 2 + ui/src/app/views/navbar/navbar.component.ts | 14 +- ui/src/app/views/navbar/navbar.html | 80 ++++--- ui/src/app/views/navbar/navbar.scss | 239 ++++++++++---------- 8 files changed, 199 insertions(+), 152 deletions(-) diff --git a/engine/api/api.go b/engine/api/api.go index 316db0d0ac..98191b4936 100644 --- a/engine/api/api.go +++ b/engine/api/api.go @@ -220,6 +220,10 @@ type Configuration struct { DisablePurgeDeletion bool `toml:"disablePurgeDeletion" comment:"Allow you to disable the deletion part of the purge. Workflow run will only be marked as delete" json:"disablePurgeDeletion" default:"false"` TemplateBulkRunnerCount int64 `toml:"templateBulkRunnerCount" comment:"The count of runner that will execute the workflow template bulk operation." json:"templateBulkRunnerCount" default:"10"` } `toml:"workflow" comment:"######################\n 'Workflow' global configuration \n######################" json:"workflow"` + Project struct { + CreationDisabled bool `toml:"creationDisabled" comment:"Disable project creation for CDS non admin users." json:"creationDisabled" default:"false" commented:"true"` + InfoCreationDisabled string `toml:"infoCreationDisabled" comment:"Optional message to display if project creation is disabled." json:"infoCreationDisabled" default:"" commented:"true"` + } `toml:"project" comment:"######################\n 'Project' global configuration \n######################" json:"project"` EventBus event.Config `toml:"events" comment:"######################\n Event bus configuration \n######################" json:"events" mapstructure:"events"` } diff --git a/engine/api/config.go b/engine/api/config.go index 26f57b7e9a..3767b7010b 100644 --- a/engine/api/config.go +++ b/engine/api/config.go @@ -91,7 +91,9 @@ func (api *API) configCDNHandler() service.Handler { func (api *API) configAPIHandler() service.Handler { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { return service.WriteJSON(w, sdk.APIConfig{ - DefaultRunRetentionPolicy: api.Config.Workflow.DefaultRetentionPolicy, + DefaultRunRetentionPolicy: api.Config.Workflow.DefaultRetentionPolicy, + ProjectCreationDisabled: api.Config.Project.CreationDisabled, + ProjectInfoCreationDisabled: api.Config.Project.InfoCreationDisabled, }, http.StatusOK) } } diff --git a/engine/api/project.go b/engine/api/project.go index 78403659b0..54df8fb2f9 100644 --- a/engine/api/project.go +++ b/engine/api/project.go @@ -447,6 +447,10 @@ func (api *API) postProjectHandler() service.Handler { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { consumer := getUserConsumer(ctx) + if api.Config.Project.CreationDisabled && !isAdmin(ctx) { + return sdk.NewErrorFrom(sdk.ErrForbidden, "project creation is disabled") + } + var p sdk.Project if err := service.UnmarshalBody(r, &p); err != nil { return sdk.WrapError(err, "unable to unmarshal body") diff --git a/sdk/config.go b/sdk/config.go index 4de84164ff..5ba972152d 100644 --- a/sdk/config.go +++ b/sdk/config.go @@ -12,7 +12,9 @@ type ConfigUser struct { } type APIConfig struct { - DefaultRunRetentionPolicy string `json:"default_run_retention_policy"` + DefaultRunRetentionPolicy string `json:"default_run_retention_policy"` + ProjectCreationDisabled bool `json:"project_creation_disabled"` + ProjectInfoCreationDisabled string `json:"project_info_creation_disabled,omitempty"` } type TCPServer struct { diff --git a/ui/src/app/model/config.service.ts b/ui/src/app/model/config.service.ts index 8f35ca05cb..ad1624edfe 100644 --- a/ui/src/app/model/config.service.ts +++ b/ui/src/app/model/config.service.ts @@ -1,3 +1,5 @@ export class APIConfig { default_run_retention_policy: string; + project_creation_disabled: boolean; + project_info_creation_disabled: string; } diff --git a/ui/src/app/views/navbar/navbar.component.ts b/ui/src/app/views/navbar/navbar.component.ts index 5af22774c7..ef887130e2 100644 --- a/ui/src/app/views/navbar/navbar.component.ts +++ b/ui/src/app/views/navbar/navbar.component.ts @@ -1,13 +1,14 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; import { NavigationEnd, Router } from '@angular/router'; import { Store } from '@ngxs/store'; +import { APIConfig } from 'app/model/config.service'; import { Help } from 'app/model/help.model'; import { NavbarProjectData, NavbarRecentData, NavbarSearchItem } from 'app/model/navbar.model'; import { Project } from 'app/model/project.model'; import { AuthSummary } from 'app/model/user.model'; import { NavbarService } from 'app/service/navbar/navbar.service'; import { RouterService } from 'app/service/router/router.service'; -import { ProjectStore } from 'app/service/services.module'; +import { ConfigService, ProjectStore } from 'app/service/services.module'; import { ThemeStore } from 'app/service/theme/theme.store'; import { WorkflowStore } from 'app/service/workflow/workflow.store'; import { AutoUnsubscribe } from 'app/shared/decorator/autoUnsubscribe'; @@ -51,9 +52,8 @@ export class NavbarComponent implements OnInit, OnDestroy { containsResult = false; projectsSubscription: Subscription; workflowsSubscription: Subscription; - - showNotif = false; + apiConfig: APIConfig; constructor( private _navbarService: NavbarService, @@ -63,8 +63,14 @@ export class NavbarComponent implements OnInit, OnDestroy { private _router: Router, private _theme: ThemeStore, private _routerService: RouterService, - private _cd: ChangeDetectorRef + private _cd: ChangeDetectorRef, + private _configService: ConfigService ) { + this._configService.getAPIConfig().subscribe(c => { + this.apiConfig = c; + this._cd.markForCheck(); + }); + this.authSubscription = this._store.select(AuthenticationState.summary).subscribe(s => { this.currentAuthSummary = s; this._cd.markForCheck(); diff --git a/ui/src/app/views/navbar/navbar.html b/ui/src/app/views/navbar/navbar.html index 869c723a3a..120233b724 100644 --- a/ui/src/app/views/navbar/navbar.html +++ b/ui/src/app/views/navbar/navbar.html @@ -1,14 +1,14 @@ -