mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-10-29 18:03:45 +08:00
feat(authentication): permission check and menu
CB-110
This commit is contained in:
@@ -6,21 +6,21 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
*/
|
||||
|
||||
import { SettingsMenuService } from '@dbeaver/core/app';
|
||||
import { SettingsMenuService, ScreenService, AppScreenService } from '@dbeaver/core/app';
|
||||
import { injectable, Bootstrap } from '@dbeaver/core/di';
|
||||
import { PermissionsService } from '@dbeaver/core/root';
|
||||
|
||||
import { AdministrationScreenService } from './AdministrationScreen/AdministrationScreenService';
|
||||
|
||||
export const ADMINISTRATION_PERMISSION = 'admin';
|
||||
import { EAdminPermission } from './EAdminPermission';
|
||||
|
||||
@injectable()
|
||||
export class AdministrationMenuService extends Bootstrap {
|
||||
static administrationMenuToken = 'administrationMenu';
|
||||
constructor(
|
||||
private settingsMenuService: SettingsMenuService,
|
||||
private permissionsService: PermissionsService,
|
||||
private screenService: ScreenService,
|
||||
private administrationScreenService: AdministrationScreenService,
|
||||
private appScreenService: AppScreenService
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@@ -29,12 +29,23 @@ export class AdministrationMenuService extends Bootstrap {
|
||||
this.settingsMenuService.addMenuItem(
|
||||
SettingsMenuService.settingsMenuToken,
|
||||
{
|
||||
id: AdministrationMenuService.administrationMenuToken,
|
||||
id: 'administrationMenuEnter',
|
||||
order: 0,
|
||||
isHidden: () => !this.permissionsService.has(ADMINISTRATION_PERMISSION),
|
||||
isHidden: () => !this.permissionsService.has(EAdminPermission.admin)
|
||||
|| this.screenService.isActive(AdministrationScreenService.screenName),
|
||||
title: 'administration_menu_enter',
|
||||
onClick: () => this.administrationScreenService.navigateToRoot(),
|
||||
}
|
||||
);
|
||||
this.settingsMenuService.addMenuItem(
|
||||
SettingsMenuService.settingsMenuToken,
|
||||
{
|
||||
id: 'administrationMenuBack',
|
||||
order: 0,
|
||||
isHidden: () => !this.screenService.isActive(AdministrationScreenService.screenName),
|
||||
title: 'administration_menu_back',
|
||||
onClick: () => this.appScreenService.navigateToRoot(),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,21 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
*/
|
||||
|
||||
import { observer } from 'mobx-react';
|
||||
|
||||
import { usePermission } from '@dbeaver/core/root';
|
||||
|
||||
import { EAdminPermission } from '../EAdminPermission';
|
||||
import { AdministrationTopAppBar } from './AdministrationTopAppBar/AdministrationTopAppBar';
|
||||
|
||||
export function AdministrationScreen() {
|
||||
export const AdministrationScreen = observer(function AdministrationScreen() {
|
||||
if (!usePermission(EAdminPermission.admin)) {
|
||||
return <>You has no permission</>;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<AdministrationTopAppBar />
|
||||
</>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
+18
-2
@@ -6,9 +6,11 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
*/
|
||||
|
||||
import { ScreenService, RouterService } from '@dbeaver/core/app';
|
||||
import { ScreenService, RouterService, AppScreenService } from '@dbeaver/core/app';
|
||||
import { injectable, Bootstrap } from '@dbeaver/core/di';
|
||||
import { PermissionsService } from '@dbeaver/core/root';
|
||||
|
||||
import { EAdminPermission } from '../EAdminPermission';
|
||||
import { AdministrationScreen } from './AdministrationScreen';
|
||||
|
||||
@injectable()
|
||||
@@ -19,8 +21,11 @@ export class AdministrationScreenService extends Bootstrap {
|
||||
constructor(
|
||||
private screenService: ScreenService,
|
||||
private routerService: RouterService,
|
||||
private permissionsService: PermissionsService,
|
||||
private appScreenService: AppScreenService
|
||||
) {
|
||||
super();
|
||||
this.permissionsService.onUpdate.subscribe(this.handleActivate.bind(this));
|
||||
}
|
||||
|
||||
navigateToRoot() {
|
||||
@@ -28,6 +33,17 @@ export class AdministrationScreenService extends Bootstrap {
|
||||
}
|
||||
|
||||
bootstrap() {
|
||||
this.screenService.add({ name: AdministrationScreenService.screenName, path: '/admin', component: AdministrationScreen });
|
||||
this.screenService.create({
|
||||
name: AdministrationScreenService.screenName,
|
||||
path: '/admin',
|
||||
component: AdministrationScreen,
|
||||
onActivate: this.handleActivate.bind(this),
|
||||
});
|
||||
}
|
||||
|
||||
private handleActivate() {
|
||||
if (!this.permissionsService.has(EAdminPermission.admin)) {
|
||||
this.appScreenService.navigateToRoot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* cloudbeaver - Cloud Database Manager
|
||||
* Copyright (C) 2020 DBeaver Corp and others
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0.
|
||||
* you may not use this file except in compliance with the License.
|
||||
*/
|
||||
|
||||
export enum EAdminPermission {
|
||||
admin = 'admin'
|
||||
}
|
||||
@@ -42,6 +42,7 @@ module.exports = {
|
||||
data_transfer_exporting_sql: 'SQL:',
|
||||
|
||||
administration_menu_enter: 'Administration',
|
||||
administration_menu_back: 'Back to app',
|
||||
|
||||
authentication_login_dialog_title: 'Authentication',
|
||||
authentication_login: 'Login',
|
||||
|
||||
@@ -41,6 +41,7 @@ module.exports = {
|
||||
data_transfer_exporting_sql: 'SQL:',
|
||||
|
||||
administration_menu_enter: 'Администрирование',
|
||||
administration_menu_back: 'Вернуться в приложение',
|
||||
|
||||
authentication_login_dialog_title: 'Авторизация',
|
||||
authentication_login: 'Войти',
|
||||
|
||||
@@ -27,6 +27,6 @@ export class AppScreenService {
|
||||
}
|
||||
|
||||
register() {
|
||||
this.screenService.add({ name: AppScreenService.screenName, path: '/', component: AppScreen });
|
||||
this.screenService.create({ name: AppScreenService.screenName, path: '/', component: AppScreen });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export class ScreenService {
|
||||
this.routerService.subscribe(this.onRouteChange.bind(this));
|
||||
}
|
||||
|
||||
add(screen: IScreen) {
|
||||
create(screen: IScreen) {
|
||||
if (this.screens.has(screen.name)) {
|
||||
return;
|
||||
}
|
||||
@@ -37,6 +37,14 @@ export class ScreenService {
|
||||
this.routerService.router.add({ name: screen.name, path: screen.path });
|
||||
}
|
||||
|
||||
isActive(name: string, strict = false): boolean {
|
||||
if (strict) {
|
||||
return this.screen?.name === name;
|
||||
}
|
||||
|
||||
return this.screen?.name.startsWith(name) || false;
|
||||
}
|
||||
|
||||
buildUrl(screen: string) {
|
||||
return this.routerService.router.buildUrl(screen);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user