access.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {hasEveryAccess} from 'sentry/components/acl/access';
  2. import type {Scope} from 'sentry/types/core';
  3. import type {Team} from 'sentry/types/organization';
  4. import type {Project} from 'sentry/types/project';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import {useUser} from 'sentry/utils/useUser';
  7. type Props = {
  8. /**
  9. * List of required access levels
  10. */
  11. access?: Scope[];
  12. /**
  13. * Optional: To be used when you need to check for access to the Project
  14. *
  15. * E.g. On the project settings page, the user will need project:write.
  16. * An "org-member" does not have project:write but if they are "team-admin" for
  17. * of a parent team, they will have appropriate scopes.
  18. */
  19. project?: Project | null | undefined;
  20. /**
  21. * Optional: To be used when you need to check for access to the Team
  22. *
  23. * E.g. On the team settings page, the user will need team:write.
  24. * An "org-member" does not have team:write but if they are "team-admin" for
  25. * the team, they will have appropriate scopes.
  26. */
  27. team?: Team | null | undefined;
  28. };
  29. export function useAccess({access = [], team, project}: Props) {
  30. const user = useUser();
  31. const organization = useOrganization();
  32. team = team ?? undefined;
  33. project = project ?? undefined;
  34. const hasAccess = hasEveryAccess(access, {organization, team, project});
  35. const hasSuperuser = Boolean(user?.isSuperuser);
  36. return {
  37. hasAccess,
  38. hasSuperuser,
  39. };
  40. }