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';
export interface AuthPayload {
authUser: AuthUser;
userDetails: User;
export interface SysParamsState {
userTokenAccessEnabled: boolean;
allowedDashboardIds: string[];
edgesSupportEnabled: boolean;
}
export interface AuthPayload extends SysParamsState {
authUser: AuthUser;
userDetails: User;
forceFullscreen: boolean;
}
export interface AuthState {
export interface AuthState extends AuthPayload {
isAuthenticated: boolean;
isUserLoaded: boolean;
authUser: AuthUser;
userDetails: User;
userTokenAccessEnabled: boolean;
allowedDashboardIds: string[];
forceFullscreen: boolean;
lastPublicDashboardId: string;
}

View File

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

View File

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

View File

@ -15,7 +15,7 @@
///
import { Injectable } from '@angular/core';
import { defaultHttpOptions, defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
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 { EntityId } from "@shared/models/id/entity-id";
import { Event } from "@shared/models/event.models";
import { map } from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class EdgeService {
private edgesSupportEnabled = false;
constructor(
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>> {
return this.http.get<Array<Edge>>(`/api/edges?edgeIds=${edgeIds.join(',')}`,
defaultHttpOptionsFromConfig(config));

View File

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