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

Login if restricted Item - accessing via ..handle/ url #612

Merged
merged 3 commits into from
May 7, 2024
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
14 changes: 13 additions & 1 deletion src/app/core/data/dso-redirect.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { EMBED_SEPARATOR } from './base/base-data.service';
import { HardRedirectService } from '../services/hard-redirect.service';
import { environment } from '../../../environments/environment.test';
import { AppConfig } from '../../../config/app-config.interface';
import { AuthService } from '../auth/auth.service';
import { RouterStub } from '../../shared/testing/router.stub';

describe('DsoRedirectService', () => {
let scheduler: TestScheduler;
Expand All @@ -22,6 +24,8 @@ describe('DsoRedirectService', () => {
let rdbService: RemoteDataBuildService;
let redirectService: HardRedirectService;
let remoteData;
let router: any;
let authService: AuthService;
const dsoUUID = '9b4f22f4-164a-49db-8817-3316b6ee5746';
const dsoHandle = '1234567789/22';
const encodedHandle = encodeURIComponent(dsoHandle);
Expand Down Expand Up @@ -57,13 +61,21 @@ describe('DsoRedirectService', () => {
redirect: {}
});

router = new RouterStub();

authService = jasmine.createSpyObj('authService', {
setRedirectUrl: {}
});

service = new DsoRedirectService(
environment as AppConfig,
requestService,
rdbService,
objectCache,
halService,
redirectService
redirectService,
router,
authService
);
});

Expand Down
15 changes: 14 additions & 1 deletion src/app/core/data/dso-redirect.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { IdentifiableDataService } from './base/identifiable-data.service';
import { getDSORoute } from '../../app-routing-paths';
import { HardRedirectService } from '../services/hard-redirect.service';
import { APP_CONFIG, AppConfig } from '../../../config/app-config.interface';
import { Router } from '@angular/router';
import { AuthService } from '../auth/auth.service';

const ID_ENDPOINT = 'pid';
const UUID_ENDPOINT = 'dso';
Expand Down Expand Up @@ -76,7 +78,9 @@ export class DsoRedirectService {
protected rdbService: RemoteDataBuildService,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
private hardRedirectService: HardRedirectService
private hardRedirectService: HardRedirectService,
private router: Router,
private authService: AuthService,
) {
this.dataService = new DsoByIdOrUUIDDataService(requestService, rdbService, objectCache, halService);
}
Expand Down Expand Up @@ -104,6 +108,15 @@ export class DsoRedirectService {
}
}
}
// Redirect to login page if the user is not authenticated to see the requested page
if (response.hasFailed && (response.statusCode === 401 || response.statusCode === 403)) {
vidiecan marked this conversation as resolved.
Show resolved Hide resolved
// Remove `/` from the namespace if it is empty
const namespace = this.appConfig.ui.nameSpace === '/' ? '' : this.appConfig.ui.nameSpace;
vidiecan marked this conversation as resolved.
Show resolved Hide resolved
// Compose redirect URL - remove `https://.../namespace` from the current URL. Keep only `handle/...`
const redirectUrl = window.location.href.replace(this.appConfig.ui.baseUrl + namespace, '');
this.authService.setRedirectUrl(redirectUrl);
this.router.navigateByUrl('login');
}
})
);
}
Expand Down
Loading