Skip to content

Commit

Permalink
Merge pull request #15024 from opf/fix/49136/create-button-disabled
Browse files Browse the repository at this point in the history
Use capability to identify whether work package create button is available
  • Loading branch information
dombesz committed Apr 18, 2024
2 parents cb27d91 + 34c77bc commit 5c9220c
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
//++

import { StateService, TransitionService } from '@uirouter/core';
import {
ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnDestroy, OnInit,
} from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnDestroy, OnInit } from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { AuthorisationService } from 'core-app/core/model-auth/model-auth.service';
import { Observable } from 'rxjs';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { componentDestroyed } from '@w11k/ngx-componentdestroyed';
import { CurrentProjectService } from 'core-app/core/current-project/current-project.service';
import { take } from 'rxjs/operators';
import { CurrentUserService } from 'core-app/core/current-user/current-user.service';

@Component({
selector: 'wp-create-button',
Expand Down Expand Up @@ -64,29 +63,29 @@ export class WorkPackageCreateButtonComponent extends UntilDestroyedMixin implem
explanation: this.I18n.t('js.label_create_work_package'),
};

constructor(readonly $state:StateService,
constructor(
readonly $state:StateService,
readonly currentUser:CurrentUserService,
readonly currentProject:CurrentProjectService,
readonly authorisationService:AuthorisationService,
readonly transition:TransitionService,
readonly I18n:I18nService,
readonly cdRef:ChangeDetectorRef) {
readonly cdRef:ChangeDetectorRef,
) {
super();
}

ngOnInit() {
this.projectIdentifier = this.currentProject.identifier;

// Find the first permission that is allowed
this.authorisationService
.observeUntil(componentDestroyed(this))
.subscribe(() => {
this.allowed = !!this
.allowedWhen
.find((combined) => {
const [module, permission] = combined.split('.');
return this.authorisationService.can(module, permission);
});

this.currentUser
.hasCapabilities$('work_packages/create', this.currentProject.id)
.pipe(
take(1),
)
.subscribe((allowed) => {
this.allowed = allowed;
this.updateDisabledState();
});

Expand Down

0 comments on commit 5c9220c

Please sign in to comment.