Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 7c3f50a

Browse files
ghillertcppwfs
authored andcommitted
gh-559 Fix: Tabs don't show up after authentication
- Ensure that roles are checked after login - Capture pre-existing `display` css property (used for reset) - Use a `Subject` instead of relying on `ngDoCheck` to react to role-changes gh-559 Ensure all tests pass
1 parent 349bb74 commit 7c3f50a

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

ui/src/app/about/about-details.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('AboutDetailsComponent', () => {
134134

135135
de = fixture.debugElement.query(By.css('h2[id=serverWarningError]'));
136136
el = de.nativeElement;
137-
expect(el.textContent).toContain('Error occurred when attempting to obtain about info from server.');
137+
expect(el.textContent).toContain('Obtaining about info from server.');
138138
});
139139

140140
it('Should navigate to the details page.', () => {

ui/src/app/about/about.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('AboutComponent', () => {
4646
fixture.detectChanges();
4747
expect(component).toBeTruthy();
4848
let des: DebugElement[] = fixture.debugElement.queryAll(By.css('table[id=dataFlowVersionTable] td'));
49-
expect(des.length).toBe(4);
49+
expect(des.length).toBe(8);
5050
expect(des[0].nativeElement.textContent).toContain('Name');
5151
expect(des[1].nativeElement.textContent).toContain('FOO');
5252
expect(des[2].nativeElement.textContent).toContain('Version');
@@ -77,7 +77,7 @@ describe('AboutComponent', () => {
7777

7878
de = fixture.debugElement.query(By.css('h2[id=serverWarningError]'));
7979
el = de.nativeElement;
80-
expect(el.textContent).toContain('Error occurred when attempting to obtain about info from server.');
80+
expect(el.textContent).toContain('Obtaining about info from server.');
8181
});
8282

8383
it('Should navigate to the details page.', () => {

ui/src/app/auth/auth.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { LoginRequest } from './model/login-request.model';
99
import { ErrorHandler } from '../shared/model/error-handler';
1010
import { HttpUtils } from '../shared/support/http.utils';
1111
import { SecurityAwareRequestOptions } from './support/security-aware-request-options';
12+
import { Output } from '@angular/core';
13+
import { EventEmitter } from '@angular/core';
14+
import { Subject } from 'rxjs/Subject';
1215

1316
/**
1417
* The AuthService deals with all security-related services:
@@ -29,6 +32,7 @@ export class AuthService {
2932
private readonly xAuthTokenKeyName = 'xAuthToken';
3033

3134
public securityInfo: SecurityInfo;
35+
public securityInfoSubject = new Subject<SecurityInfo>();
3236

3337
constructor(
3438
private http: Http,
@@ -60,8 +64,8 @@ export class AuthService {
6064
.map(response => {
6165
const body = response.json();
6266
this.securityInfo = new SecurityInfo().deserialize(body);
67+
this.securityInfoSubject.next(this.securityInfo);
6368
console.log('SecurityInfo:', this.securityInfo);
64-
6569
if (!this.securityInfo.isAuthenticationEnabled
6670
&& requestOptions.xAuthToken) {
6771
requestOptions.xAuthToken = undefined;
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { AfterViewInit, Directive, ElementRef, Input, Output, EventEmitter, HostListener,
2-
DoCheck, Renderer2 } from '@angular/core';
2+
Renderer2 } from '@angular/core';
33

44
import { AuthService } from '../auth.service';
5+
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
56

67
/**
78
* This directive will show or hide the element depending whether
@@ -13,34 +14,39 @@ import { AuthService } from '../auth.service';
1314
@Directive({
1415
selector: '[appRoles]'
1516
})
16-
export class RolesDirective implements AfterViewInit, DoCheck {
17+
export class RolesDirective implements AfterViewInit, OnInit {
1718

1819
@Input()
1920
public appRoles: string[];
2021

22+
private existingDisplayPropertyValue: string;
2123
constructor(private authService: AuthService, private elem: ElementRef, private renderer: Renderer2) {
2224
}
2325

2426
private checkRoles() {
2527
const found = this.authService.securityInfo.canAccess(this.appRoles);
26-
2728
if (!found) {
2829
this.renderer.setStyle(this.elem.nativeElement, 'display', 'none');
30+
} else {
31+
if (this.existingDisplayPropertyValue) {
32+
this.renderer.setStyle(this.elem.nativeElement, 'display', this.existingDisplayPropertyValue);
33+
} else {
34+
this.renderer.removeStyle(this.elem.nativeElement, 'display');
35+
}
2936
}
3037
}
3138

3239
/**
3340
* Initializes the state element and calls checkRoles().
3441
*/
3542
ngAfterViewInit() {
43+
this.existingDisplayPropertyValue = this.elem.nativeElement.style.display;
3644
this.checkRoles();
3745
}
3846

39-
/**
40-
* Called when Angular dirty checks a directive.
41-
* Will in return call checkRoles().
42-
*/
43-
ngDoCheck() {
44-
this.checkRoles();
47+
ngOnInit() {
48+
this.authService.securityInfoSubject.forEach(event => {
49+
this.checkRoles();
50+
});
4551
}
4652
}

ui/src/app/tests/mocks/auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Observable } from 'rxjs/Observable';
2+
import { Subject } from 'rxjs/Subject';
23

34
import { SecurityInfo } from '../../auth/model/security-info.model';
45
import { LoginRequest } from '../../auth/model/login-request.model';
@@ -11,6 +12,7 @@ import { LoginRequest } from '../../auth/model/login-request.model';
1112
export class MockAuthService {
1213

1314
public securityInfo = new SecurityInfo();
15+
public securityInfoSubject = new Subject<SecurityInfo>();
1416

1517
login(loginRequest: LoginRequest): Observable<SecurityInfo> {
1618

0 commit comments

Comments
 (0)