organizationMemberDetail.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import {Fragment, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import isEqual from 'lodash/isEqual';
  5. import {removeAuthenticator} from 'sentry/actionCreators/account';
  6. import {
  7. addErrorMessage,
  8. addLoadingMessage,
  9. addSuccessMessage,
  10. } from 'sentry/actionCreators/indicator';
  11. import {resendMemberInvite, updateMember} from 'sentry/actionCreators/members';
  12. import {Button} from 'sentry/components/button';
  13. import Confirm from 'sentry/components/confirm';
  14. import {DateTime} from 'sentry/components/dateTime';
  15. import NotFound from 'sentry/components/errors/notFound';
  16. import FieldGroup from 'sentry/components/forms/fieldGroup';
  17. import HookOrDefault from 'sentry/components/hookOrDefault';
  18. import ExternalLink from 'sentry/components/links/externalLink';
  19. import LoadingError from 'sentry/components/loadingError';
  20. import LoadingIndicator from 'sentry/components/loadingIndicator';
  21. import Panel from 'sentry/components/panels/panel';
  22. import PanelBody from 'sentry/components/panels/panelBody';
  23. import PanelHeader from 'sentry/components/panels/panelHeader';
  24. import PanelItem from 'sentry/components/panels/panelItem';
  25. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  26. import {Tooltip} from 'sentry/components/tooltip';
  27. import {IconRefresh} from 'sentry/icons';
  28. import {t, tct} from 'sentry/locale';
  29. import {space} from 'sentry/styles/space';
  30. import type {Member} from 'sentry/types/organization';
  31. import isMemberDisabledFromLimit from 'sentry/utils/isMemberDisabledFromLimit';
  32. import {
  33. type ApiQueryKey,
  34. setApiQueryData,
  35. useApiQuery,
  36. useMutation,
  37. useQueryClient,
  38. } from 'sentry/utils/queryClient';
  39. import type RequestError from 'sentry/utils/requestError/requestError';
  40. import Teams from 'sentry/utils/teams';
  41. import useApi from 'sentry/utils/useApi';
  42. import {useNavigate} from 'sentry/utils/useNavigate';
  43. import useOrganization from 'sentry/utils/useOrganization';
  44. import {useParams} from 'sentry/utils/useParams';
  45. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  46. import TeamSelectForMember from 'sentry/views/settings/components/teamSelect/teamSelectForMember';
  47. import OrganizationRoleSelect from './inviteMember/orgRoleSelect';
  48. const MULTIPLE_ORGS = t('Cannot be reset since user is in more than one organization');
  49. const NOT_ENROLLED = t('Not enrolled in two-factor authentication');
  50. const NO_PERMISSION = t('You do not have permission to perform this action');
  51. const TWO_FACTOR_REQUIRED = t(
  52. 'Cannot be reset since two-factor is required for this organization'
  53. );
  54. const DisabledMemberTooltip = HookOrDefault({
  55. hookName: 'component:disabled-member-tooltip',
  56. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  57. });
  58. function MemberStatus({
  59. member,
  60. memberDeactivated,
  61. }: {
  62. member: Member;
  63. memberDeactivated: boolean;
  64. }) {
  65. if (memberDeactivated) {
  66. return (
  67. <em>
  68. <DisabledMemberTooltip>{t('Deactivated')}</DisabledMemberTooltip>
  69. </em>
  70. );
  71. }
  72. if (member.expired) {
  73. return <em>{t('Invitation Expired')}</em>;
  74. }
  75. if (member.pending) {
  76. return <em>{t('Invitation Pending')}</em>;
  77. }
  78. return t('Active');
  79. }
  80. const getMemberQueryKey = (orgSlug: string, memberId: string): ApiQueryKey => [
  81. `/organizations/${orgSlug}/members/${memberId}/`,
  82. ];
  83. function OrganizationMemberDetailContent({member}: {member: Member}) {
  84. const api = useApi();
  85. const queryClient = useQueryClient();
  86. const organization = useOrganization();
  87. const navigate = useNavigate();
  88. const [orgRole, setOrgRole] = useState<Member['orgRole']>('');
  89. const [teamRoles, setTeamRoles] = useState<Member['teamRoles']>([]);
  90. const hasTeamRoles = organization.features.includes('team-roles');
  91. useEffect(() => {
  92. if (member) {
  93. setOrgRole(member.orgRole);
  94. setTeamRoles(member.teamRoles);
  95. }
  96. }, [member]);
  97. const {mutate: updatedMember, isPending: isSaving} = useMutation<Member, RequestError>({
  98. mutationFn: () => {
  99. return updateMember(api, {
  100. orgId: organization.slug,
  101. memberId: member.id,
  102. data: {orgRole, teamRoles} as any,
  103. });
  104. },
  105. onMutate: () => {
  106. addLoadingMessage(t('Saving\u2026'));
  107. },
  108. onSuccess: data => {
  109. addSuccessMessage(t('Saved'));
  110. setApiQueryData<Member>(
  111. queryClient,
  112. getMemberQueryKey(organization.slug, member.id),
  113. data
  114. );
  115. },
  116. onError: error => {
  117. addErrorMessage(
  118. (error?.responseJSON?.detail as string) ?? t('Failed to update member')
  119. );
  120. },
  121. });
  122. const {mutate: inviteMember, isPending: isInviting} = useMutation<Member, RequestError>(
  123. {
  124. mutationFn: () => {
  125. return resendMemberInvite(api, {
  126. orgId: organization.slug,
  127. memberId: member.id,
  128. regenerate: true,
  129. });
  130. },
  131. onSuccess: data => {
  132. addSuccessMessage(t('Sent invite!'));
  133. setApiQueryData<Member>(
  134. queryClient,
  135. getMemberQueryKey(organization.slug, member.id),
  136. data
  137. );
  138. },
  139. onError: () => {
  140. addErrorMessage(t('Could not send invite'));
  141. },
  142. }
  143. );
  144. const {mutate: reset2fa, isPending: isResetting2fa} = useMutation<unknown>({
  145. mutationFn: () => {
  146. const {user} = member;
  147. const promises =
  148. user?.authenticators?.map(auth => removeAuthenticator(api, user.id, auth.id)) ??
  149. [];
  150. return Promise.all(promises);
  151. },
  152. onSuccess: () => {
  153. addSuccessMessage(t('All authenticators have been removed'));
  154. navigate(`/settings/${organization.slug}/members/`);
  155. },
  156. onError: error => {
  157. addErrorMessage(t('Error removing authenticators'));
  158. Sentry.captureException(error);
  159. },
  160. });
  161. const onAddTeam = (teamSlug: string) => {
  162. const newTeamRoles = [...teamRoles];
  163. const i = newTeamRoles.findIndex(r => r.teamSlug === teamSlug);
  164. if (i !== -1) {
  165. return;
  166. }
  167. newTeamRoles.push({teamSlug, role: null});
  168. setTeamRoles(newTeamRoles);
  169. };
  170. const onRemoveTeam = (teamSlug: string) => {
  171. const newTeamRoles = teamRoles.filter(r => r.teamSlug !== teamSlug);
  172. setTeamRoles(newTeamRoles);
  173. };
  174. const onChangeTeamRole = (teamSlug: string, role: string) => {
  175. if (!hasTeamRoles) {
  176. return;
  177. }
  178. const newTeamRoles = [...teamRoles];
  179. const i = newTeamRoles.findIndex(r => r.teamSlug === teamSlug);
  180. if (i === -1) {
  181. return;
  182. }
  183. newTeamRoles[i] = {...newTeamRoles[i], role};
  184. setTeamRoles(newTeamRoles);
  185. };
  186. const showResetButton = useMemo(() => {
  187. const {user} = member;
  188. if (!user || !user.authenticators || organization.require2FA) {
  189. return false;
  190. }
  191. const hasAuth = user.authenticators.length >= 1;
  192. return hasAuth && user.canReset2fa;
  193. }, [member, organization.require2FA]);
  194. const getTooltip = (): string => {
  195. const {user} = member;
  196. if (!user) {
  197. return '';
  198. }
  199. if (!user.authenticators) {
  200. return NO_PERMISSION;
  201. }
  202. if (!user.authenticators.length) {
  203. return NOT_ENROLLED;
  204. }
  205. if (!user.canReset2fa) {
  206. return MULTIPLE_ORGS;
  207. }
  208. if (organization.require2FA) {
  209. return TWO_FACTOR_REQUIRED;
  210. }
  211. return '';
  212. };
  213. function hasFormChanged() {
  214. if (!member) {
  215. return false;
  216. }
  217. if (orgRole !== member.orgRole || !isEqual(teamRoles, member.teamRoles)) {
  218. return true;
  219. }
  220. return false;
  221. }
  222. const memberDeactivated = isMemberDisabledFromLimit(member);
  223. const canEdit = organization.access.includes('org:write') && !memberDeactivated;
  224. const isPartnershipUser = member.flags['partnership:restricted'] === true;
  225. const {email, expired, pending} = member;
  226. const canResend = !expired;
  227. const showAuth = !pending;
  228. const showResendButton = (member.pending || member.expired) && canResend;
  229. return (
  230. <Fragment>
  231. <SentryDocumentTitle title={t('%s Member Settings', member.name || member.email)} />
  232. <SettingsPageHeader
  233. title={
  234. <Fragment>
  235. <div>{member.name}</div>
  236. <ExtraHeaderText>{t('Member Settings')}</ExtraHeaderText>
  237. </Fragment>
  238. }
  239. />
  240. <Panel>
  241. <PanelHeader hasButtons={showResendButton}>
  242. {t('Basics')}
  243. {showResendButton && (
  244. <Button
  245. data-test-id="resend-invite"
  246. size="xs"
  247. priority="primary"
  248. icon={<IconRefresh />}
  249. title={t('Generate a new invite link and send a new email.')}
  250. busy={isInviting}
  251. onClick={() => inviteMember()}
  252. >
  253. {t('Resend Invite')}
  254. </Button>
  255. )}
  256. </PanelHeader>
  257. <PanelBody>
  258. <PanelItem>
  259. <Details>
  260. <div>
  261. <DetailLabel>{t('Email')}</DetailLabel>
  262. <div>
  263. <ExternalLink href={`mailto:${email}`}>{email}</ExternalLink>
  264. </div>
  265. </div>
  266. <div>
  267. <DetailLabel>{t('Status')}</DetailLabel>
  268. <div data-test-id="member-status">
  269. <MemberStatus member={member} memberDeactivated={memberDeactivated} />
  270. </div>
  271. </div>
  272. <div>
  273. <DetailLabel>{t('Added')}</DetailLabel>
  274. <div>
  275. <DateTime dateOnly date={member.dateCreated} />
  276. </div>
  277. </div>
  278. </Details>
  279. </PanelItem>
  280. </PanelBody>
  281. </Panel>
  282. {showAuth && (
  283. <Panel>
  284. <PanelHeader>{t('Authentication')}</PanelHeader>
  285. <PanelBody>
  286. <FieldGroup
  287. alignRight
  288. flexibleControlStateSize
  289. label={t('Reset two-factor authentication')}
  290. help={t(
  291. 'Resetting two-factor authentication will remove all two-factor authentication methods for this member.'
  292. )}
  293. >
  294. <Tooltip disabled={showResetButton} title={getTooltip()}>
  295. <Confirm
  296. disabled={!showResetButton}
  297. message={tct(
  298. 'Are you sure you want to disable all two-factor authentication methods for [name]?',
  299. {name: member.name ? member.name : 'this member'}
  300. )}
  301. onConfirm={() => reset2fa()}
  302. >
  303. <Button priority="danger" busy={isResetting2fa}>
  304. {t('Reset two-factor authentication')}
  305. </Button>
  306. </Confirm>
  307. </Tooltip>
  308. </FieldGroup>
  309. </PanelBody>
  310. </Panel>
  311. )}
  312. <OrganizationRoleSelect
  313. enforceAllowed={false}
  314. enforceRetired={hasTeamRoles}
  315. disabled={!canEdit || isPartnershipUser}
  316. roleList={organization.orgRoleList}
  317. roleSelected={orgRole}
  318. setSelected={(newOrgRole: Member['orgRole']) => {
  319. setOrgRole(newOrgRole);
  320. }}
  321. helpText={
  322. isPartnershipUser
  323. ? t('You cannot make changes to this partner-provisioned user.')
  324. : undefined
  325. }
  326. />
  327. <Teams slugs={member.teams}>
  328. {({initiallyLoaded}) => (
  329. <TeamSelectForMember
  330. disabled={!canEdit}
  331. organization={organization}
  332. member={member}
  333. selectedOrgRole={orgRole}
  334. selectedTeamRoles={teamRoles}
  335. onChangeTeamRole={onChangeTeamRole}
  336. onAddTeam={onAddTeam}
  337. onRemoveTeam={onRemoveTeam}
  338. loadingTeams={!initiallyLoaded}
  339. />
  340. )}
  341. </Teams>
  342. <Footer>
  343. <Button
  344. priority="primary"
  345. busy={isSaving}
  346. onClick={() => updatedMember()}
  347. disabled={!canEdit || !hasFormChanged()}
  348. >
  349. {t('Save Member')}
  350. </Button>
  351. </Footer>
  352. </Fragment>
  353. );
  354. }
  355. function OrganizationMemberDetail() {
  356. const params = useParams<{memberId: string}>();
  357. const organization = useOrganization();
  358. const {
  359. data: member,
  360. isPending,
  361. isError,
  362. refetch,
  363. } = useApiQuery<Member>(getMemberQueryKey(organization.slug, params.memberId), {
  364. staleTime: 0,
  365. });
  366. if (isPending) {
  367. return <LoadingIndicator />;
  368. }
  369. if (isError) {
  370. return <LoadingError onRetry={refetch} />;
  371. }
  372. if (!member) {
  373. return <NotFound />;
  374. }
  375. return <OrganizationMemberDetailContent member={member} />;
  376. }
  377. export default OrganizationMemberDetail;
  378. const ExtraHeaderText = styled('div')`
  379. color: ${p => p.theme.gray300};
  380. font-weight: ${p => p.theme.fontWeightNormal};
  381. font-size: ${p => p.theme.fontSizeLarge};
  382. `;
  383. const Details = styled('div')`
  384. display: grid;
  385. grid-auto-flow: column;
  386. grid-template-columns: 2fr 1fr 1fr;
  387. gap: ${space(2)};
  388. width: 100%;
  389. @media (max-width: ${p => p.theme.breakpoints.small}) {
  390. grid-auto-flow: row;
  391. grid-template-columns: auto;
  392. }
  393. `;
  394. const DetailLabel = styled('div')`
  395. font-weight: ${p => p.theme.fontWeightBold};
  396. margin-bottom: ${space(0.5)};
  397. color: ${p => p.theme.textColor};
  398. `;
  399. const Footer = styled('div')`
  400. display: flex;
  401. justify-content: flex-end;
  402. `;