Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: project member resource edit permissions (#DEV-3479) #1554

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -67,7 +67,11 @@
<button class="menu-content" mat-menu-item (click)="openImageInNewTab(images[0].fileValue.fileUrl)">
Open IIIF URL in new tab
</button>
<button class="menu-content" mat-menu-item [disabled]="!adminPermissions" (click)="openReplaceFileDialog()">
<button
class="menu-content"
mat-menu-item
[disabled]="failedToLoad || !editorPermissions"
(click)="openReplaceFileDialog()">
Replace file
</button>
</mat-menu>
Expand Down Expand Up @@ -99,7 +103,7 @@
mat-icon-button
id="DSP_OSD_DRAW_REGION"
matTooltip="Draw Region"
[disabled]="failedToLoad || !adminPermissions"
[disabled]="failedToLoad || !editorPermissions"
(click)="drawButtonClicked()"
[class.active]="regionDrawMode">
<mat-icon svgIcon="draw_region_icon"></mat-icon>
Expand Down
Expand Up @@ -122,7 +122,7 @@ export class StillImageComponent implements OnChanges, OnDestroy, AfterViewInit
@Input() compoundNavigation?: DspCompoundPosition;
@Input() currentTab: string;
@Input() parentResource: ReadResource;
@Input() adminPermissions: boolean;
@Input() editorPermissions: boolean;

@Output() goToPage = new EventEmitter<number>();

Expand Down
Expand Up @@ -60,7 +60,7 @@ <h4>{{resourceLabel(incomingResource, resource)}}</h4>
[currentTab]="selectedTabLabel"
[parentResource]="incomingResource ? incomingResource.res : resource.res"
[activateRegion]="selectedRegion"
[adminPermissions]="isAdmin$ | async"
[editorPermissions]="isEditor$ | async"
(loaded)="representationLoaded($event)"
(goToPage)="compoundNavigation($event)"
(regionClicked)="openRegion($event)"
Expand Down
38 changes: 24 additions & 14 deletions apps/dsp-app/src/app/workspace/resource/resource.component.ts
Expand Up @@ -52,7 +52,7 @@ import {
ResourceSelectors,
UserSelectors,
} from '@dasch-swiss/vre/shared/app-state';
import { Actions, Select, Store, ofActionSuccessful } from '@ngxs/store';
import { Actions, Store, ofActionSuccessful } from '@ngxs/store';
import { Observable, Subject, Subscription, combineLatest } from 'rxjs';
import { map, take, takeUntil, takeWhile, tap } from 'rxjs/operators';
import { ConfirmationWithComment, DialogComponent } from '../../main/dialog/dialog.component';
Expand Down Expand Up @@ -166,24 +166,34 @@ export class ResourceComponent implements OnChanges, OnDestroy {
return allPermissions.indexOf(PermissionUtil.Permissions.M) !== -1;
}

get isAdmin$(): Observable<boolean> {
return combineLatest([this.user$, this.userProjectAdminGroups$]).pipe(
takeUntil(this.ngUnsubscribe),
map(([user, userProjectGroups]) => {
return this.attachedToProjectResource
? ProjectService.IsProjectAdminOrSysAdmin(user, userProjectGroups, this.attachedToProjectResource)
: false;
})
);
}
isAdmin$: Observable<boolean> = combineLatest([
this._store.select(UserSelectors.user),
this._store.select(UserSelectors.userProjectAdminGroups),
]).pipe(
takeUntil(this.ngUnsubscribe),
map(([user, userProjectGroups]) => {
return this.attachedToProjectResource
? ProjectService.IsProjectAdminOrSysAdmin(user, userProjectGroups, this.attachedToProjectResource)
: false;
})
);

isEditor$: Observable<boolean> = combineLatest([
this._store.select(UserSelectors.user),
this._store.select(UserSelectors.userProjectAdminGroups),
]).pipe(
takeUntil(this.ngUnsubscribe),
map(([user, userProjectGroups]) => {
return this.attachedToProjectResource
? ProjectService.IsProjectMemberOrAdminOrSysAdmin(user, userProjectGroups, this.attachedToProjectResource)
: false;
})
);

get resourceClassType(): ResourceClassDefinitionWithPropertyDefinition {
return this.resource.res.entityInfo.classes[this.resource.res.type];
}

@Select(UserSelectors.user) user$: Observable<ReadUser>;
@Select(UserSelectors.userProjectAdminGroups) userProjectAdminGroups$: Observable<string[]>;

constructor(
@Inject(DspApiConnectionToken)
private _dspApiConnection: KnoraApiConnection,
Expand Down
26 changes: 23 additions & 3 deletions libs/vre/shared/app-helper-services/src/lib/project.service.ts
Expand Up @@ -69,6 +69,13 @@ export class ProjectService {
return ProjectService.IsProjectOrSysAdmin(groupsPerProject, userProjectGroups, projectIri);
}

static IsProjectMemberOrAdminOrSysAdmin(user: ReadUser, userProjectGroups: string[], projectIri: string): boolean {
return (
ProjectService.IsProjectMember(user, userProjectGroups, projectIri) ||
ProjectService.IsProjectAdminOrSysAdmin(user, userProjectGroups, projectIri)
);
}

static IsProjectOrSysAdmin(
groupsPerProject: { [key: string]: string[] },
userProjectGroups: string[],
Expand All @@ -79,12 +86,25 @@ export class ProjectService {
}

static IsProjectMember(user: ReadUser, userProjectGroups: string[], projectUuid: string): boolean {
projectUuid = ProjectService.IriToUuid(projectUuid);
const isProjectAdmin = ProjectService.IsInProjectGroup(userProjectGroups, projectUuid);
return isProjectAdmin
? // check if the user is member of the current project(id contains the iri)
true
: !user || user.projects.length === 0
? false
: user.projects.some(p => ProjectService.IriToUuid(p.id) === projectUuid);
: user && ProjectService.HasProjectMemberRights(user, projectUuid);
}

static HasProjectMemberRights(user: ReadUser, projectUuid: string): boolean {
const project = user.projects.find(p => ProjectService.IriToUuid(p.id) === projectUuid);
if (!project) {
return false;
}

const groupsPerProject = user.permissions.groupsPerProject;
const hasProjectMemberRights =
groupsPerProject &&
groupsPerProject[project.id] &&
groupsPerProject[project.id].includes(Constants.ProjectMemberGroupIRI);
return hasProjectMemberRights === true;
}
}