access.tsx 1.3 KB

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