access.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import type {Project, Scope, Team} from 'sentry/types';
  2. import useOrganization from 'sentry/utils/useOrganization';
  3. import {useUser} from 'sentry/utils/useUser';
  4. import {hasEveryAccess} from '../../../components/acl/access';
  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 = !!(user && user.isSuperuser);
  34. return {
  35. hasAccess,
  36. hasSuperuser,
  37. };
  38. }