345 lines
8.6 KiB
TypeScript
Raw Normal View History

///
/// Copyright © 2016-2019 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
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 { 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';
@Injectable({
providedIn: 'root'
})
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) {
this.store.pipe(select(selectIsAuthenticated)).subscribe(
(authenticated: boolean) => {
if (authenticated) {
this.buildMenu();
}
}
);
}
private buildMenu() {
this.store.pipe(select(selectAuthUser), take(1)).subscribe(
(authUser: AuthUser) => {
if (authUser) {
let menuSections: Array<MenuSection>;
let homeSections: Array<HomeSection>;
switch (authUser.authority) {
case Authority.SYS_ADMIN:
menuSections = this.buildSysAdminMenu(authUser);
homeSections = this.buildSysAdminHome(authUser);
break;
case Authority.TENANT_ADMIN:
menuSections = this.buildTenantAdminMenu(authUser);
homeSections = this.buildTenantAdminHome(authUser);
break;
case Authority.CUSTOMER_USER:
menuSections = this.buildCustomerUserMenu(authUser);
homeSections = this.buildCustomerUserHome(authUser);
break;
}
this.menuSections$.next(menuSections);
this.homeSections$.next(homeSections);
}
}
);
}
private buildSysAdminMenu(authUser: any): Array<MenuSection> {
const sections: Array<MenuSection> = [];
sections.push(
{
name: 'home.home',
type: 'link',
path: '/home',
icon: 'home'
},
{
name: 'tenant.tenants',
type: 'link',
path: '/tenants',
icon: 'supervisor_account'
},
{
name: 'widget.widget-library',
type: 'link',
path: '/widgets-bundles',
icon: 'now_widgets'
},
{
name: 'admin.system-settings',
type: 'toggle',
path: '/settings',
height: '120px',
icon: 'settings',
pages: [
{
name: 'admin.general',
type: 'link',
path: '/settings/general',
icon: 'settings_applications'
},
{
name: 'admin.outgoing-mail',
type: 'link',
path: '/settings/outgoing-mail',
icon: 'mail'
},
{
name: 'admin.security-settings',
type: 'link',
path: '/settings/security-settings',
icon: 'security'
}
]
}
);
return sections;
}
private buildSysAdminHome(authUser: any): Array<HomeSection> {
const homeSections: Array<HomeSection> = [];
homeSections.push(
{
name: 'tenant.management',
places: [
{
name: 'tenant.tenants',
icon: 'supervisor_account',
path: '/tenants'
}
]
},
{
name: 'widget.management',
places: [
{
name: 'widget.widget-library',
icon: 'now_widgets',
path: '/widgets-bundles'
}
]
},
{
name: 'admin.system-settings',
places: [
{
name: 'admin.general',
icon: 'settings_applications',
path: '/settings/general'
},
{
name: 'admin.outgoing-mail',
icon: 'mail',
path: '/settings/outgoing-mail'
},
{
name: 'admin.security-settings',
icon: 'security',
path: '/settings/security-settings'
}
]
}
);
return homeSections;
}
private buildTenantAdminMenu(authUser: any): Array<MenuSection> {
const sections: Array<MenuSection> = [];
sections.push(
{
name: 'home.home',
type: 'link',
path: '/home',
icon: 'home'
},
{
name: 'rulechain.rulechains',
type: 'link',
path: '/ruleChains',
icon: 'settings_ethernet'
},
{
name: 'customer.customers',
type: 'link',
path: '/customers',
icon: 'supervisor_account'
},
{
name: 'asset.assets',
type: 'link',
path: '/assets',
icon: 'domain'
},
{
name: 'device.devices',
type: 'link',
path: '/devices',
icon: 'devices_other'
},
{
name: 'entity-view.entity-views',
type: 'link',
path: '/entityViews',
icon: 'view_quilt'
},
{
name: 'widget.widget-library',
type: 'link',
path: '/widgets-bundles',
icon: 'now_widgets'
},
{
name: 'dashboard.dashboards',
type: 'link',
path: '/dashboards',
icon: 'dashboards'
},
{
name: 'audit-log.audit-logs',
type: 'link',
path: '/auditLogs',
icon: 'track_changes'
}
);
return sections;
}
private buildTenantAdminHome(authUser: any): Array<HomeSection> {
const homeSections: Array<HomeSection> = [];
homeSections.push(
{
name: 'rulechain.management',
places: [
{
name: 'rulechain.rulechains',
icon: 'settings_ethernet',
path: '/ruleChains'
}
]
},
{
name: 'customer.management',
places: [
{
name: 'customer.customers',
icon: 'supervisor_account',
path: '/customers'
}
]
},
{
name: 'asset.management',
places: [
{
name: 'asset.assets',
icon: 'domain',
path: '/assets'
}
]
},
{
name: 'device.management',
places: [
{
name: 'device.devices',
icon: 'devices_other',
path: '/devices'
}
]
},
{
name: 'entity-view.management',
places: [
{
name: 'entity-view.entity-views',
icon: 'view_quilt',
path: '/entityViews'
}
]
},
{
name: 'dashboard.management',
places: [
{
name: 'widget.widget-library',
icon: 'now_widgets',
path: '/widgets-bundles'
},
{
name: 'dashboard.dashboards',
icon: 'dashboard',
path: '/dashboards'
}
]
},
{
name: 'audit-log.audit',
places: [
{
name: 'audit-log.audit-logs',
icon: 'track_changes',
path: '/auditLogs'
}
]
}
);
return homeSections;
}
private buildCustomerUserMenu(authUser: any): Array<MenuSection> {
const sections: Array<MenuSection> = [];
sections.push(
{
name: 'home.home',
type: 'link',
path: '/home',
icon: 'home'
}
);
// TODO:
return sections;
}
private buildCustomerUserHome(authUser: any): Array<HomeSection> {
const homeSections: Array<HomeSection> = [];
// TODO:
return homeSections;
}
public menuSections(): Observable<Array<MenuSection>> {
return this.menuSections$;
}
public homeSections(): Observable<Array<HomeSection>> {
return this.homeSections$;
}
}