Remove PermissionsService and replace its usage with the CurrentUserService (#9282)

This removes the only usage of the permissions service in the invite-user-button and puts the new capabilities based
APIs in its place. It also unifies the action being checked for both outside the ium and inside
pull/9291/head
Benjamin Bädorf 4 years ago committed by GitHub
parent 2957236a07
commit 97489ca1e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      frontend/src/app/core/services/permissions/permissions.service.spec.ts
  2. 33
      frontend/src/app/core/services/permissions/permissions.service.ts
  3. 2
      frontend/src/app/modules/fields/edit/field-types/select-edit-field/select-edit-field.component.ts
  4. 2
      frontend/src/app/modules/invite-user-modal/button/invite-user-button.component.html
  5. 25
      frontend/src/app/modules/invite-user-modal/button/invite-user-button.component.ts
  6. 2
      frontend/src/app/modules/invite-user-modal/invite-user-modal.module.ts
  7. 2
      frontend/src/app/modules/invite-user-modal/project-selection/project-allowed.validator.ts

@ -1,17 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { PermissionsService } from './permissions.service';
// TODO: Test this service
/*describe('PermissionsService', () => {
let service:PermissionsService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(PermissionsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});*/

@ -1,33 +0,0 @@
import { Injectable } from '@angular/core';
import { APIV3Service } from 'core-app/modules/apiv3/api-v3.service';
import { Observable, of } from 'rxjs';
import { CurrentProjectService } from 'core-components/projects/current-project.service';
import { map, catchError } from 'rxjs/operators';
import { FilterOperator } from 'core-components/api/api-v3/api-v3-filter-builder';
@Injectable({
providedIn: 'root',
})
export class PermissionsService {
constructor(
private apiV3Service:APIV3Service,
private currentProjectService:CurrentProjectService,
) { }
canInviteUsersToProject(projectId = this.currentProjectService.id!):Observable<boolean> {
// TODO: Remove/Fix this typing issue
const filters:[string, FilterOperator, string[]][] = [['id', '=', [projectId]]];
return this.apiV3Service
.memberships
.available_projects
.list({ filters })
.pipe(
map(collection => !!collection.elements.length),
catchError((error) => {
console.error(error);
return of(false);
}),
);
}
}

@ -36,7 +36,6 @@ import { from } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { HalResourceNotificationService } from 'core-app/modules/hal/services/hal-resource-notification.service';
import { InjectField } from 'core-app/helpers/angular/inject-field.decorator';
import { PermissionsService } from 'core-app/core/services/permissions/permissions.service';
import { CreateAutocompleterComponent } from "core-app/modules/autocompleter/create-autocompleter/create-autocompleter.component";
import { EditFormComponent } from "core-app/modules/fields/edit/edit-form/edit-form.component";
import { StateService } from "@uirouter/core";
@ -53,7 +52,6 @@ export class SelectEditFieldComponent extends EditFieldComponent implements OnIn
@InjectField() selectAutocompleterRegister:SelectAutocompleterRegisterService;
@InjectField() halNotification:HalResourceNotificationService;
@InjectField() halSorting:HalResourceSortingService;
@InjectField() permissionsService:PermissionsService;
@InjectField() $state:StateService;
@InjectField(EditFormComponent, null, InjectFlags.Optional) editFormComponent:EditFormComponent;

@ -2,7 +2,7 @@
class="op-select-footer--label"
type="button"
(click)="onAddNewClick($event)"
*ngIf="canInviteUsersToProject"
*ngIf="canInviteUsersToProject$ | async"
>
<span class="icon-context">
<op-icon icon-classes="icon-user-plus icon-context"></op-icon>

@ -1,7 +1,8 @@
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { ChangeDetectorRef, Component } from '@angular/core';
import { NgSelectComponent } from "@ng-select/ng-select";
import { I18nService } from "core-app/modules/common/i18n/i18n.service";
import { PermissionsService } from "core-app/core/services/permissions/permissions.service";
import { CurrentUserService } from "core-app/modules/current-user/current-user.service";
import { CurrentProjectService } from "core-app/components/projects/current-project.service";
import { OpInviteUserModalService } from "core-app/modules/invite-user-modal/invite-user-modal.service";
@Component({
@ -9,7 +10,7 @@ import { OpInviteUserModalService } from "core-app/modules/invite-user-modal/inv
templateUrl: './invite-user-button.component.html',
styleUrls: ['./invite-user-button.component.sass']
})
export class InviteUserButtonComponent implements OnInit {
export class InviteUserButtonComponent {
/** This component does not provide an output, because both primary usecases were in places where the button was
* destroyed before the modal closed, causing the data from the modal to never arrive at the parent.
* If you want to do something with the output from the modal that is opened, use the OpInviteUserModalService
@ -18,25 +19,21 @@ export class InviteUserButtonComponent implements OnInit {
text = {
button: this.I18n.t('js.invite_user_modal.invite'),
};
canInviteUsersToProject:boolean;
canInviteUsersToProject$ = this.currentUserService.hasCapabilities$(
'memberships/create',
this.currentProjectService.id || undefined,
);
constructor(
readonly I18n:I18nService,
readonly opInviteUserModalService:OpInviteUserModalService,
readonly permissionsService:PermissionsService,
readonly currentUserService:CurrentUserService,
readonly currentProjectService:CurrentProjectService,
readonly ngSelectComponent:NgSelectComponent,
readonly changeDetectorRef:ChangeDetectorRef,
) {}
ngOnInit():void {
this.permissionsService
.canInviteUsersToProject()
.subscribe(canInviteUsersToProject => {
this.canInviteUsersToProject = canInviteUsersToProject;
this.changeDetectorRef.detectChanges();
});
}
onAddNewClick($event:Event) {
$event.stopPropagation();
this.opInviteUserModalService.open();

@ -5,6 +5,7 @@ import { TextFieldModule } from '@angular/cdk/text-field';
import { NgSelectModule } from "@ng-select/ng-select";
import { OpenprojectModalModule } from "core-app/modules/modal/modal.module";
import { OpenprojectCommonModule } from "core-app/modules/common/openproject-common.module";
import { CurrentUserModule } from 'core-app/modules/current-user/current-user.module';
import { DynamicFormsModule } from "core-app/modules/common/dynamic-forms/dynamic-forms.module";
import { OpInviteUserModalAugmentService } from "core-app/modules/invite-user-modal/invite-user-modal-augment.service";
import { OpInviteUserModalService } from "core-app/modules/invite-user-modal/invite-user-modal.service";
@ -37,6 +38,7 @@ export function initializeServices(injector:Injector) {
TextFieldModule,
DynamicFormsModule,
InviteUserButtonModule,
CurrentUserModule,
],
exports: [
InviteUserButtonModule,

@ -5,7 +5,7 @@ import { catchError, map, take } from "rxjs/operators";
export const ProjectAllowedValidator = (currentUserService:CurrentUserService) => {
return (control: AbstractControl) => {
return currentUserService.hasCapabilities$('memberships/update', control.value.idFromLink).pipe(
return currentUserService.hasCapabilities$('memberships/create', control.value.idFromLink).pipe(
take(1),
map(isAllowed => isAllowed ? null : { lackingPermission: true }),
catchError(() => of(null))

Loading…
Cancel
Save