actions.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { lastValueFrom } from 'rxjs';
  2. import { locationUtil } from '@grafana/data';
  3. import { getBackendSrv, locationService } from '@grafana/runtime';
  4. import { notifyApp, updateNavIndex } from 'app/core/actions';
  5. import { createSuccessNotification, createWarningNotification } from 'app/core/copy/appNotification';
  6. import { contextSrv } from 'app/core/core';
  7. import { backendSrv } from 'app/core/services/backend_srv';
  8. import { FolderState, ThunkResult } from 'app/types';
  9. import { DashboardAcl, DashboardAclUpdateDTO, NewDashboardAclItem, PermissionLevel } from 'app/types/acl';
  10. import { buildNavModel } from './navModel';
  11. import { loadFolder, loadFolderPermissions, setCanViewFolderPermissions } from './reducers';
  12. export function getFolderByUid(uid: string): ThunkResult<void> {
  13. return async (dispatch) => {
  14. const folder = await backendSrv.getFolderByUid(uid);
  15. dispatch(loadFolder(folder));
  16. dispatch(updateNavIndex(buildNavModel(folder)));
  17. };
  18. }
  19. export function saveFolder(folder: FolderState): ThunkResult<void> {
  20. return async (dispatch) => {
  21. const res = await backendSrv.put(`/api/folders/${folder.uid}`, {
  22. title: folder.title,
  23. version: folder.version,
  24. });
  25. dispatch(notifyApp(createSuccessNotification('Folder saved')));
  26. locationService.push(`${res.url}/settings`);
  27. };
  28. }
  29. export function deleteFolder(uid: string): ThunkResult<void> {
  30. return async () => {
  31. await backendSrv.delete(`/api/folders/${uid}?forceDeleteRules=false`);
  32. locationService.push('/dashboards');
  33. };
  34. }
  35. export function getFolderPermissions(uid: string): ThunkResult<void> {
  36. return async (dispatch) => {
  37. const permissions = await backendSrv.get(`/api/folders/${uid}/permissions`);
  38. dispatch(loadFolderPermissions(permissions));
  39. };
  40. }
  41. export function checkFolderPermissions(uid: string): ThunkResult<void> {
  42. return async (dispatch) => {
  43. try {
  44. await lastValueFrom(
  45. backendSrv.fetch({
  46. method: 'GET',
  47. showErrorAlert: false,
  48. showSuccessAlert: false,
  49. url: `/api/folders/${uid}/permissions`,
  50. })
  51. );
  52. dispatch(setCanViewFolderPermissions(true));
  53. } catch (err) {
  54. if (err.status !== 403) {
  55. dispatch(notifyApp(createWarningNotification('Error checking folder permissions', err.data?.message)));
  56. }
  57. dispatch(setCanViewFolderPermissions(false));
  58. }
  59. };
  60. }
  61. function toUpdateItem(item: DashboardAcl): DashboardAclUpdateDTO {
  62. return {
  63. userId: item.userId,
  64. teamId: item.teamId,
  65. role: item.role,
  66. permission: item.permission,
  67. };
  68. }
  69. export function updateFolderPermission(itemToUpdate: DashboardAcl, level: PermissionLevel): ThunkResult<void> {
  70. return async (dispatch, getStore) => {
  71. const folder = getStore().folder;
  72. const itemsToUpdate = [];
  73. for (const item of folder.permissions) {
  74. if (item.inherited) {
  75. continue;
  76. }
  77. const updated = toUpdateItem(item);
  78. // if this is the item we want to update, update it's permission
  79. if (itemToUpdate === item) {
  80. updated.permission = level;
  81. }
  82. itemsToUpdate.push(updated);
  83. }
  84. await backendSrv.post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
  85. await dispatch(getFolderPermissions(folder.uid));
  86. };
  87. }
  88. export function removeFolderPermission(itemToDelete: DashboardAcl): ThunkResult<void> {
  89. return async (dispatch, getStore) => {
  90. const folder = getStore().folder;
  91. const itemsToUpdate = [];
  92. for (const item of folder.permissions) {
  93. if (item.inherited || item === itemToDelete) {
  94. continue;
  95. }
  96. itemsToUpdate.push(toUpdateItem(item));
  97. }
  98. await backendSrv.post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
  99. await dispatch(getFolderPermissions(folder.uid));
  100. };
  101. }
  102. export function addFolderPermission(newItem: NewDashboardAclItem): ThunkResult<void> {
  103. return async (dispatch, getStore) => {
  104. const folder = getStore().folder;
  105. const itemsToUpdate = [];
  106. for (const item of folder.permissions) {
  107. if (item.inherited) {
  108. continue;
  109. }
  110. itemsToUpdate.push(toUpdateItem(item));
  111. }
  112. itemsToUpdate.push({
  113. userId: newItem.userId,
  114. teamId: newItem.teamId,
  115. role: newItem.role,
  116. permission: newItem.permission,
  117. });
  118. await backendSrv.post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
  119. await dispatch(getFolderPermissions(folder.uid));
  120. };
  121. }
  122. export function createNewFolder(folderName: string): ThunkResult<void> {
  123. return async (dispatch) => {
  124. const newFolder = await getBackendSrv().post('/api/folders', { title: folderName });
  125. await contextSrv.fetchUserPermissions();
  126. dispatch(notifyApp(createSuccessNotification('Folder Created', 'OK')));
  127. locationService.push(locationUtil.stripBaseFromUrl(newFolder.url));
  128. };
  129. }