diff --git a/src/app/core/roles/role.service.ts b/src/app/core/roles/role.service.ts index 365a5cbbb31..d95a4a27c8a 100644 --- a/src/app/core/roles/role.service.ts +++ b/src/app/core/roles/role.service.ts @@ -2,11 +2,23 @@ import { Injectable } from '@angular/core'; import { Observable, of, + combineLatest } from 'rxjs'; -import { distinctUntilChanged } from 'rxjs/operators'; +import { distinctUntilChanged, map, switchMap, catchError } from 'rxjs/operators'; import { CollectionDataService } from '../data/collection-data.service'; import { RoleType } from './role-types'; +import { ItemDataService } from '../data/item-data.service'; +import { EPerson } from '../eperson/models/eperson.model'; +import { AuthService } from '../auth/auth.service'; +import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; +import { Item } from '../shared/item.model'; +import { itemLinksToFollow } from 'src/app/shared/utils/relation-query.utils'; +import { LinkService } from '../cache/builders/link.service'; +import { followLink } from '../../shared/utils/follow-link-config.model'; +import { DSpaceObject } from '../shared/dspace-object.model'; +import { HttpClient } from '@angular/common/http'; + /** * A service that provides methods to identify user role. @@ -19,7 +31,13 @@ export class RoleService { * * @param {CollectionDataService} collectionService */ - constructor(private collectionService: CollectionDataService) { + constructor( + private collectionService: CollectionDataService, + private itemDataService: ItemDataService, + private auth: AuthService, + private linkService: LinkService, + private http: HttpClient, + ) { } /** @@ -31,6 +49,58 @@ export class RoleService { ); } + /** + * Retrieve the current user + */ + private getCurrentUser(): Observable { + return this.auth.isAuthenticated().pipe( + switchMap((authenticated) => { + if (authenticated) { + return this.auth.getAuthenticatedUserFromStore(); + } else { + return of(undefined); + } + }), + ); + + } + + + isSubmitterOfItem(dso: DSpaceObject): Observable { + return combineLatest([ + this.itemDataService.findById(dso.id), + this.getCurrentUser() + ]).pipe( + switchMap(([itemRD, currentUser]) => { + if (!currentUser || !itemRD.hasSucceeded) { + return of(false); + } + + // Get the item information + const item = itemRD.payload; + // The submitter is not a followLink for Item, so we get it manually + const submitter_link = item._links.submitter; + + if (submitter_link?.href) { + // Call to the submitter link to get the EPerson + return this.http.get(submitter_link.href).pipe( + map(submitter => { + + const isSubmitter = submitter.uuid === currentUser.uuid; + + return isSubmitter; + }), + catchError(error => { + return of(false); + }) + ); + } + return of(false); + }) + ); + } + + /** * Check if current user is a controller */ diff --git a/src/app/core/shared/item.model.ts b/src/app/core/shared/item.model.ts index d6066f098c0..b58539662e7 100644 --- a/src/app/core/shared/item.model.ts +++ b/src/app/core/shared/item.model.ts @@ -88,6 +88,7 @@ export class Item extends DSpaceObject implements ChildHALResource, HandleObject thumbnail: HALLink; accessStatus: HALLink; identifiers: HALLink; + submitter?: HALLink; self: HALLink; }; diff --git a/src/app/shared/menu/providers/dso-print-certificate.menu.ts b/src/app/shared/menu/providers/dso-print-certificate.menu.ts index 8b39945486d..b18f7b6eb31 100644 --- a/src/app/shared/menu/providers/dso-print-certificate.menu.ts +++ b/src/app/shared/menu/providers/dso-print-certificate.menu.ts @@ -21,6 +21,7 @@ import { LinkMenuItemModel } from '../menu-item/models/link.model'; import { MenuItemType } from '../menu-item-type.model'; import { PartialMenuSection } from '../menu-provider.model'; import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu'; +import { RoleService } from '../../../core/roles/role.service'; /** * Menu provider to create the "Edit" option in the DSO edit menu @@ -29,6 +30,7 @@ import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu'; export class DSpaceObjectPrintCertificateMenuProvider extends DSpaceObjectPageMenuProvider { constructor( protected authorizationDataService: AuthorizationDataService, + protected roleService: RoleService, ) { super(); } @@ -36,11 +38,12 @@ export class DSpaceObjectPrintCertificateMenuProvider extends DSpaceObjectPageMe public getSectionsForContext(dso: DSpaceObject): Observable { return combineLatest([ this.authorizationDataService.isAuthorized(FeatureID.CanEditMetadata, dso.self), + this.roleService.isSubmitterOfItem(dso), ]).pipe( - map(([canEditItem]) => { + map(([canEditItem, isSubmitter]) => { return [ { - visible: canEditItem, + visible: canEditItem || isSubmitter, // Show if user can edit OR is submitter model: { type: MenuItemType.LINK, text: this.getDsoType(dso) + '.page.print',