isActiveSuperuser.tsx 913 B

12345678910111213141516171819202122232425262728
  1. import Cookies from 'js-cookie';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. const SUPERUSER_COOKIE_NAME = 'su';
  4. /**
  5. * Checking for just isSuperuser on a config object may not be enough as backend often checks for *active* superuser.
  6. * We therefore check both isSuperuser flag AND superuser session cookie.
  7. */
  8. export function isActiveSuperuser() {
  9. const {isSuperuser} = ConfigStore.get('user') || {};
  10. if (isSuperuser) {
  11. /**
  12. * Superuser cookie cannot be checked for existence as it is HttpOnly.
  13. * As a workaround, we try to change it to something else and if that fails we can assume that it's being present.
  14. * There may be an edgecase where it's present and expired but for current usage it's not a big deal.
  15. */
  16. Cookies.set(SUPERUSER_COOKIE_NAME, 'test');
  17. if (Cookies.get(SUPERUSER_COOKIE_NAME) === undefined) {
  18. return true;
  19. }
  20. }
  21. return false;
  22. }