mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[MNT-21636] Fix redirect URL for viewer (#6564)
* fix redirect URL for viewer * fix unit fix login SSO show when user is logged in * update js-api * remove protractor change
This commit is contained in:
@@ -150,20 +150,21 @@ export class LoginComponent implements OnInit, OnDestroy {
|
||||
this.initFormFieldsDefault();
|
||||
this.initFormFieldsMessages();
|
||||
|
||||
if (this.authService.isOauth()) {
|
||||
const oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
|
||||
if (oauth && oauth.implicitFlow) {
|
||||
this.implicitFlow = true;
|
||||
}
|
||||
|
||||
if (oauth && oauth.silentLogin && !this.authService.isLoggedIn()) {
|
||||
this.alfrescoApiService.getInstance().oauth2Auth.implicitLogin();
|
||||
}
|
||||
}
|
||||
|
||||
if (this.authService.isLoggedIn()) {
|
||||
this.router.navigate([this.successRoute]);
|
||||
} else {
|
||||
|
||||
if (this.authService.isOauth()) {
|
||||
const oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
|
||||
if (oauth && oauth.implicitFlow) {
|
||||
this.implicitFlow = true;
|
||||
}
|
||||
|
||||
if (oauth && oauth.silentLogin) {
|
||||
this.alfrescoApiService.getInstance().oauth2Auth.implicitLogin();
|
||||
}
|
||||
}
|
||||
|
||||
this.route.queryParams.subscribe((params: Params) => {
|
||||
const url = params['redirectUrl'];
|
||||
const provider = this.appConfig.get<string>(AppConfigValues.PROVIDERS);
|
||||
|
@@ -50,7 +50,6 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild {
|
||||
private storageService: StorageService
|
||||
) {
|
||||
}
|
||||
ls;
|
||||
|
||||
abstract checkLogin(
|
||||
activeRoute: ActivatedRouteSnapshot,
|
||||
@@ -63,11 +62,14 @@ ls;
|
||||
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
|
||||
const redirectFragment = this.storageService.getItem('loginFragment');
|
||||
|
||||
if (this.authenticationService.isLoggedIn() || this.withCredentials) {
|
||||
if (redirectFragment) {
|
||||
|
||||
if (redirectFragment && this.getLoginRoute() !== redirectFragment) {
|
||||
this.storageService.removeItem('loginFragment');
|
||||
return this.router.createUrlTree([redirectFragment]);
|
||||
this.redirectToUrl(redirectFragment);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -86,24 +88,32 @@ ls;
|
||||
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
return this.canActivate(route, state);
|
||||
}
|
||||
protected redirectToUrl(provider: string, url: string) {
|
||||
const pathToLogin = `/${this.getLoginRoute()}`;
|
||||
|
||||
protected redirectToUrl(url: string) {
|
||||
let urlToRedirect;
|
||||
|
||||
this.dialog.closeAll();
|
||||
|
||||
if (!this.authenticationService.isOauth()) {
|
||||
this.authenticationService.setRedirect({ provider, url });
|
||||
if (!this.authenticationService.isLoggedIn()) {
|
||||
const pathToLogin = `/${this.getLoginRoute()}`;
|
||||
|
||||
urlToRedirect = `${pathToLogin}?redirectUrl=${url}`;
|
||||
this.router.navigateByUrl(urlToRedirect);
|
||||
} else if (this.getOauthConfig().silentLogin && !this.authenticationService.isPublicUrl()) {
|
||||
this.authenticationService.ssoImplicitLogin();
|
||||
if (!this.authenticationService.isOauth()) {
|
||||
this.authenticationService.setRedirect({
|
||||
provider: this.getProvider(),
|
||||
url
|
||||
});
|
||||
|
||||
urlToRedirect = `${pathToLogin}?redirectUrl=${url}`;
|
||||
this.router.navigateByUrl(urlToRedirect);
|
||||
} else if (this.getOauthConfig().silentLogin && !this.authenticationService.isPublicUrl()) {
|
||||
this.authenticationService.ssoImplicitLogin();
|
||||
} else {
|
||||
urlToRedirect = pathToLogin;
|
||||
this.router.navigateByUrl(urlToRedirect);
|
||||
}
|
||||
} else {
|
||||
urlToRedirect = pathToLogin;
|
||||
this.router.navigateByUrl(urlToRedirect);
|
||||
this.router.navigateByUrl(url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected getOauthConfig(): OauthConfigModel {
|
||||
@@ -126,6 +136,16 @@ ls;
|
||||
);
|
||||
}
|
||||
|
||||
protected getProvider(): string {
|
||||
return (
|
||||
this.appConfigService &&
|
||||
this.appConfigService.get<string>(
|
||||
AppConfigValues.PROVIDERS,
|
||||
'ALL'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected isOAuthWithoutSilentLogin(): boolean {
|
||||
const oauth = this.appConfigService.get<OauthConfigModel>(
|
||||
AppConfigValues.OAUTHCONFIG,
|
||||
|
@@ -64,7 +64,8 @@ describe('AuthGuardService BPM', () => {
|
||||
redirectUri: '/',
|
||||
clientId: 'activiti',
|
||||
publicUrl: 'settings',
|
||||
scope: 'openid'
|
||||
scope: 'openid',
|
||||
provider: 'BPM'
|
||||
};
|
||||
|
||||
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'abc'};
|
||||
|
@@ -40,7 +40,7 @@ export class AuthGuardBpm extends AuthGuardBase {
|
||||
if (this.authenticationService.isBpmLoggedIn() || this.withCredentials) {
|
||||
return true;
|
||||
}
|
||||
this.redirectToUrl('BPM', redirectUrl);
|
||||
this.redirectToUrl(redirectUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ export class AuthGuardEcm extends AuthGuardBase {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.redirectToUrl('ECM', redirectUrl);
|
||||
this.redirectToUrl(redirectUrl);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@@ -23,12 +23,14 @@ import { AuthenticationService } from './authentication.service';
|
||||
import { setupTestBed } from '../testing/setup-test-bed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { StorageService } from './storage.service';
|
||||
|
||||
describe('AuthGuardService', () => {
|
||||
let state;
|
||||
let authService: AuthenticationService;
|
||||
let router: Router;
|
||||
let authGuard: AuthGuard;
|
||||
let storageService: StorageService;
|
||||
let appConfigService: AppConfigService;
|
||||
|
||||
setupTestBed({
|
||||
@@ -48,6 +50,7 @@ describe('AuthGuardService', () => {
|
||||
|
||||
appConfigService.config.auth = {};
|
||||
appConfigService.config.oauth2 = {};
|
||||
storageService = TestBed.inject(StorageService);
|
||||
});
|
||||
|
||||
it('if the alfresco js api is logged in should canActivate be true', async(async () => {
|
||||
@@ -76,6 +79,18 @@ describe('AuthGuardService', () => {
|
||||
expect(await authGuard.canActivate(null, route)).toBeTruthy();
|
||||
}));
|
||||
|
||||
it('should not redirect to login', async(async () => {
|
||||
storageService.setItem('loginFragment', 'login');
|
||||
|
||||
spyOn(router, 'navigateByUrl').and.stub();
|
||||
spyOn(authService, 'isLoggedIn').and.returnValue(true);
|
||||
spyOn(authService, 'isOauth').and.returnValue(true);
|
||||
appConfigService.config.oauth2.silentLogin = false;
|
||||
|
||||
expect(await authGuard.canActivate(null, state)).toBeTruthy();
|
||||
expect(router.navigateByUrl).not.toHaveBeenCalled();
|
||||
}));
|
||||
|
||||
it('should redirect url if the User is NOT logged in and isOAuthWithoutSilentLogin', async(async () => {
|
||||
spyOn(router, 'navigateByUrl').and.stub();
|
||||
spyOn(authService, 'isLoggedIn').and.returnValue(false);
|
||||
@@ -124,6 +139,7 @@ describe('AuthGuardService', () => {
|
||||
it('should set redirect url with query params', async(async () => {
|
||||
state.url = 'some-url;q=query';
|
||||
appConfigService.config.loginRoute = 'login';
|
||||
appConfigService.config.provider = 'ALL';
|
||||
|
||||
spyOn(router, 'navigateByUrl');
|
||||
spyOn(authService, 'setRedirect');
|
||||
|
@@ -45,23 +45,23 @@ export class AuthGuard extends AuthGuardBase {
|
||||
|
||||
ticketChange(event: StorageEvent) {
|
||||
if (event.key.includes('ticket-ECM') && event.newValue !== event.oldValue) {
|
||||
this.ticketChangeRedirect(event, 'ECM');
|
||||
this.ticketChangeRedirect(event);
|
||||
}
|
||||
|
||||
if (event.key.includes('ticket-BPM') && event.newValue !== event.oldValue) {
|
||||
this.ticketChangeRedirect(event, 'BPM');
|
||||
this.ticketChangeRedirect(event);
|
||||
}
|
||||
|
||||
if (event.key.endsWith(JwtHelperService.USER_ACCESS_TOKEN) &&
|
||||
this.jwtHelperService.getValueFromToken(event.newValue, JwtHelperService.USER_PREFERRED_USERNAME) !==
|
||||
this.jwtHelperService.getValueFromToken(event.oldValue, JwtHelperService.USER_PREFERRED_USERNAME)) {
|
||||
this.ticketChangeRedirect(event, 'ALL');
|
||||
this.ticketChangeRedirect(event);
|
||||
}
|
||||
}
|
||||
|
||||
private ticketChangeRedirect(event: StorageEvent, provider: string) {
|
||||
private ticketChangeRedirect(event: StorageEvent) {
|
||||
if (!event.newValue) {
|
||||
this.redirectToUrl(provider, this.router.url);
|
||||
this.redirectToUrl(this.router.url);
|
||||
} else {
|
||||
window.location.reload();
|
||||
}
|
||||
@@ -71,7 +71,7 @@ export class AuthGuard extends AuthGuardBase {
|
||||
if (this.authenticationService.isLoggedIn() || this.withCredentials) {
|
||||
return true;
|
||||
}
|
||||
this.redirectToUrl('ALL', redirectUrl);
|
||||
this.redirectToUrl( redirectUrl);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user