isActiveSuperuser.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import Cookies from 'js-cookie';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import OrganizationStore from 'sentry/stores/organizationStore';
  4. const SUPERUSER_COOKIE_NAME = window.superUserCookieName ?? 'su';
  5. const SUPERUSER_COOKIE_DOMAIN = window.superUserCookieDomain;
  6. /**
  7. * Checking for just isSuperuser on a config object may not be enough as backend
  8. * often checks for *active* superuser. We check both isSuperuser flag
  9. * AND superuser session cookie.
  10. *
  11. * Note that this function does not work all the time. It is possible to have
  12. * an expired superuser cookie.
  13. *
  14. * Documented here: https://getsentry.atlassian.net/browse/ER-1602
  15. */
  16. export function isActiveSuperuser() {
  17. const {organization} = OrganizationStore.getState();
  18. if (organization) {
  19. return organization.access.includes('org:superuser');
  20. }
  21. const {isSuperuser} = ConfigStore.get('user') || {};
  22. if (isSuperuser) {
  23. const superUserCookieName =
  24. ConfigStore.get('superUserCookieName') || SUPERUSER_COOKIE_NAME;
  25. const superUserCookieDomain =
  26. ConfigStore.get('superUserCookieDomain') || SUPERUSER_COOKIE_DOMAIN;
  27. /**
  28. * Superuser cookie cannot be checked for existence as it is HttpOnly. As a workaround, we try
  29. * to change it to something else and if that fails we can assume that it's being present.
  30. */
  31. Cookies.set(superUserCookieName, 'set-in-isActiveSuperuser', {
  32. domain: superUserCookieDomain,
  33. });
  34. if (Cookies.get(superUserCookieName) === undefined) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }