Introduced SysParamsState. Refactored hide menu approach

This commit is contained in:
Volodymyr Babak 2020-12-20 19:47:26 +02:00
parent 9089163880
commit b1f4e43e8f
5 changed files with 49 additions and 62 deletions

View File

@ -16,21 +16,18 @@
import { AuthUser, User } from '@shared/models/user.model'; import { AuthUser, User } from '@shared/models/user.model';
export interface AuthPayload { export interface SysParamsState {
authUser: AuthUser;
userDetails: User;
userTokenAccessEnabled: boolean; userTokenAccessEnabled: boolean;
allowedDashboardIds: string[]; allowedDashboardIds: string[];
edgesSupportEnabled: boolean;
}
export interface AuthPayload extends SysParamsState {
authUser: AuthUser;
userDetails: User;
forceFullscreen: boolean; forceFullscreen: boolean;
} }
export interface AuthState extends AuthPayload {
export interface AuthState {
isAuthenticated: boolean; isAuthenticated: boolean;
isUserLoaded: boolean; isUserLoaded: boolean;
authUser: AuthUser;
userDetails: User;
userTokenAccessEnabled: boolean;
allowedDashboardIds: string[];
forceFullscreen: boolean;
lastPublicDashboardId: string; lastPublicDashboardId: string;
} }

View File

