Skip to content

Commit 17d4d22

Browse files
FrancescoMolinaroAndrea Barbasso
authored andcommitted
Merged in task/dspace-cris-2025_02_x/DSC-2672 (pull request DSpace#4223)
[DSC-2672] prevent custom url in admin pages Approved-by: Andrea Barbasso
2 parents 6f760e9 + 79ec1cd commit 17d4d22

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/app/item-page/item-page.resolver.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import {
2525
} from '../core/shared/authorized.operators';
2626
import { Item } from '../core/shared/item.model';
2727
import { getFirstCompletedRemoteData } from '../core/shared/operators';
28-
import {
29-
hasValue,
30-
isNotEmpty,
31-
} from '../shared/empty.util';
28+
import { hasValue } from '../shared/empty.util';
3229
import { getItemPageLinksToFollow } from './item.resolver';
3330
import { getItemPageRoute } from './item-page-routing-paths';
3431

@@ -55,7 +52,6 @@ export const itemPageResolver: ResolveFn<RemoteData<Item>> = (
5552
platformId: any = inject(PLATFORM_ID),
5653
hardRedirectService: HardRedirectService = inject(HardRedirectService),
5754
): Observable<RemoteData<Item>> => {
58-
5955
const itemRD$ = itemService.findById(
6056
route.params.id,
6157
false,
@@ -73,23 +69,32 @@ export const itemPageResolver: ResolveFn<RemoteData<Item>> = (
7369

7470
return itemRD$.pipe(
7571
map((rd: RemoteData<Item>) => {
76-
store.dispatch(new ResolvedAction(state.url, rd.payload));
7772
if (rd.hasSucceeded && hasValue(rd.payload)) {
78-
const isItemEditPage = state.url.includes('/edit');
79-
const itemRoute = isItemEditPage ? state.url : router.parseUrl(getItemPageRoute(rd.payload)).toString();
80-
// Check if custom url not empty and if the current id parameter is different from the custom url redirect to custom url
81-
if (hasValue(rd.payload.metadata) && isNotEmpty(rd.payload.metadata['cris.customurl'])) {
82-
if (route.params.id !== rd.payload.metadata['cris.customurl'][0].value) {
83-
const newUrl = itemRoute.replace(route.params.id, rd.payload.metadata['cris.customurl'][0].value);
73+
let itemRoute;
74+
if (hasValue(rd.payload.metadata) && rd.payload.hasMetadata('cris.customurl')) {
75+
const customUrl = rd.payload.firstMetadataValue('cris.customurl');
76+
const isSubPath = !(state.url.endsWith(customUrl) || state.url.endsWith(rd.payload.id) || state.url.endsWith('/full'));
77+
itemRoute = isSubPath ? state.url : router.parseUrl(getItemPageRoute(rd.payload)).toString();
78+
let newUrl: string;
79+
if (route.params.id !== customUrl && !isSubPath) {
80+
newUrl = itemRoute.replace(route.params.id,rd.payload.firstMetadataValue('cris.customurl'));
81+
} else if (isSubPath && route.params.id === customUrl) {
82+
// In case of a sub path, we need to ensure we navigate to the edit page of the item ID, not the custom URL
83+
const itemId = rd.payload.uuid;
84+
newUrl = itemRoute.replace(rd.payload.firstMetadataValue('cris.customurl'), itemId);
85+
}
86+
87+
if (hasValue(newUrl)) {
8488
router.navigateByUrl(newUrl);
8589
}
86-
} else {
90+
} else {
8791
const thisRoute = state.url;
8892

8993
// Angular uses a custom function for encodeURIComponent, (e.g. it doesn't encode commas
9094
// or semicolons) and thisRoute has been encoded with that function. If we want to compare
9195
// it with itemRoute, we have to run itemRoute through Angular's version as well to ensure
9296
// the same characters are encoded the same way.
97+
itemRoute = router.parseUrl(getItemPageRoute(rd.payload)).toString();
9398

9499
if (!thisRoute.startsWith(itemRoute)) {
95100
const itemId = rd.payload.uuid;

0 commit comments

Comments
 (0)