Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions src/app/core/roles/role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
) {
}

/**
Expand All @@ -31,6 +49,58 @@ export class RoleService {
);
}

/**
* Retrieve the current user
*/
private getCurrentUser(): Observable<EPerson> {
return this.auth.isAuthenticated().pipe(
switchMap((authenticated) => {
if (authenticated) {
return this.auth.getAuthenticatedUserFromStore();
} else {
return of(undefined);
}
}),
);

}


isSubmitterOfItem(dso: DSpaceObject): Observable<boolean> {
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<EPerson>(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
*/
Expand Down
1 change: 1 addition & 0 deletions src/app/core/shared/item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class Item extends DSpaceObject implements ChildHALResource, HandleObject
thumbnail: HALLink;
accessStatus: HALLink;
identifiers: HALLink;
submitter?: HALLink;
self: HALLink;
};

Expand Down
7 changes: 5 additions & 2 deletions src/app/shared/menu/providers/dso-print-certificate.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,18 +30,20 @@ import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu';
export class DSpaceObjectPrintCertificateMenuProvider extends DSpaceObjectPageMenuProvider {
constructor(
protected authorizationDataService: AuthorizationDataService,
protected roleService: RoleService,
) {
super();
}

public getSectionsForContext(dso: DSpaceObject): Observable<PartialMenuSection[]> {
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',
Expand Down
Loading