-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the user preferences page [#723]
* Added a guard that checks if the user is logged in.
- Loading branch information
Showing
26 changed files
with
598 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2021, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { UserGuard } from './user.guard'; | ||
import { | ||
initialState as initialUserState, | ||
USER_FEATURE_KEY | ||
} from '@app/user/reducers/user.reducer'; | ||
import { MockStore, provideMockStore } from '@ngrx/store/testing'; | ||
import { Router } from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { LoggerModule } from '@angular-ru/logger'; | ||
import { Observable } from 'rxjs'; | ||
import { USER_ADMIN } from '@app/user/user.fixtures'; | ||
|
||
describe('UserGuard', () => { | ||
const USER = USER_ADMIN; | ||
const initialState = { | ||
[USER_FEATURE_KEY]: initialUserState | ||
}; | ||
|
||
let guard: UserGuard; | ||
let store: MockStore<any>; | ||
let router: Router; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
RouterTestingModule.withRoutes([{ path: '*', redirectTo: '' }]), | ||
LoggerModule.forRoot() | ||
], | ||
providers: [provideMockStore({ initialState })] | ||
}); | ||
guard = TestBed.inject(UserGuard); | ||
store = TestBed.inject(MockStore); | ||
router = TestBed.inject(Router); | ||
spyOn(router, 'navigateByUrl'); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
|
||
describe('when the authentication system is not initialized', () => { | ||
beforeEach(() => { | ||
store.setState({ | ||
...initialState, | ||
[USER_FEATURE_KEY]: { | ||
...initialUserState, | ||
initializing: true, | ||
authenticated: false, | ||
user: null | ||
} | ||
}); | ||
}); | ||
|
||
it('defers access', () => { | ||
(guard.canActivate( | ||
null, | ||
null | ||
) as Observable<boolean>).subscribe(response => | ||
expect(response).toBeTrue() | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
store.setState({ | ||
...initialState, | ||
[USER_FEATURE_KEY]: { | ||
...initialUserState, | ||
initializing: false, | ||
authenticated: true, | ||
user: USER_ADMIN | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when the user is not authenticated', () => { | ||
beforeEach(() => { | ||
store.setState({ | ||
...initialState, | ||
[USER_FEATURE_KEY]: { | ||
...initialUserState, | ||
initializing: false, | ||
authenticated: false, | ||
user: null | ||
} | ||
}); | ||
}); | ||
|
||
it('redirects the browser to the login form', () => { | ||
expect(router.navigateByUrl).toHaveBeenCalledWith('/login'); | ||
}); | ||
|
||
it('denies access', () => { | ||
expect(guard.canActivate(null, null)).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe('when the user is authenticated', () => { | ||
beforeEach(() => { | ||
store.setState({ | ||
...initialState, | ||
[USER_FEATURE_KEY]: { | ||
...initialUserState, | ||
initializing: false, | ||
authenticated: true, | ||
user: USER | ||
} | ||
}); | ||
}); | ||
|
||
it('allows access', () => { | ||
expect(guard.canActivate(null, null)).toBeTrue(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2021, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { | ||
ActivatedRouteSnapshot, | ||
CanActivate, | ||
Router, | ||
RouterStateSnapshot | ||
} from '@angular/router'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { LoggerService } from '@angular-ru/logger'; | ||
import { Store } from '@ngrx/store'; | ||
import { selectUserState } from '@app/user/selectors/user.selectors'; | ||
import { filter } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class UserGuard implements CanActivate { | ||
initialized = false; | ||
authenticated = false; | ||
hasUser = false; | ||
delayed$ = new Subject<boolean>(); | ||
|
||
constructor( | ||
private logger: LoggerService, | ||
private store: Store<any>, | ||
private router: Router | ||
) { | ||
this.store | ||
.select(selectUserState) | ||
.pipe(filter(state => !!state && !state.initializing)) | ||
.subscribe(state => { | ||
this.initialized = true; | ||
this.logger.debug('Guard: user state updated:', state); | ||
this.authenticated = state.authenticated; | ||
this.delayed$.next(this.authenticated); | ||
if (!this.authenticated) { | ||
this.router.navigateByUrl('/login'); | ||
} | ||
}); | ||
} | ||
|
||
canActivate( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot | ||
): Observable<boolean> | boolean { | ||
if (this.initialized) { | ||
return this.authenticated; | ||
} else { | ||
return this.delayed$.asObservable(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
comixed-web/src/app/user/pages/user-preferences-page/user-preferences-page.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<mat-table [dataSource]="dataSource" matSort> | ||
<ng-container matColumnDef="name"> | ||
<mat-header-cell mat-sort-header *matHeaderCellDef> | ||
{{ "user.user-preferences.label.preference-name" | translate }} | ||
</mat-header-cell> | ||
<mat-cell *matCellDef="let entry"> | ||
<span class="cx-width-100 cx-text-nowrap">{{ entry.name }}</span> | ||
</mat-cell> | ||
</ng-container> | ||
|
||
<ng-container matColumnDef="value"> | ||
<mat-header-cell mat-sort-header *matHeaderCellDef> | ||
{{ "user.user-preferences.label.preference-value" | translate }} | ||
</mat-header-cell> | ||
<mat-cell *matCellDef="let entry"> | ||
<span class="cx-width-100 cx-text-nowrap">{{ entry.value }}</span> | ||
</mat-cell> | ||
</ng-container> | ||
|
||
<ng-container matColumnDef="actions"> | ||
<mat-header-cell mat-sort-header *matHeaderCellDef></mat-header-cell> | ||
<mat-cell *matCellDef="let entry"> | ||
<button | ||
id="'delete-preference-value-' + entry.name + '-button'" | ||
mat-icon-button | ||
color="warn" | ||
[matTooltip]=" | ||
'user.user-preferences.tooltip.delete-preference' | translate | ||
" | ||
(click)="onDeletePreference(entry.name)" | ||
> | ||
<mat-icon>delete</mat-icon> | ||
</button> | ||
</mat-cell> | ||
</ng-container> | ||
|
||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> | ||
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row> | ||
</mat-table> |
14 changes: 14 additions & 0 deletions
14
comixed-web/src/app/user/pages/user-preferences-page/user-preferences-page.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@import 'styles'; | ||
|
||
.mat-column-name { | ||
width: $cx-column-large; | ||
max-width: $cx-column-large; | ||
} | ||
|
||
.mat-column-value { | ||
} | ||
|
||
.mat-column-actions { | ||
width: $cx-column-small; | ||
max-width: $cx-column-small; | ||
} |
Oops, something went wrong.