From 396bdeba47a17c63f082a6c972dd332bc2021516 Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Fri, 25 Jan 2019 17:44:41 +0200 Subject: [PATCH 1/5] Refactor Policy and OrganizationStatus services --- .../dashboards/edit-dashboard-dialog.js | 5 +- .../app/components/empty-state/empty-state.js | 13 +-- client/app/config/index.js | 8 +- client/app/pages/dashboards/dashboard.js | 4 +- client/app/pages/data-sources/list.js | 5 +- client/app/pages/queries/view.js | 4 +- client/app/pages/users/list.js | 5 +- client/app/services/ng.js | 2 + client/app/services/organization-status.js | 22 ----- client/app/services/organizationStatus.js | 17 ++++ client/app/services/policy.js | 84 ------------------- client/app/services/policy/DefaultPolicy.js | 62 ++++++++++++++ client/app/services/policy/Policy.js | 56 +++++++++++++ client/app/services/policy/index.js | 68 +++++++++++++++ 14 files changed, 228 insertions(+), 127 deletions(-) delete mode 100644 client/app/services/organization-status.js create mode 100644 client/app/services/organizationStatus.js delete mode 100644 client/app/services/policy.js create mode 100644 client/app/services/policy/DefaultPolicy.js create mode 100644 client/app/services/policy/Policy.js create mode 100644 client/app/services/policy/index.js diff --git a/client/app/components/dashboards/edit-dashboard-dialog.js b/client/app/components/dashboards/edit-dashboard-dialog.js index 2253f1b067..82988b24a6 100644 --- a/client/app/components/dashboards/edit-dashboard-dialog.js +++ b/client/app/components/dashboards/edit-dashboard-dialog.js @@ -1,4 +1,5 @@ import { isEmpty } from 'lodash'; +import policy from '@/services/policy'; import template from './edit-dashboard-dialog.html'; const EditDashboardDialog = { @@ -8,11 +9,11 @@ const EditDashboardDialog = { dismiss: '&', }, template, - controller($location, $http, Policy, Events) { + controller($location, $http, Events) { 'ngInject'; this.dashboard = this.resolve.dashboard; - this.policy = Policy; + this.policy = policy; this.isFormValid = () => !isEmpty(this.dashboard.name); diff --git a/client/app/components/empty-state/empty-state.js b/client/app/components/empty-state/empty-state.js index 98af5b1d7c..5db8948af4 100644 --- a/client/app/components/empty-state/empty-state.js +++ b/client/app/components/empty-state/empty-state.js @@ -1,3 +1,4 @@ +import organizationStatus from '@/services/organizationStatus'; import './empty-state.less'; import template from './empty-state.html'; @@ -15,14 +16,14 @@ const EmptyStateComponent = { showInviteStep: '<', onboardingMode: '<', }, - controller($uibModal, OrganizationStatus, currentUser) { + controller($uibModal, currentUser) { this.isAdmin = currentUser.isAdmin; - this.dataSourceStepCompleted = OrganizationStatus.objectCounters.data_sources > 0; - this.queryStepCompleted = OrganizationStatus.objectCounters.queries > 0; - this.dashboardStepCompleted = OrganizationStatus.objectCounters.dashboards > 0; - this.alertStepCompleted = OrganizationStatus.objectCounters.alerts > 0; - this.inviteStepCompleted = OrganizationStatus.objectCounters.users > 1; + this.dataSourceStepCompleted = organizationStatus.objectCounters.data_sources > 0; + this.queryStepCompleted = organizationStatus.objectCounters.queries > 0; + this.dashboardStepCompleted = organizationStatus.objectCounters.dashboards > 0; + this.alertStepCompleted = organizationStatus.objectCounters.alerts > 0; + this.inviteStepCompleted = organizationStatus.objectCounters.users > 1; this.shouldShowOnboarding = () => { if (!this.onboardingMode) { diff --git a/client/app/config/index.js b/client/app/config/index.js index ee8905009d..2ce76b5e69 100644 --- a/client/app/config/index.js +++ b/client/app/config/index.js @@ -24,6 +24,8 @@ import { each, isFunction, extend } from 'lodash'; import '@/lib/sortable'; +import organizationStatus from '@/services/organizationStatus'; + import * as filters from '@/filters'; import registerDirectives from '@/directives'; import markdownFilter from '@/filters/markdown'; @@ -110,11 +112,7 @@ function registerPages() { route.authenticated = true; route.resolve = extend( { - __organizationStatus: (OrganizationStatus) => { - 'ngInject'; - - return OrganizationStatus.refresh(); - }, + __organizationStatus: () => organizationStatus.refresh(), }, route.resolve, ); diff --git a/client/app/pages/dashboards/dashboard.js b/client/app/pages/dashboards/dashboard.js index 9e77d1e57a..951f80c0ad 100644 --- a/client/app/pages/dashboards/dashboard.js +++ b/client/app/pages/dashboards/dashboard.js @@ -1,6 +1,7 @@ import * as _ from 'lodash'; import PromiseRejectionError from '@/lib/promise-rejection-error'; import getTags from '@/services/getTags'; +import policy from '@/services/policy'; import { durationHumanize } from '@/filters'; import template from './dashboard.html'; import shareDashboardTemplate from './share-dashboard.html'; @@ -36,7 +37,6 @@ function DashboardCtrl( clientConfig, Events, toastr, - Policy, ) { this.saveInProgress = false; @@ -83,7 +83,7 @@ function DashboardCtrl( enabled: true, })); - const allowedIntervals = Policy.getDashboardRefreshIntervals(); + const allowedIntervals = policy.getDashboardRefreshIntervals(); if (_.isArray(allowedIntervals)) { _.each(this.refreshRates, (rate) => { rate.enabled = allowedIntervals.indexOf(rate.rate) >= 0; diff --git a/client/app/pages/data-sources/list.js b/client/app/pages/data-sources/list.js index f1d64e1e5d..20cf409c43 100644 --- a/client/app/pages/data-sources/list.js +++ b/client/app/pages/data-sources/list.js @@ -1,8 +1,9 @@ import settingsMenu from '@/services/settingsMenu'; +import policy from '@/services/policy'; import template from './list.html'; -function DataSourcesCtrl(Policy, DataSource) { - this.policy = Policy; +function DataSourcesCtrl(DataSource) { + this.policy = policy; this.dataSources = DataSource.query(); } diff --git a/client/app/pages/queries/view.js b/client/app/pages/queries/view.js index 80b7942b3f..ba6316d819 100644 --- a/client/app/pages/queries/view.js +++ b/client/app/pages/queries/view.js @@ -1,6 +1,7 @@ import { pick, some, find, minBy, map, intersection, isArray, isObject } from 'lodash'; import { SCHEMA_NOT_SUPPORTED, SCHEMA_LOAD_ERROR } from '@/services/data-source'; import getTags from '@/services/getTags'; +import policy from '@/services/policy'; import Notifications from '@/services/notifications'; import template from './query.html'; @@ -21,7 +22,6 @@ function QueryViewCtrl( toastr, $uibModal, currentUser, - Policy, Query, DataSource, Visualization, @@ -451,7 +451,7 @@ function QueryViewCtrl( $scope.openVisualizationEditor(); } const intervals = clientConfig.queryRefreshIntervals; - const allowedIntervals = Policy.getQueryRefreshIntervals(); + const allowedIntervals = policy.getQueryRefreshIntervals(); $scope.refreshOptions = isArray(allowedIntervals) ? intersection(intervals, allowedIntervals) : intervals; $scope.updateQueryMetadata = changes => diff --git a/client/app/pages/users/list.js b/client/app/pages/users/list.js index ffdeba3541..3ee3d24c9b 100644 --- a/client/app/pages/users/list.js +++ b/client/app/pages/users/list.js @@ -1,12 +1,13 @@ import { extend } from 'lodash'; +import policy from '@/services/policy'; import ListCtrl from '@/lib/list-ctrl'; import settingsMenu from '@/services/settingsMenu'; import template from './list.html'; class UsersListCtrl extends ListCtrl { - constructor($scope, $location, currentUser, clientConfig, Policy, User) { + constructor($scope, $location, currentUser, clientConfig, User) { super($scope, $location, currentUser, clientConfig); - this.policy = Policy; + this.policy = policy; this.enableUser = user => User.enableUser(user).then(this.update); this.disableUser = user => User.disableUser(user).then(this.update); this.deleteUser = user => User.deleteUser(user).then(this.update); diff --git a/client/app/services/ng.js b/client/app/services/ng.js index ce59976f82..de32a79f45 100644 --- a/client/app/services/ng.js +++ b/client/app/services/ng.js @@ -1,5 +1,6 @@ export let $http = null; // eslint-disable-line import/no-mutable-exports export let $sanitize = null; // eslint-disable-line import/no-mutable-exports +export let $q = null; // eslint-disable-line import/no-mutable-exports export let $uibModal = null; // eslint-disable-line import/no-mutable-exports export let toastr = null; // eslint-disable-line import/no-mutable-exports @@ -7,6 +8,7 @@ export default function init(ngModule) { ngModule.run(($injector) => { $http = $injector.get('$http'); $sanitize = $injector.get('$sanitize'); + $q = $injector.get('$q'); $uibModal = $injector.get('$uibModal'); toastr = $injector.get('toastr'); }); diff --git a/client/app/services/organization-status.js b/client/app/services/organization-status.js deleted file mode 100644 index 81d827e92e..0000000000 --- a/client/app/services/organization-status.js +++ /dev/null @@ -1,22 +0,0 @@ -export let OrganizationStatus = null; // eslint-disable-line import/no-mutable-exports - -function OrganizationStatusService($http) { - this.objectCounters = {}; - - this.refresh = () => - $http.get('api/organization/status').then(({ data }) => { - this.objectCounters = data.object_counters; - return this; - }); -} - -export default function init(ngModule) { - ngModule.service('OrganizationStatus', OrganizationStatusService); - - ngModule.run(($injector) => { - OrganizationStatus = $injector.get('OrganizationStatus'); - }); -} - -init.init = true; - diff --git a/client/app/services/organizationStatus.js b/client/app/services/organizationStatus.js new file mode 100644 index 0000000000..d849ad7c67 --- /dev/null +++ b/client/app/services/organizationStatus.js @@ -0,0 +1,17 @@ +import { $http } from '@/services/ng'; + +class OrganizationStatus { + constructor() { + this.objectCounters = {}; + } + + refresh() { + return $http.get('api/organization/status').then(({ data }) => { + this.objectCounters = data.object_counters; + return this; + }); + } +} + +export default new OrganizationStatus(); + diff --git a/client/app/services/policy.js b/client/app/services/policy.js deleted file mode 100644 index 366c06fd3e..0000000000 --- a/client/app/services/policy.js +++ /dev/null @@ -1,84 +0,0 @@ -import { isFunction, isArray } from 'lodash'; - -export class Policy { - constructor($injector) { - this.$injector = $injector; - } - - get user() { - return this.$injector.get('currentUser'); - } - - get organizationStatus() { - return this.$injector.get('OrganizationStatus'); - } - - refresh() { - const $q = this.$injector.get('$q'); - return $q.resolve(this); - } - - canCreateDataSource() { - return this.user.hasPermission('admin'); - } - - isCreateDataSourceEnabled() { - return this.user.hasPermission('admin'); - } - - canCreateDashboard() { - return this.user.hasPermission('create_dashboard'); - } - - isCreateDashboardEnabled() { - return this.user.hasPermission('create_dashboard'); - } - - // eslint-disable-next-line class-methods-use-this - canCreateAlert() { - return true; - } - - canCreateUser() { - return this.user.hasPermission('admin'); - } - - isCreateUserEnabled() { - return this.user.hasPermission('admin'); - } - - getDashboardRefreshIntervals() { - const clientConfig = this.$injector.get('clientConfig'); - const result = clientConfig.dashboardRefreshIntervals; - return isArray(result) ? result : null; - } - - getQueryRefreshIntervals() { - const clientConfig = this.$injector.get('clientConfig'); - const result = clientConfig.queryRefreshIntervals; - return isArray(result) ? result : null; - } -} - -export default function init(ngModule) { - let appInjector = null; - - ngModule.run(($injector) => { - 'ngInject'; - - appInjector = $injector; - }); - - ngModule.provider('Policy', function policyProvider() { - let PolicyClass = Policy; - - this.setPolicyClass = (policyClass) => { - PolicyClass = policyClass; - }; - - this.$get = () => (isFunction(PolicyClass) ? new PolicyClass(appInjector) : null); - }); -} - -init.init = true; - diff --git a/client/app/services/policy/DefaultPolicy.js b/client/app/services/policy/DefaultPolicy.js new file mode 100644 index 0000000000..811829d5d2 --- /dev/null +++ b/client/app/services/policy/DefaultPolicy.js @@ -0,0 +1,62 @@ +import { isArray } from 'lodash'; +import { $q } from '@/services/ng'; +import { currentUser, clientConfig } from '@/services/auth'; +import organizationStatus from '@/services/organizationStatus'; +import Policy from './Policy'; + +export default class DefaultPolicy extends Policy { + // eslint-disable-next-line class-methods-use-this + get user() { + return currentUser; + } + + // eslint-disable-next-line class-methods-use-this + get organizationStatus() { + return organizationStatus; + } + + refresh() { + return $q.resolve(this); + } + + canCreateDataSource() { + return this.user.isAdmin; + } + + isCreateDataSourceEnabled() { + return this.user.isAdmin; + } + + canCreateDashboard() { + return this.user.hasPermission('create_dashboard'); + } + + isCreateDashboardEnabled() { + return this.user.hasPermission('create_dashboard'); + } + + // eslint-disable-next-line class-methods-use-this + canCreateAlert() { + return true; + } + + canCreateUser() { + return this.user.isAdmin; + } + + isCreateUserEnabled() { + return this.user.isAdmin; + } + + // eslint-disable-next-line class-methods-use-this + getDashboardRefreshIntervals() { + const result = clientConfig.dashboardRefreshIntervals; + return isArray(result) ? result : null; + } + + // eslint-disable-next-line class-methods-use-this + getQueryRefreshIntervals() { + const result = clientConfig.queryRefreshIntervals; + return isArray(result) ? result : null; + } +} diff --git a/client/app/services/policy/Policy.js b/client/app/services/policy/Policy.js new file mode 100644 index 0000000000..8787c17463 --- /dev/null +++ b/client/app/services/policy/Policy.js @@ -0,0 +1,56 @@ +export default class Policy { + // eslint-disable-next-line class-methods-use-this + _notImplemented() { + throw new Error('Not implemented'); + } + + // eslint-disable-next-line class-methods-use-this + get user() { + return null; + } + + // eslint-disable-next-line class-methods-use-this + get organizationStatus() { + return null; + } + + refresh() { + this._notImplemented(); + } + + canCreateDataSource() { + this._notImplemented(); + } + + isCreateDataSourceEnabled() { + this._notImplemented(); + } + + canCreateDashboard() { + this._notImplemented(); + } + + isCreateDashboardEnabled() { + this._notImplemented(); + } + + canCreateAlert() { + this._notImplemented(); + } + + canCreateUser() { + this._notImplemented(); + } + + isCreateUserEnabled() { + this._notImplemented(); + } + + getDashboardRefreshIntervals() { + this._notImplemented(); + } + + getQueryRefreshIntervals() { + this._notImplemented(); + } +} diff --git a/client/app/services/policy/index.js b/client/app/services/policy/index.js new file mode 100644 index 0000000000..6de096e4f1 --- /dev/null +++ b/client/app/services/policy/index.js @@ -0,0 +1,68 @@ +import Policy from './Policy'; +import DefaultPolicy from './DefaultPolicy'; + +class PolicyProxy extends Policy { + constructor(policy) { + super(); + this.policy = policy; + } + + get policy() { + return this._policy; + } + set policy(policy) { + this._policy = policy instanceof Policy ? policy : null; + } + + // Policy proxy methods + + get user() { + return this.policy ? this.policy.user : super.user; + } + + get organizationStatus() { + return this.policy ? this.policy.organizationStatus : super.organizationStatus; + } + + refresh() { + return this.policy ? this.policy.refresh() : super.refresh(); + } + + canCreateDataSource() { + return this.policy ? this.policy.canCreateDataSource : super.canCreateDataSource; + } + + isCreateDataSourceEnabled() { + return this.policy ? this.policy.isCreateDataSourceEnabled() : super.isCreateDataSourceEnabled(); + } + + canCreateDashboard() { + return this.policy ? this.policy.canCreateDashboard() : super.canCreateDashboard(); + } + + isCreateDashboardEnabled() { + return this.policy ? this.policy.isCreateDashboardEnabled() : super.isCreateDashboardEnabled(); + } + + canCreateAlert() { + return this.policy ? this.policy.canCreateAlert() : super.canCreateAlert(); + } + + canCreateUser() { + return this.policy ? this.policy.canCreateUser() : super.canCreateUser(); + } + + isCreateUserEnabled() { + return this.policy ? this.policy.isCreateUserEnabled() : super.isCreateUserEnabled(); + } + + getDashboardRefreshIntervals() { + return this.policy ? this.policy.getDashboardRefreshIntervals() : super.getDashboardRefreshIntervals(); + } + + getQueryRefreshIntervals() { + return this.policy ? this.policy.getQueryRefreshIntervals() : super.getQueryRefreshIntervals(); + } +} + +export default new PolicyProxy(new DefaultPolicy()); From 64defda49e8c4d31de69cb7c5cd895c60136166a Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Fri, 25 Jan 2019 19:47:54 +0200 Subject: [PATCH 2/5] Better class names --- client/app/services/policy/{Policy.js => AbstractPolicy.js} | 2 +- client/app/services/policy/DefaultPolicy.js | 4 ++-- client/app/services/policy/index.js | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename client/app/services/policy/{Policy.js => AbstractPolicy.js} (95%) diff --git a/client/app/services/policy/Policy.js b/client/app/services/policy/AbstractPolicy.js similarity index 95% rename from client/app/services/policy/Policy.js rename to client/app/services/policy/AbstractPolicy.js index 8787c17463..f6cf81ae64 100644 --- a/client/app/services/policy/Policy.js +++ b/client/app/services/policy/AbstractPolicy.js @@ -1,4 +1,4 @@ -export default class Policy { +export default class AbstractPolicy { // eslint-disable-next-line class-methods-use-this _notImplemented() { throw new Error('Not implemented'); diff --git a/client/app/services/policy/DefaultPolicy.js b/client/app/services/policy/DefaultPolicy.js index 811829d5d2..a514e771d1 100644 --- a/client/app/services/policy/DefaultPolicy.js +++ b/client/app/services/policy/DefaultPolicy.js @@ -2,9 +2,9 @@ import { isArray } from 'lodash'; import { $q } from '@/services/ng'; import { currentUser, clientConfig } from '@/services/auth'; import organizationStatus from '@/services/organizationStatus'; -import Policy from './Policy'; +import AbstractPolicy from './AbstractPolicy'; -export default class DefaultPolicy extends Policy { +export default class DefaultPolicy extends AbstractPolicy { // eslint-disable-next-line class-methods-use-this get user() { return currentUser; diff --git a/client/app/services/policy/index.js b/client/app/services/policy/index.js index 6de096e4f1..a5579228c0 100644 --- a/client/app/services/policy/index.js +++ b/client/app/services/policy/index.js @@ -1,7 +1,7 @@ -import Policy from './Policy'; +import AbstractPolicy from './AbstractPolicy'; import DefaultPolicy from './DefaultPolicy'; -class PolicyProxy extends Policy { +class PolicyProxy extends AbstractPolicy { constructor(policy) { super(); this.policy = policy; @@ -11,7 +11,7 @@ class PolicyProxy extends Policy { return this._policy; } set policy(policy) { - this._policy = policy instanceof Policy ? policy : null; + this._policy = policy instanceof AbstractPolicy ? policy : null; } // Policy proxy methods From b654686e178df77d516d7c42854195a70d9c62a9 Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Mon, 28 Jan 2019 10:50:25 +0200 Subject: [PATCH 3/5] CR1 --- .../dashboards/edit-dashboard-dialog.js | 2 +- client/app/pages/dashboards/dashboard.js | 2 +- client/app/pages/data-sources/list.js | 2 +- client/app/pages/queries/view.js | 2 +- client/app/pages/users/list.js | 2 +- client/app/services/policy/AbstractPolicy.js | 56 --------------- client/app/services/policy/DefaultPolicy.js | 3 +- client/app/services/policy/index.js | 68 ++----------------- 8 files changed, 10 insertions(+), 127 deletions(-) delete mode 100644 client/app/services/policy/AbstractPolicy.js diff --git a/client/app/components/dashboards/edit-dashboard-dialog.js b/client/app/components/dashboards/edit-dashboard-dialog.js index 82988b24a6..a1c765dec7 100644 --- a/client/app/components/dashboards/edit-dashboard-dialog.js +++ b/client/app/components/dashboards/edit-dashboard-dialog.js @@ -1,5 +1,5 @@ import { isEmpty } from 'lodash'; -import policy from '@/services/policy'; +import { policy } from '@/services/policy'; import template from './edit-dashboard-dialog.html'; const EditDashboardDialog = { diff --git a/client/app/pages/dashboards/dashboard.js b/client/app/pages/dashboards/dashboard.js index 951f80c0ad..4cdf8c07a8 100644 --- a/client/app/pages/dashboards/dashboard.js +++ b/client/app/pages/dashboards/dashboard.js @@ -1,7 +1,7 @@ import * as _ from 'lodash'; import PromiseRejectionError from '@/lib/promise-rejection-error'; import getTags from '@/services/getTags'; -import policy from '@/services/policy'; +import { policy } from '@/services/policy'; import { durationHumanize } from '@/filters'; import template from './dashboard.html'; import shareDashboardTemplate from './share-dashboard.html'; diff --git a/client/app/pages/data-sources/list.js b/client/app/pages/data-sources/list.js index 20cf409c43..c81bc46c0d 100644 --- a/client/app/pages/data-sources/list.js +++ b/client/app/pages/data-sources/list.js @@ -1,5 +1,5 @@ import settingsMenu from '@/services/settingsMenu'; -import policy from '@/services/policy'; +import { policy } from '@/services/policy'; import template from './list.html'; function DataSourcesCtrl(DataSource) { diff --git a/client/app/pages/queries/view.js b/client/app/pages/queries/view.js index ba6316d819..758ef9facb 100644 --- a/client/app/pages/queries/view.js +++ b/client/app/pages/queries/view.js @@ -1,7 +1,7 @@ import { pick, some, find, minBy, map, intersection, isArray, isObject } from 'lodash'; import { SCHEMA_NOT_SUPPORTED, SCHEMA_LOAD_ERROR } from '@/services/data-source'; import getTags from '@/services/getTags'; -import policy from '@/services/policy'; +import { policy } from '@/services/policy'; import Notifications from '@/services/notifications'; import template from './query.html'; diff --git a/client/app/pages/users/list.js b/client/app/pages/users/list.js index 3ee3d24c9b..ee1e165ac8 100644 --- a/client/app/pages/users/list.js +++ b/client/app/pages/users/list.js @@ -1,5 +1,5 @@ import { extend } from 'lodash'; -import policy from '@/services/policy'; +import { policy } from '@/services/policy'; import ListCtrl from '@/lib/list-ctrl'; import settingsMenu from '@/services/settingsMenu'; import template from './list.html'; diff --git a/client/app/services/policy/AbstractPolicy.js b/client/app/services/policy/AbstractPolicy.js deleted file mode 100644 index f6cf81ae64..0000000000 --- a/client/app/services/policy/AbstractPolicy.js +++ /dev/null @@ -1,56 +0,0 @@ -export default class AbstractPolicy { - // eslint-disable-next-line class-methods-use-this - _notImplemented() { - throw new Error('Not implemented'); - } - - // eslint-disable-next-line class-methods-use-this - get user() { - return null; - } - - // eslint-disable-next-line class-methods-use-this - get organizationStatus() { - return null; - } - - refresh() { - this._notImplemented(); - } - - canCreateDataSource() { - this._notImplemented(); - } - - isCreateDataSourceEnabled() { - this._notImplemented(); - } - - canCreateDashboard() { - this._notImplemented(); - } - - isCreateDashboardEnabled() { - this._notImplemented(); - } - - canCreateAlert() { - this._notImplemented(); - } - - canCreateUser() { - this._notImplemented(); - } - - isCreateUserEnabled() { - this._notImplemented(); - } - - getDashboardRefreshIntervals() { - this._notImplemented(); - } - - getQueryRefreshIntervals() { - this._notImplemented(); - } -} diff --git a/client/app/services/policy/DefaultPolicy.js b/client/app/services/policy/DefaultPolicy.js index a514e771d1..b70cde8d64 100644 --- a/client/app/services/policy/DefaultPolicy.js +++ b/client/app/services/policy/DefaultPolicy.js @@ -2,9 +2,8 @@ import { isArray } from 'lodash'; import { $q } from '@/services/ng'; import { currentUser, clientConfig } from '@/services/auth'; import organizationStatus from '@/services/organizationStatus'; -import AbstractPolicy from './AbstractPolicy'; -export default class DefaultPolicy extends AbstractPolicy { +export default class DefaultPolicy { // eslint-disable-next-line class-methods-use-this get user() { return currentUser; diff --git a/client/app/services/policy/index.js b/client/app/services/policy/index.js index a5579228c0..219e348e26 100644 --- a/client/app/services/policy/index.js +++ b/client/app/services/policy/index.js @@ -1,68 +1,8 @@ -import AbstractPolicy from './AbstractPolicy'; import DefaultPolicy from './DefaultPolicy'; -class PolicyProxy extends AbstractPolicy { - constructor(policy) { - super(); - this.policy = policy; - } +// eslint-disable-next-line import/no-mutable-exports +export let policy = new DefaultPolicy(); - get policy() { - return this._policy; - } - set policy(policy) { - this._policy = policy instanceof AbstractPolicy ? policy : null; - } - - // Policy proxy methods - - get user() { - return this.policy ? this.policy.user : super.user; - } - - get organizationStatus() { - return this.policy ? this.policy.organizationStatus : super.organizationStatus; - } - - refresh() { - return this.policy ? this.policy.refresh() : super.refresh(); - } - - canCreateDataSource() { - return this.policy ? this.policy.canCreateDataSource : super.canCreateDataSource; - } - - isCreateDataSourceEnabled() { - return this.policy ? this.policy.isCreateDataSourceEnabled() : super.isCreateDataSourceEnabled(); - } - - canCreateDashboard() { - return this.policy ? this.policy.canCreateDashboard() : super.canCreateDashboard(); - } - - isCreateDashboardEnabled() { - return this.policy ? this.policy.isCreateDashboardEnabled() : super.isCreateDashboardEnabled(); - } - - canCreateAlert() { - return this.policy ? this.policy.canCreateAlert() : super.canCreateAlert(); - } - - canCreateUser() { - return this.policy ? this.policy.canCreateUser() : super.canCreateUser(); - } - - isCreateUserEnabled() { - return this.policy ? this.policy.isCreateUserEnabled() : super.isCreateUserEnabled(); - } - - getDashboardRefreshIntervals() { - return this.policy ? this.policy.getDashboardRefreshIntervals() : super.getDashboardRefreshIntervals(); - } - - getQueryRefreshIntervals() { - return this.policy ? this.policy.getQueryRefreshIntervals() : super.getQueryRefreshIntervals(); - } +export function setPolicy(newPolicy) { + policy = newPolicy instanceof DefaultPolicy ? newPolicy : new DefaultPolicy(); } - -export default new PolicyProxy(new DefaultPolicy()); From 9066f8227579cf4fcdbbb1fca48189fbb0fb964d Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Mon, 28 Jan 2019 13:22:08 +0200 Subject: [PATCH 4/5] CR2 --- client/app/services/policy/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/app/services/policy/index.js b/client/app/services/policy/index.js index 219e348e26..9a4ebd0b1d 100644 --- a/client/app/services/policy/index.js +++ b/client/app/services/policy/index.js @@ -4,5 +4,5 @@ import DefaultPolicy from './DefaultPolicy'; export let policy = new DefaultPolicy(); export function setPolicy(newPolicy) { - policy = newPolicy instanceof DefaultPolicy ? newPolicy : new DefaultPolicy(); + policy = newPolicy; } From f09a8a646487a6cd5acffdc9b907599e29648c14 Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Mon, 28 Jan 2019 13:48:25 +0200 Subject: [PATCH 5/5] CR3 --- client/app/services/policy/DefaultPolicy.js | 28 ++++++--------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/client/app/services/policy/DefaultPolicy.js b/client/app/services/policy/DefaultPolicy.js index b70cde8d64..b1101addbe 100644 --- a/client/app/services/policy/DefaultPolicy.js +++ b/client/app/services/policy/DefaultPolicy.js @@ -1,59 +1,47 @@ import { isArray } from 'lodash'; import { $q } from '@/services/ng'; import { currentUser, clientConfig } from '@/services/auth'; -import organizationStatus from '@/services/organizationStatus'; -export default class DefaultPolicy { - // eslint-disable-next-line class-methods-use-this - get user() { - return currentUser; - } - - // eslint-disable-next-line class-methods-use-this - get organizationStatus() { - return organizationStatus; - } +/* eslint-disable class-methods-use-this */ +export default class DefaultPolicy { refresh() { return $q.resolve(this); } canCreateDataSource() { - return this.user.isAdmin; + return currentUser.isAdmin; } isCreateDataSourceEnabled() { - return this.user.isAdmin; + return currentUser.isAdmin; } canCreateDashboard() { - return this.user.hasPermission('create_dashboard'); + return currentUser.hasPermission('create_dashboard'); } isCreateDashboardEnabled() { - return this.user.hasPermission('create_dashboard'); + return currentUser.hasPermission('create_dashboard'); } - // eslint-disable-next-line class-methods-use-this canCreateAlert() { return true; } canCreateUser() { - return this.user.isAdmin; + return currentUser.isAdmin; } isCreateUserEnabled() { - return this.user.isAdmin; + return currentUser.isAdmin; } - // eslint-disable-next-line class-methods-use-this getDashboardRefreshIntervals() { const result = clientConfig.dashboardRefreshIntervals; return isArray(result) ? result : null; } - // eslint-disable-next-line class-methods-use-this getQueryRefreshIntervals() { const result = clientConfig.queryRefreshIntervals; return isArray(result) ? result : null;