access-control.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { contextSrv } from 'app/core/services/context_srv';
  2. import { AccessControlAction } from 'app/types';
  3. import { GRAFANA_RULES_SOURCE_NAME, isGrafanaRulesSource } from './datasource';
  4. type RulesSourceType = 'grafana' | 'external';
  5. function getRulesSourceType(alertManagerSourceName: string): RulesSourceType {
  6. return isGrafanaRulesSource(alertManagerSourceName) ? 'grafana' : 'external';
  7. }
  8. export const instancesPermissions = {
  9. read: {
  10. grafana: AccessControlAction.AlertingInstanceRead,
  11. external: AccessControlAction.AlertingInstancesExternalRead,
  12. },
  13. create: {
  14. grafana: AccessControlAction.AlertingInstanceCreate,
  15. external: AccessControlAction.AlertingInstancesExternalWrite,
  16. },
  17. update: {
  18. grafana: AccessControlAction.AlertingInstanceUpdate,
  19. external: AccessControlAction.AlertingInstancesExternalWrite,
  20. },
  21. delete: {
  22. grafana: AccessControlAction.AlertingInstanceUpdate,
  23. external: AccessControlAction.AlertingInstancesExternalWrite,
  24. },
  25. };
  26. export const notificationsPermissions = {
  27. read: {
  28. grafana: AccessControlAction.AlertingNotificationsRead,
  29. external: AccessControlAction.AlertingNotificationsExternalRead,
  30. },
  31. create: {
  32. grafana: AccessControlAction.AlertingNotificationsWrite,
  33. external: AccessControlAction.AlertingNotificationsExternalWrite,
  34. },
  35. update: {
  36. grafana: AccessControlAction.AlertingNotificationsWrite,
  37. external: AccessControlAction.AlertingNotificationsExternalWrite,
  38. },
  39. delete: {
  40. grafana: AccessControlAction.AlertingNotificationsWrite,
  41. external: AccessControlAction.AlertingNotificationsExternalWrite,
  42. },
  43. };
  44. const rulesPermissions = {
  45. read: {
  46. grafana: AccessControlAction.AlertingRuleRead,
  47. external: AccessControlAction.AlertingRuleExternalRead,
  48. },
  49. create: {
  50. grafana: AccessControlAction.AlertingRuleCreate,
  51. external: AccessControlAction.AlertingRuleExternalWrite,
  52. },
  53. update: {
  54. grafana: AccessControlAction.AlertingRuleUpdate,
  55. external: AccessControlAction.AlertingRuleExternalWrite,
  56. },
  57. delete: {
  58. grafana: AccessControlAction.AlertingRuleDelete,
  59. external: AccessControlAction.AlertingRuleExternalWrite,
  60. },
  61. };
  62. export function getInstancesPermissions(rulesSourceName: string) {
  63. const sourceType = getRulesSourceType(rulesSourceName);
  64. return {
  65. read: instancesPermissions.read[sourceType],
  66. create: instancesPermissions.create[sourceType],
  67. update: instancesPermissions.update[sourceType],
  68. delete: instancesPermissions.delete[sourceType],
  69. };
  70. }
  71. export function getNotificationsPermissions(rulesSourceName: string) {
  72. const sourceType = getRulesSourceType(rulesSourceName);
  73. return {
  74. read: notificationsPermissions.read[sourceType],
  75. create: notificationsPermissions.create[sourceType],
  76. update: notificationsPermissions.update[sourceType],
  77. delete: notificationsPermissions.delete[sourceType],
  78. };
  79. }
  80. export function getRulesPermissions(rulesSourceName: string) {
  81. const sourceType = getRulesSourceType(rulesSourceName);
  82. return {
  83. read: rulesPermissions.read[sourceType],
  84. create: rulesPermissions.create[sourceType],
  85. update: rulesPermissions.update[sourceType],
  86. delete: rulesPermissions.delete[sourceType],
  87. };
  88. }
  89. export function evaluateAccess(actions: AccessControlAction[], fallBackUserRoles: string[]) {
  90. return () => {
  91. return contextSrv.evaluatePermission(() => fallBackUserRoles, actions);
  92. };
  93. }
  94. export function getRulesAccess() {
  95. return {
  96. canCreateGrafanaRules:
  97. contextSrv.hasAccess(AccessControlAction.FoldersRead, contextSrv.hasEditPermissionInFolders) &&
  98. contextSrv.hasAccess(rulesPermissions.create.grafana, contextSrv.hasEditPermissionInFolders),
  99. canCreateCloudRules:
  100. contextSrv.hasAccess(AccessControlAction.DataSourcesRead, contextSrv.isEditor) &&
  101. contextSrv.hasAccess(rulesPermissions.create.external, contextSrv.isEditor),
  102. canEditRules: (rulesSourceName: string) => {
  103. const permissionFallback =
  104. rulesSourceName === GRAFANA_RULES_SOURCE_NAME ? contextSrv.hasEditPermissionInFolders : contextSrv.isEditor;
  105. return contextSrv.hasAccess(getRulesPermissions(rulesSourceName).update, permissionFallback);
  106. },
  107. };
  108. }