123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import { TimeZone } from '@grafana/data';
- import { getBackendSrv } from '@grafana/runtime';
- import { notifyApp } from 'app/core/actions';
- import { createSuccessNotification } from 'app/core/copy/appNotification';
- import { updateTimeZoneForSession, updateWeekStartForSession } from 'app/features/profile/state/reducers';
- import { DashboardAcl, DashboardAclUpdateDTO, NewDashboardAclItem, PermissionLevel, ThunkResult } from 'app/types';
- import { loadPluginDashboards } from '../../plugins/admin/state/actions';
- import { cancelVariables } from '../../variables/state/actions';
- import { getTimeSrv } from '../services/TimeSrv';
- import { cleanUpDashboard, loadDashboardPermissions } from './reducers';
- export function getDashboardPermissions(id: number): ThunkResult<void> {
- return async (dispatch) => {
- const permissions = await getBackendSrv().get(`/api/dashboards/id/${id}/permissions`);
- dispatch(loadDashboardPermissions(permissions));
- };
- }
- function toUpdateItem(item: DashboardAcl): DashboardAclUpdateDTO {
- return {
- userId: item.userId,
- teamId: item.teamId,
- role: item.role,
- permission: item.permission,
- };
- }
- export function updateDashboardPermission(
- dashboardId: number,
- itemToUpdate: DashboardAcl,
- level: PermissionLevel
- ): ThunkResult<void> {
- return async (dispatch, getStore) => {
- const { dashboard } = getStore();
- const itemsToUpdate = [];
- for (const item of dashboard.permissions) {
- if (item.inherited) {
- continue;
- }
- const updated = toUpdateItem(item);
- // if this is the item we want to update, update it's permission
- if (itemToUpdate === item) {
- updated.permission = level;
- }
- itemsToUpdate.push(updated);
- }
- await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
- await dispatch(getDashboardPermissions(dashboardId));
- };
- }
- export function removeDashboardPermission(dashboardId: number, itemToDelete: DashboardAcl): ThunkResult<void> {
- return async (dispatch, getStore) => {
- const dashboard = getStore().dashboard;
- const itemsToUpdate = [];
- for (const item of dashboard.permissions) {
- if (item.inherited || item === itemToDelete) {
- continue;
- }
- itemsToUpdate.push(toUpdateItem(item));
- }
- await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
- await dispatch(getDashboardPermissions(dashboardId));
- };
- }
- export function addDashboardPermission(dashboardId: number, newItem: NewDashboardAclItem): ThunkResult<void> {
- return async (dispatch, getStore) => {
- const { dashboard } = getStore();
- const itemsToUpdate = [];
- for (const item of dashboard.permissions) {
- if (item.inherited) {
- continue;
- }
- itemsToUpdate.push(toUpdateItem(item));
- }
- itemsToUpdate.push({
- userId: newItem.userId,
- teamId: newItem.teamId,
- role: newItem.role,
- permission: newItem.permission,
- });
- await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
- await dispatch(getDashboardPermissions(dashboardId));
- };
- }
- export function importDashboard(data: any, dashboardTitle: string): ThunkResult<void> {
- return async (dispatch) => {
- await getBackendSrv().post('/api/dashboards/import', data);
- dispatch(notifyApp(createSuccessNotification('Dashboard Imported', dashboardTitle)));
- dispatch(loadPluginDashboards());
- };
- }
- export function removeDashboard(uid: string): ThunkResult<void> {
- return async (dispatch) => {
- await getBackendSrv().delete(`/api/dashboards/uid/${uid}`);
- dispatch(loadPluginDashboards());
- };
- }
- export const cleanUpDashboardAndVariables = (): ThunkResult<void> => (dispatch, getStore) => {
- const store = getStore();
- const dashboard = store.dashboard.getModel();
- if (dashboard) {
- dashboard.destroy();
- dispatch(cancelVariables(dashboard.uid));
- }
- getTimeSrv().stopAutoRefresh();
- dispatch(cleanUpDashboard());
- };
- export const updateTimeZoneDashboard =
- (timeZone: TimeZone): ThunkResult<void> =>
- (dispatch) => {
- dispatch(updateTimeZoneForSession(timeZone));
- getTimeSrv().refreshTimeModel();
- };
- export const updateWeekStartDashboard =
- (weekStart: string): ThunkResult<void> =>
- (dispatch) => {
- dispatch(updateWeekStartForSession(weekStart));
- getTimeSrv().refreshTimeModel();
- };
|