@@ -21,6 +21,8 @@ import { hasValue } from '../../empty.util';
2121import { MenuItemType } from '../menu-item-type.model' ;
2222import { PartialMenuSection } from '../menu-provider.model' ;
2323import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu' ;
24+ import { switchMap , take } from 'rxjs/operators' ;
25+ import { ActivatedRoute } from '@angular/router' ;
2426
2527/**
2628 * Menu provider to create the "Statistics" menu section in the public navbar. The menu depends on the page it is on.
@@ -29,14 +31,148 @@ import { DSpaceObjectPageMenuProvider } from './helper-providers/dso.menu';
2931 */
3032@Injectable ( )
3133export class StatisticsMenuProvider extends DSpaceObjectPageMenuProvider {
34+ private activatedRouteLastChild : any ;
3235
3336 constructor (
3437 protected authorizationService : AuthorizationDataService ,
38+ protected route : ActivatedRoute
3539 ) {
3640 super ( ) ;
3741 }
3842
43+ /**
44+ * Get statistics route dso data
45+ */
46+ getObjectUrl ( data ) {
47+ const object = data . site ? data . site : data . dso ?. payload ;
48+ return object ?. _links ?. self ?. href ;
49+ }
50+
51+ /**
52+ * Get activated route of the deepest activated route
53+ */
54+ getActivatedRoute ( route ) {
55+ if ( route . children . length > 0 ) {
56+ return this . getActivatedRoute ( route . firstChild ) ;
57+ } else {
58+ return route ;
59+ }
60+ }
61+
62+ /**
63+ * Checking authorization for Usage
64+ */
65+ getAuthorizedUsageStatistics ( ) {
66+ return this . activatedRouteLastChild . data . pipe (
67+ switchMap ( ( data ) => {
68+ return this . authorizationService . isAuthorized ( FeatureID . CanViewUsageStatistics , this . getObjectUrl ( data ) ) . pipe (
69+ map ( ( canViewUsageStatistics : boolean ) => {
70+ return canViewUsageStatistics ;
71+ } ) ) ;
72+ } ) ,
73+ ) ;
74+ }
75+
76+ /**
77+ * Checking authorization for Login
78+ */
79+ getAuthorizedLoginStatistics ( ) {
80+ return this . activatedRouteLastChild . data . pipe (
81+ switchMap ( ( data ) => {
82+ return this . authorizationService . isAuthorized ( FeatureID . CanViewLoginStatistics , this . getObjectUrl ( data ) ) . pipe (
83+ map ( ( canViewLoginStatistics : boolean ) => {
84+ return canViewLoginStatistics ;
85+ } ) ) ;
86+ } ) ,
87+ ) ;
88+ }
89+
90+ /**
91+ * Checking authorization for Workflow
92+ */
93+ getAuthorizedWorkflowStatistics ( ) {
94+ return this . activatedRouteLastChild . data . pipe (
95+ switchMap ( ( data ) => {
96+ return this . authorizationService . isAuthorized ( FeatureID . CanViewWorkflowStatistics , this . getObjectUrl ( data ) ) . pipe (
97+ map ( ( canViewWorkflowStatistics : boolean ) => {
98+ return canViewWorkflowStatistics ;
99+ } ) ) ;
100+ } ) ,
101+ ) ;
102+ }
103+
39104 public getSectionsForContext ( dso : DSpaceObject ) : Observable < PartialMenuSection [ ] > {
105+ this . activatedRouteLastChild = this . getActivatedRoute ( this . route ) ;
106+ return combineLatest ( {
107+ canViewUsage : this . getAuthorizedUsageStatistics ( ) ,
108+ canViewLogin : this . getAuthorizedLoginStatistics ( ) ,
109+ canViewWorkflow : this . getAuthorizedWorkflowStatistics ( ) ,
110+ } ) . pipe (
111+ take ( 1 ) ,
112+ map ( ( { canViewUsage, canViewLogin, canViewWorkflow} ) => {
113+ const menuList = [ ] ;
114+ if ( canViewUsage || canViewLogin || canViewWorkflow ) {
115+ if ( canViewUsage ) {
116+ menuList . push ( {
117+ id : 'statistics_site' ,
118+ parentID : 'statistics' ,
119+ active : false ,
120+ visible : true ,
121+ model : {
122+ type : MenuItemType . LINK ,
123+ text : 'menu.section.statistics.site' ,
124+ link : '/statistics' ,
125+ } ,
126+ } ) ;
127+ }
128+
129+ if ( canViewLogin ) {
130+ menuList . push ( {
131+ id : 'statistics_login' ,
132+ parentID : 'statistics' ,
133+ active : false ,
134+ visible : true ,
135+ model : {
136+ type : MenuItemType . LINK ,
137+ text : 'menu.section.statistics.login' ,
138+ link : '/statistics/login' ,
139+ } ,
140+ } ) ;
141+ }
142+
143+ if ( canViewWorkflow ) {
144+ menuList . push ( {
145+ id : 'statistics_workflow' ,
146+ parentID : 'statistics' ,
147+ active : false ,
148+ visible : true ,
149+ model : {
150+ type : MenuItemType . LINK ,
151+ text : 'menu.section.statistics.workflow' ,
152+ link : '/statistics/workflow' ,
153+ } ,
154+ } ) ;
155+ }
156+
157+ // the parent menu should be added after the children
158+ menuList . push (
159+ {
160+ id : 'statistics' ,
161+ active : false ,
162+ visible : true ,
163+ index : 1 ,
164+ model : {
165+ type : MenuItemType . TEXT ,
166+ text : 'menu.section.statistics' ,
167+ } ,
168+ } ,
169+ ) ;
170+ }
171+ return menuList ;
172+ } ) ) ;
173+ }
174+
175+ public getSectionsForContext2 ( dso : DSpaceObject ) : Observable < PartialMenuSection [ ] > {
40176 return combineLatest ( [
41177 this . authorizationService . isAuthorized ( FeatureID . CanViewUsageStatistics , dso ?. _links . self . href ) ,
42178 ] ) . pipe (
0 commit comments