@ -22,7 +22,8 @@ const emptyUserAuthState: AuthPayload = {
userDetails: null, userDetails: null,
userTokenAccessEnabled: false, userTokenAccessEnabled: false,
forceFullscreen: false, forceFullscreen: false,
allowedDashboardIds: [] allowedDashboardIds: [],
edgesSupportEnabled: false
}; };
export const initialState: AuthState = { export const initialState: AuthState = {

View File

@ -31,7 +31,7 @@ import { ActionAuthAuthenticated, ActionAuthLoadUser, ActionAuthUnauthenticated
import { getCurrentAuthState, getCurrentAuthUser } from './auth.selectors'; import { getCurrentAuthState, getCurrentAuthUser } from './auth.selectors';
import { Authority } from '@shared/models/authority.enum'; import { Authority } from '@shared/models/authority.enum';
import { ActionSettingsChangeLanguage } from '@app/core/settings/settings.actions'; import { ActionSettingsChangeLanguage } from '@app/core/settings/settings.actions';
import { AuthPayload, AuthState } from '@core/auth/auth.models'; import { AuthPayload, AuthState, SysParamsState } from '@core/auth/auth.models';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { AuthUser } from '@shared/models/user.model'; import { AuthUser } from '@shared/models/user.model';
import { TimeService } from '@core/services/time.service'; import { TimeService } from '@core/services/time.service';
@ -64,8 +64,7 @@ export class AuthService {
private dashboardService: DashboardService, private dashboardService: DashboardService,
private adminService: AdminService, private adminService: AdminService,
private translate: TranslateService, private translate: TranslateService,
private dialog: MatDialog, private dialog: MatDialog
private edgeService: EdgeService
) { ) {
} }
@ -427,17 +426,24 @@ export class AuthService {
} }
} }
private loadSystemParams(authPayload: AuthPayload): Observable<any> { public loadIsEdgesSupportEnabled(): Observable<boolean> {
const sources: Array<Observable<any>> = [this.loadIsUserTokenAccessEnabled(authPayload.authUser), return this.http.get<boolean>('/api/edges/enabled', defaultHttpOptions());
this.fetchAllowedDashboardIds(authPayload), }
this.timeService.loadMaxDatapointsLimit(),
this.edgeService.loadIsEdgesSupportEnabled()]; private loadSystemParams(authPayload: AuthPayload): Observable<SysParamsState> {
const sources = [this.loadIsUserTokenAccessEnabled(authPayload.authUser),
this.fetchAllowedDashboardIds(authPayload),
this.loadIsEdgesSupportEnabled(),
this.timeService.loadMaxDatapointsLimit()];
return forkJoin(sources) return forkJoin(sources)
.pipe(map((data) => { .pipe(map((data) => {
const userTokenAccessEnabled: boolean = data[0]; const userTokenAccessEnabled: boolean = data[0] as boolean;
const allowedDashboardIds: string[] = data[1]; const allowedDashboardIds: string[] = data[1] as string[];
return {userTokenAccessEnabled, allowedDashboardIds}; const edgesSupportEnabled: boolean = data[2] as boolean;
})); return {userTokenAccessEnabled, allowedDashboardIds, edgesSupportEnabled};
}, catchError((err) => {
return of({});
})));
} }
public refreshJwtToken(loadUserElseStoreJwtToken = true): Observable<LoginResponse> { public refreshJwtToken(loadUserElseStoreJwtToken = true): Observable<LoginResponse> {

View File

@ -15,7 +15,7 @@
/// ///
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { defaultHttpOptions, defaultHttpOptionsFromConfig, RequestConfig } from './http-utils'; import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { PageLink, TimePageLink } from '@shared/models/page/page-link'; import { PageLink, TimePageLink } from '@shared/models/page/page-link';
@ -24,32 +24,15 @@ import { EntitySubtype } from '@app/shared/models/entity-type.models';
import { Edge, EdgeInfo, EdgeSearchQuery } from "@shared/models/edge.models"; import { Edge, EdgeInfo, EdgeSearchQuery } from "@shared/models/edge.models";
import { EntityId } from "@shared/models/id/entity-id"; import { EntityId } from "@shared/models/id/entity-id";
import { Event } from "@shared/models/event.models"; import { Event } from "@shared/models/event.models";
import { map } from "rxjs/operators";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class EdgeService { export class EdgeService {
private edgesSupportEnabled = false;
constructor( constructor(
private http: HttpClient private http: HttpClient
) { } ) { }
public isEdgesSupportEnabled(): boolean {
return this.edgesSupportEnabled;
}
public loadIsEdgesSupportEnabled(): Observable<boolean> {
return this.http.get<boolean>('/api/edges/enabled',
defaultHttpOptions(true)).pipe(
map( (enabled) => {
this.edgesSupportEnabled = enabled;
return this.edgesSupportEnabled;
})
);
}
public getEdges(edgeIds: Array<string>, config?: RequestConfig): Observable<Array<Edge>> { public getEdges(edgeIds: Array<string>, config?: RequestConfig): Observable<Array<Edge>> {
return this.http.get<Array<Edge>>(`/api/edges?edgeIds=${edgeIds.join(',')}`, return this.http.get<Array<Edge>>(`/api/edges?edgeIds=${edgeIds.join(',')}`,
defaultHttpOptionsFromConfig(config)); defaultHttpOptionsFromConfig(config));

View File

@ -18,14 +18,14 @@ import { Injectable } from '@angular/core';
import { AuthService } from '../auth/auth.service'; import { AuthService } from '../auth/auth.service';
import { select, Store } from '@ngrx/store'; import { select, Store } from '@ngrx/store';
import { AppState } from '../core.state'; import { AppState } from '../core.state';
import { selectAuthUser, selectIsAuthenticated } from '../auth/auth.selectors'; import {selectAuth, selectAuthUser, selectIsAuthenticated} from '../auth/auth.selectors';
import { take } from 'rxjs/operators'; import { take } from 'rxjs/operators';
import { HomeSection, MenuSection } from '@core/services/menu.models'; import { HomeSection, MenuSection } from '@core/services/menu.models';
import { BehaviorSubject, Observable, Subject } from 'rxjs'; import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { Authority } from '@shared/models/authority.enum'; import { Authority } from '@shared/models/authority.enum';
import { AuthUser } from '@shared/models/user.model'; import { AuthUser } from '@shared/models/user.model';
import { guid } from '@core/utils'; import { guid } from '@core/utils';
import { EdgeService } from '../http/edge.service'; import {AuthState} from "@core/auth/auth.models";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -35,7 +35,7 @@ export class MenuService {
menuSections$: Subject<Array<MenuSection>> = new BehaviorSubject<Array<MenuSection>>([]); menuSections$: Subject<Array<MenuSection>> = new BehaviorSubject<Array<MenuSection>>([]);
homeSections$: Subject<Array<HomeSection>> = new BehaviorSubject<Array<HomeSection>>([]); homeSections$: Subject<Array<HomeSection>> = new BehaviorSubject<Array<HomeSection>>([]);
constructor(private store: Store<AppState>, private authService: AuthService, private edgeService: EdgeService) { constructor(private store: Store<AppState>, private authService: AuthService) {
this.store.pipe(select(selectIsAuthenticated)).subscribe( this.store.pipe(select(selectIsAuthenticated)).subscribe(
(authenticated: boolean) => { (authenticated: boolean) => {
if (authenticated) { if (authenticated) {
@ -46,23 +46,23 @@ export class MenuService {
} }
private buildMenu() { private buildMenu() {
this.store.pipe(select(selectAuthUser), take(1)).subscribe( this.store.pipe(select(selectAuth), take(1)).subscribe(
(authUser: AuthUser) => { (authState: AuthState) => {
if (authUser) { if (authState.authUser) {
let menuSections: Array<MenuSection>; let menuSections: Array<MenuSection>;
let homeSections: Array<HomeSection>; let homeSections: Array<HomeSection>;
switch (authUser.authority) { switch (authState.authUser.authority) {
case Authority.SYS_ADMIN: case Authority.SYS_ADMIN:
menuSections = this.buildSysAdminMenu(authUser); menuSections = this.buildSysAdminMenu(authState);
homeSections = this.buildSysAdminHome(authUser); homeSections = this.buildSysAdminHome(authState);
break; break;
case Authority.TENANT_ADMIN: case Authority.TENANT_ADMIN:
menuSections = this.buildTenantAdminMenu(authUser); menuSections = this.buildTenantAdminMenu(authState);
homeSections = this.buildTenantAdminHome(authUser); homeSections = this.buildTenantAdminHome(authState);
break; break;
case Authority.CUSTOMER_USER: case Authority.CUSTOMER_USER:
menuSections = this.buildCustomerUserMenu(authUser); menuSections = this.buildCustomerUserMenu(authState);
homeSections = this.buildCustomerUserHome(authUser); homeSections = this.buildCustomerUserHome(authState);
break; break;
} }
this.menuSections$.next(menuSections); this.menuSections$.next(menuSections);
@ -72,7 +72,7 @@ export class MenuService {
); );
} }
private buildSysAdminMenu(authUser: any): Array<MenuSection> { private buildSysAdminMenu(authState: AuthState): Array<MenuSection> {
const sections: Array<MenuSection> = []; const sections: Array<MenuSection> = [];
sections.push( sections.push(
{ {
@ -153,7 +153,7 @@ export class MenuService {
return sections; return sections;
} }
private buildSysAdminHome(authUser: any): Array<HomeSection> { private buildSysAdminHome(authState: AuthState): Array<HomeSection> {
const homeSections: Array<HomeSection> = []; const homeSections: Array<HomeSection> = [];
homeSections.push( homeSections.push(
{ {
@ -216,7 +216,7 @@ export class MenuService {
return homeSections; return homeSections;
} }
private buildTenantAdminMenu(authUser: any): Array<MenuSection> { private buildTenantAdminMenu(authState: AuthState): Array<MenuSection> {
const sections: Array<MenuSection> = []; const sections: Array<MenuSection> = [];
sections.push( sections.push(
{ {
@ -270,7 +270,7 @@ export class MenuService {
icon: 'view_quilt' icon: 'view_quilt'
} }
); );
if (this.edgeService.isEdgesSupportEnabled()) { if (authState.edgesSupportEnabled) {
sections.push( sections.push(
{ {
id: guid(), id: guid(),
@ -332,7 +332,7 @@ export class MenuService {
return sections; return sections;
} }
private buildTenantAdminHome(authUser: any): Array<HomeSection> { private buildTenantAdminHome(authState: AuthState): Array<HomeSection> {
const homeSections: Array<HomeSection> = []; const homeSections: Array<HomeSection> = [];
homeSections.push( homeSections.push(
{ {
@ -392,7 +392,7 @@ export class MenuService {
] ]
} }
); );
if (this.edgeService.isEdgesSupportEnabled()) { if (authState.edgesSupportEnabled) {
homeSections.push( homeSections.push(
{ {
name: 'edge.management', name: 'edge.management',
@ -446,7 +446,7 @@ export class MenuService {
return homeSections; return homeSections;
} }
private buildCustomerUserMenu(authUser: any): Array<MenuSection> { private buildCustomerUserMenu(authState: AuthState): Array<MenuSection> {
const sections: Array<MenuSection> = []; const sections: Array<MenuSection> = [];
sections.push( sections.push(
{ {
@ -488,7 +488,7 @@ export class MenuService {
return sections; return sections;
} }
private buildCustomerUserHome(authUser: any): Array<HomeSection> { private buildCustomerUserHome(authState: AuthState): Array<HomeSection> {
const homeSections: Array<HomeSection> = [ const homeSections: Array<HomeSection> = [
{ {
name: 'asset.view-assets', name: 'asset.view-assets',