OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Derive inline create permissions from capabilities
Oliver Güntherea4b4b4a8045
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex ef5b3ee859d..2e49697a53d 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -142,14 +142,16 @@ Rails.application.reloader.to_prepare docontract_actions: { work_packages: %i[read] }wpt.permission :add_work_packages,- {}+ {},+ contract_actions: { work_packages: %i[create] }wpt.permission :edit_work_packages,{'work_packages/bulk': %i[edit update]},require: :member,- dependencies: :view_work_packages+ dependencies: :view_work_packages,+ contract_actions: { work_packages: %i[update] }wpt.permission :move_work_packages,{ 'work_packages/moves': %i[new create] },diff --git a/frontend/src/app/features/boards/board/board-list/board-inline-create.service.ts b/frontend/src/app/features/boards/board/board-list/board-inline-create.service.tsindex 3b9869a6fbc..1da0968e206 100644--- a/frontend/src/app/features/boards/board/board-list/board-inline-create.service.ts+++ b/frontend/src/app/features/boards/board/board-list/board-inline-create.service.ts@@ -26,7 +26,10 @@// See COPYRIGHT and LICENSE files for more details.//++-import { Injectable, Injector } from '@angular/core';+import {+ Injectable,+ Injector,+} from '@angular/core';import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';import { WorkPackageRelationsHierarchyService } from 'core-app/features/work-packages/components/wp-relations/wp-relations-hierarchy/wp-relations-hierarchy.service';import { WorkPackageInlineCreateService } from 'core-app/features/work-packages/components/wp-inline-create/wp-inline-create.service';@@ -35,15 +38,14 @@ import { HalResourceService } from 'core-app/features/hal/services/hal-resource.import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';import { BoardInlineAddAutocompleterComponent } from 'core-app/features/boards/board/inline-add/board-inline-add-autocompleter.component';import { GonService } from 'core-app/core/gon/gon.service';+import { Observable } from 'rxjs';@Injectable()export class BoardInlineCreateService extends WorkPackageInlineCreateService {constructor(readonly injector:Injector,protected readonly querySpace:IsolatedQuerySpace,protected readonly halResourceService:HalResourceService,- protected readonly pathHelperService:PathHelperService,- protected readonly Gon:GonService,- protected readonly wpRelationsHierarchyService:WorkPackageRelationsHierarchyService) {+ ) {super(injector);}@@ -57,12 +59,22 @@ export class BoardInlineCreateService extends WorkPackageInlineCreateService {*/public referenceTarget:WorkPackageResource|null = null;- public get canAdd() {- return this.authorisationService.can('work_packages', 'createWorkPackage');+ public get canAdd():Observable<boolean> {+ return this+ .currentUser+ .hasCapabilities$(+ ['work_packages/create'],+ this.currentProject.id || 'global',+ );}- public get canReference() {- return this.authorisationService.can('work_packages', 'editWorkPackage');+ public get canReference():Observable<boolean> {+ return this+ .currentUser+ .hasCapabilities$(+ ['work_packages/update'],+ this.currentProject.id || 'global',+ );}/**diff --git a/frontend/src/app/features/boards/board/board-list/board-list.component.ts b/frontend/src/app/features/boards/board/board-list/board-list.component.tsindex 566b9db91b4..ecbafa572cb 100644--- a/frontend/src/app/features/boards/board/board-list/board-list.component.ts+++ b/frontend/src/app/features/boards/board/board-list/board-list.component.ts@@ -43,7 +43,11 @@ import { WorkPackageViewFocusService } from 'core-app/features/work-packages/rouimport { WorkPackageViewSelectionService } from 'core-app/features/work-packages/routing/wp-view-base/view-services/wp-view-selection.service';import { BoardListCrossSelectionService } from 'core-app/features/boards/board/board-list/board-list-cross-selection.service';import {- debounceTime, filter, map, retry,+ debounceTime,+ filter,+ map,+ retry,+ take,} from 'rxjs/operators';import { ChangeItem } from 'core-app/shared/components/fields/changeset/changeset';import { WorkPackageChangeset } from 'core-app/features/work-packages/components/wp-edit/work-package-changeset';@@ -107,6 +111,8 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnIni/** Whether the add button should be shown */public showAddButton = false;+ private canAdd = this.wpInlineCreate.canAdd.pipe(take(1)).toPromise();+public columnsQueryProps:any;public text = {@@ -172,16 +178,6 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnInithis.wpViewSelectionService.initializeSelection([wpId]);}- // Update permission on model updates- this.authorisationService- .observeUntil(componentDestroyed(this))- .subscribe(() => {- if (!this.board.isAction) {- this.showAddButton = this.canDragInto && (this.wpInlineCreate.canAdd || this.canReference);- this.cdRef.detectChanges();- }- });-// If this query space changes its focused or selected// work packages, update the board cross selectionthis.wpViewSelectionService@@ -228,10 +224,10 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnIni.pipe(this.untilDestroyed(),)- .subscribe((query) => {+ .subscribe(async (query) => {this.query = query;this.canDragOutOf = !!this.query.updateOrderedWorkPackages;- this.loadActionAttribute(query);+ await this.loadActionAttribute(query);this.cdRef.detectChanges();});}@@ -248,10 +244,6 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnInireturn this.canDragOutOf && (!this.actionService || this.actionService.canMove(workPackage));}- public get canReference() {- return this.wpInlineCreate.canReference;- }-public get canManage() {return this.boardService.canManage(this.board);}@@ -330,11 +322,13 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnInithis.loadQuery(visibly);}- private loadActionAttribute(query:QueryResource) {+ private async loadActionAttribute(query:QueryResource) {if (!this.board.isAction) {this.actionResource = undefined;this.headerComponent = undefined;this.canDragInto = !!query.updateOrderedWorkPackages;+ const canAdd = await this.canAdd;+ this.showAddButton = this.canDragInto && canAdd;return;}@@ -355,7 +349,8 @@ export class BoardListComponent extends AbstractWidgetComponent implements OnInithis.canDragInto = actionService.dragIntoAllowed(query, resource);const canWriteAttribute = await actionService.canAddToQuery(query);- this.showAddButton = this.canDragInto && this.wpInlineCreate.canAdd && canWriteAttribute;+ const canAdd = await this.canAdd;+ this.showAddButton = this.canDragInto && canAdd && canWriteAttribute;this.cdRef.detectChanges();});}diff --git a/frontend/src/app/features/work-packages/components/wp-card-view/wp-card-view.component.ts b/frontend/src/app/features/work-packages/components/wp-card-view/wp-card-view.component.tsindex a75099bd3db..72f1d08da1b 100644--- a/frontend/src/app/features/work-packages/components/wp-card-view/wp-card-view.component.ts+++ b/frontend/src/app/features/work-packages/components/wp-card-view/wp-card-view.component.ts@@ -41,6 +41,7 @@ import { QueryResource } from 'core-app/features/hal/resources/query-resource';import { HalEventsService } from 'core-app/features/hal/services/hal-events.service';import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';import isNewResource from 'core-app/features/hal/helpers/is-new-resource';+import { combineLatest } from 'rxjs';export type CardViewOrientation = 'horizontal'|'vertical';@@ -102,11 +103,6 @@ export class WorkPackageCardViewComponent extends UntilDestroyedMixin implements},};- /** Inline create / reference properties */- public canAdd = false;-- public canReference = false;-public inReference = false;public referenceClass = this.wpInlineCreate.referenceComponentClass;@@ -144,15 +140,6 @@ export class WorkPackageCardViewComponent extends UntilDestroyedMixin implementsngOnInit() {this.registerCreationCallback();- // Update permission on model updates- this.authorisationService- .observeUntil(componentDestroyed(this))- .subscribe(() => {- this.canAdd = this.wpInlineCreate.canAdd;- this.canReference = this.wpInlineCreate.canReference;- this.cdRef.detectChanges();- });-// Observe changes to the work packages in this viewthis.halEvents.aggregated$('WorkPackage')diff --git a/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.component.ts b/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.component.tsindex 818dc4fbd6a..5636898b8cc 100644--- a/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.component.ts+++ b/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.component.ts@@ -35,16 +35,23 @@ import {Injector,Input,EventEmitter,- OnInit, Output,+ OnInit,+ Output,} from '@angular/core';import { AuthorisationService } from 'core-app/core/model-auth/model-auth.service';import { WorkPackageViewFocusService } from 'core-app/features/work-packages/routing/wp-view-base/view-services/wp-view-focus.service';-import { filter } from 'rxjs/operators';+import {+ filter,+ shareReplay,+} from 'rxjs/operators';import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';import { IsolatedQuerySpace } from 'core-app/features/work-packages/directives/query-space/isolated-query-space';import { I18nService } from 'core-app/core/i18n/i18n.service';import { WorkPackageInlineCreateService } from 'core-app/features/work-packages/components/wp-inline-create/wp-inline-create.service';-import { Subscription } from 'rxjs';+import {+ combineLatest,+ Subscription,+} from 'rxjs';import { WorkPackageViewColumnsService } from 'core-app/features/work-packages/routing/wp-view-base/view-services/wp-view-columns.service';import { WorkPackageChangeset } from 'core-app/features/work-packages/components/wp-edit/work-package-changeset';import { EditForm } from 'core-app/shared/components/fields/edit/edit-form/edit-form';@@ -113,14 +120,18 @@ export class WorkPackageInlineCreateComponent extends UntilDestroyedMixin implemthis.$element = jQuery(this.elementRef.nativeElement);}- ngAfterViewInit() {- this.authorisationService- .observeUntil(componentDestroyed(this))- .subscribe(() => {- this.canReference = this.hasReferenceClass && this.wpInlineCreate.canReference;- this.canAdd = this.wpInlineCreate.canAdd;+ ngAfterViewInit():void {+ combineLatest([+ this.wpInlineCreate.canAdd,+ this.wpInlineCreate.canReference,+ ])+ .pipe(+ this.untilDestroyed(),+ )+ .subscribe(([canAdd, canReference]) => {+ this.canAdd = canAdd;+ this.canReference = this.hasReferenceClass && canReference;this.cdRef.detectChanges();-this.showing.emit(this.canAdd || this.canReference);});diff --git a/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.service.ts b/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.service.tsindex 8aaccb3460f..d9c7bbeaf40 100644--- a/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.service.ts+++ b/frontend/src/app/features/work-packages/components/wp-inline-create/wp-inline-create.service.ts@@ -26,19 +26,30 @@// See COPYRIGHT and LICENSE files for more details.//++-import { Injectable, Injector, OnDestroy } from '@angular/core';+import {+ Injectable,+ Injector,+ OnDestroy,+} from '@angular/core';import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';-import { Subject } from 'rxjs';+import {+ Observable,+ of,+ Subject,+} from 'rxjs';import { ComponentType } from '@angular/cdk/portal';import { I18nService } from 'core-app/core/i18n/i18n.service';-import { AuthorisationService } from 'core-app/core/model-auth/model-auth.service';import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';+import { CurrentUserService } from 'core-app/core/current-user/current-user.service';+import { CurrentProjectService } from 'core-app/core/current-project/current-project.service';@Injectable()export class WorkPackageInlineCreateService implements OnDestroy {@InjectField() I18n!:I18nService;- @InjectField() protected readonly authorisationService:AuthorisationService;+ @InjectField() protected readonly currentUser:CurrentUserService;++ @InjectField() protected readonly currentProject:CurrentProjectService;constructor(readonly injector:Injector) {}@@ -61,17 +72,28 @@ export class WorkPackageInlineCreateService implements OnDestroy {create: this.I18n.t('js.label_create_work_package'),};- public get canAdd() {- return this.canCreateWorkPackages || this.authorisationService.can('work_package', 'addChild');+ public get canAdd():Observable<boolean> {+ return this.canCreateWorkPackages();}- public get canReference() {- return false;+ public get canReference():Observable<boolean> {+ return of(false);}- public get canCreateWorkPackages() {- return this.authorisationService.can('work_packages', 'createWorkPackage')- && this.authorisationService.can('work_packages', 'editWorkPackage');+ /**+ * Observable capability check for work_packages/create and /edit.+ * Edit is included as inline create saves quickly, preventing further edits for users+ * that don't also have edit permisison.+ *+ * @protected+ */+ protected canCreateWorkPackages(projectId:string|null = this.currentProject.id):Observable<boolean> {+ return this+ .currentUser+ .hasCapabilities$(+ ['work_packages/create', 'work_packages/update'],+ projectId || 'global',+ );}/** Allow callbacks to happen on newly created inline work packages */@@ -83,7 +105,7 @@ export class WorkPackageInlineCreateService implements OnDestroy {/*** Ensure hierarchical injected versions of this service correctly unregister*/- ngOnDestroy() {+ ngOnDestroy():void {this.newInlineWorkPackageCreated.complete();this.newInlineWorkPackageReferenced.complete();}diff --git a/frontend/src/app/features/work-packages/components/wp-relations/embedded/children/wp-children-inline-create.service.ts b/frontend/src/app/features/work-packages/components/wp-relations/embedded/children/wp-children-inline-create.service.tsindex de1a84b9861..6954c83630b 100644--- a/frontend/src/app/features/work-packages/components/wp-relations/embedded/children/wp-children-inline-create.service.ts+++ b/frontend/src/app/features/work-packages/components/wp-relations/embedded/children/wp-children-inline-create.service.ts@@ -33,6 +33,11 @@ import { WorkPackageInlineCreateService } from 'core-app/features/work-packages/import { WpRelationInlineCreateServiceInterface } from 'core-app/features/work-packages/components/wp-relations/embedded/wp-relation-inline-create.service.interface';import { WpRelationInlineAddExistingComponent } from 'core-app/features/work-packages/components/wp-relations/embedded/inline/add-existing/wp-relation-inline-add-existing.component';import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';+import {+ Observable,+ of,+} from 'rxjs';+import idFromLink from 'core-app/features/hal/helpers/id-from-link';@Injectable()export class WpChildrenInlineCreateService extends WorkPackageInlineCreateService implements WpRelationInlineCreateServiceInterface {@@ -71,16 +76,21 @@ export class WpChildrenInlineCreateService extends WorkPackageInlineCreateServic*/public referenceTarget:WorkPackageResource|null = null;- public get canAdd() {- return !!(this.referenceTarget && this.canCreateWorkPackages && this.canAddChild);+ public get canAdd():Observable<boolean> {+ if (!(this.referenceTarget && this.canAddChild)) {+ return of(false);+ }++ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ return this.canCreateWorkPackages(idFromLink(this.referenceTarget.project.href));}- public get canReference() {- return !!(this.referenceTarget && this.canAddChild);+ public get canReference():Observable<boolean> {+ return of(!!this.referenceTarget && this.canAddChild);}- public get canAddChild() {- return this.schema && !this.schema.isMilestone && this.referenceTarget!.changeParent;+ public get canAddChild():boolean {+ return !!(this.schema && !this.schema.isMilestone && this.referenceTarget!.changeParent);}/**diff --git a/frontend/src/app/features/work-packages/components/wp-relations/embedded/relations/wp-relation-inline-create.service.ts b/frontend/src/app/features/work-packages/components/wp-relations/embedded/relations/wp-relation-inline-create.service.tsindex 6b1fda9464e..2119bc5850d 100644--- a/frontend/src/app/features/work-packages/components/wp-relations/embedded/relations/wp-relation-inline-create.service.ts+++ b/frontend/src/app/features/work-packages/components/wp-relations/embedded/relations/wp-relation-inline-create.service.ts@@ -33,6 +33,11 @@ import { WpRelationInlineAddExistingComponent } from 'core-app/features/work-pacimport { WorkPackageRelationsService } from 'core-app/features/work-packages/components/wp-relations/wp-relations.service';import { WpRelationInlineCreateServiceInterface } from 'core-app/features/work-packages/components/wp-relations/embedded/wp-relation-inline-create.service.interface';import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';+import {+ Observable,+ of,+} from 'rxjs';+import idFromLink from 'core-app/features/hal/helpers/id-from-link';@Injectable()export class WpRelationInlineCreateService extends WorkPackageInlineCreateService implements WpRelationInlineCreateServiceInterface, OnDestroy {@@ -77,12 +82,17 @@ export class WpRelationInlineCreateService extends WorkPackageInlineCreateServic*/public referenceTarget:WorkPackageResource|null = null;- public get canAdd() {- return !!(this.referenceTarget && this.canCreateWorkPackages && this.referenceTarget.addRelation);+ public get canAdd():Observable<boolean> {+ if (!this.referenceTarget?.addRelation) {+ return of(false);+ }++ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ return this.canCreateWorkPackages(idFromLink(this.referenceTarget.project.href));}- public get canReference() {- return !!this.canAdd;+ public get canReference():Observable<boolean> {+ return this.canAdd;}/**