organizationMemberRow.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import {Fragment, PureComponent} from 'react';
  2. import styled from '@emotion/styled';
  3. import UserAvatar from 'sentry/components/avatar/userAvatar';
  4. import {Button} from 'sentry/components/button';
  5. import Confirm from 'sentry/components/confirm';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import Link from 'sentry/components/links/link';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import PanelItem from 'sentry/components/panels/panelItem';
  10. import {IconCheckmark, IconClose, IconFlag, IconMail, IconSubtract} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Member, Organization} from 'sentry/types/organization';
  14. import type {AvatarUser} from 'sentry/types/user';
  15. import isMemberDisabledFromLimit from 'sentry/utils/isMemberDisabledFromLimit';
  16. import {capitalize} from 'sentry/utils/string/capitalize';
  17. type Props = {
  18. canAddMembers: boolean;
  19. canRemoveMembers: boolean;
  20. currentUser: AvatarUser;
  21. member: Member;
  22. memberCanLeave: boolean;
  23. onLeave: (member: Member) => void;
  24. onRemove: (member: Member) => void;
  25. onSendInvite: (member: Member) => void;
  26. organization: Organization;
  27. requireLink: boolean;
  28. status: '' | 'loading' | 'success' | 'error' | null;
  29. };
  30. type State = {
  31. busy: boolean;
  32. };
  33. const DisabledMemberTooltip = HookOrDefault({
  34. hookName: 'component:disabled-member-tooltip',
  35. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  36. });
  37. export default class OrganizationMemberRow extends PureComponent<Props, State> {
  38. state: State = {
  39. busy: false,
  40. };
  41. handleRemove = () => {
  42. const {onRemove} = this.props;
  43. if (typeof onRemove !== 'function') {
  44. return;
  45. }
  46. this.setState({busy: true});
  47. onRemove(this.props.member);
  48. };
  49. handleLeave = () => {
  50. const {onLeave} = this.props;
  51. if (typeof onLeave !== 'function') {
  52. return;
  53. }
  54. this.setState({busy: true});
  55. onLeave(this.props.member);
  56. };
  57. handleSendInvite = () => {
  58. const {onSendInvite, member} = this.props;
  59. if (typeof onSendInvite !== 'function') {
  60. return;
  61. }
  62. onSendInvite(member);
  63. };
  64. renderMemberRole() {
  65. const {member} = this.props;
  66. const {roleName, pending, expired} = member;
  67. if (isMemberDisabledFromLimit(member)) {
  68. return <DisabledMemberTooltip>{t('Deactivated')}</DisabledMemberTooltip>;
  69. }
  70. if (pending) {
  71. return (
  72. <InvitedRole>
  73. <IconMail size="md" />
  74. {expired ? t('Expired Invite') : tct('Invited [roleName]', {roleName})}
  75. </InvitedRole>
  76. );
  77. }
  78. return <Fragment>{capitalize(member.orgRole)}</Fragment>;
  79. }
  80. render() {
  81. const {
  82. member,
  83. organization,
  84. status,
  85. requireLink,
  86. memberCanLeave,
  87. currentUser,
  88. canRemoveMembers,
  89. canAddMembers,
  90. } = this.props;
  91. const {id, flags, email, name, pending, user} = member;
  92. // if member is not the only owner, they can leave
  93. const isIdpProvisioned = flags['idp:provisioned'];
  94. const isPartnershipUser = flags['partnership:restricted'];
  95. const needsSso = !flags['sso:linked'] && requireLink;
  96. const isCurrentUser = currentUser.email === email;
  97. const showRemoveButton = !isCurrentUser;
  98. const showLeaveButton = isCurrentUser;
  99. const canRemoveMember =
  100. canRemoveMembers && !isCurrentUser && !isIdpProvisioned && !isPartnershipUser;
  101. // member has a `user` property if they are registered with sentry
  102. // i.e. has accepted an invite to join org
  103. const has2fa = user?.has2fa;
  104. const detailsUrl = `/settings/${organization.slug}/members/${id}/`;
  105. const isInviteSuccessful = status === 'success';
  106. const isInviting = status === 'loading';
  107. const showResendButton = pending || needsSso;
  108. return (
  109. <StyledPanelItem data-test-id={email}>
  110. <MemberHeading>
  111. <UserAvatar
  112. size={32}
  113. user={user ?? {email, id: email, name: email, type: 'user'}}
  114. />
  115. <MemberDescription to={detailsUrl}>
  116. <h5 style={{margin: '0 0 3px'}}>
  117. <UserName>{name}</UserName>
  118. </h5>
  119. <Email>{email}</Email>
  120. </MemberDescription>
  121. </MemberHeading>
  122. <div data-test-id="member-role">{this.renderMemberRole()}</div>
  123. <div data-test-id="member-status">
  124. {showResendButton ? (
  125. <Fragment>
  126. {isInviting && (
  127. <LoadingContainer>
  128. <LoadingIndicator mini />
  129. </LoadingContainer>
  130. )}
  131. {isInviteSuccessful && <span>{t('Sent!')}</span>}
  132. {!isInviting && !isInviteSuccessful && (
  133. <Button
  134. disabled={!canAddMembers}
  135. priority="primary"
  136. size="sm"
  137. onClick={this.handleSendInvite}
  138. >
  139. {pending ? t('Resend invite') : t('Resend SSO link')}
  140. </Button>
  141. )}
  142. </Fragment>
  143. ) : (
  144. <AuthStatus>
  145. {has2fa ? (
  146. <IconCheckmark isCircled color="success" />
  147. ) : (
  148. <IconFlag color="error" />
  149. )}
  150. {has2fa ? t('2FA Enabled') : t('2FA Not Enabled')}
  151. </AuthStatus>
  152. )}
  153. </div>
  154. {showRemoveButton || showLeaveButton ? (
  155. <RightColumn>
  156. {showRemoveButton && canRemoveMember && (
  157. <Confirm
  158. message={tct('Are you sure you want to remove [name] from [orgName]?', {
  159. name,
  160. orgName: organization.slug,
  161. })}
  162. onConfirm={this.handleRemove}
  163. >
  164. <Button
  165. data-test-id="remove"
  166. icon={<IconSubtract isCircled />}
  167. size="sm"
  168. busy={this.state.busy}
  169. >
  170. {t('Remove')}
  171. </Button>
  172. </Confirm>
  173. )}
  174. {showRemoveButton && !canRemoveMember && (
  175. <Button
  176. disabled
  177. size="sm"
  178. title={
  179. isIdpProvisioned
  180. ? t(
  181. "This user is managed through your organization's identity provider."
  182. )
  183. : isPartnershipUser
  184. ? t('You cannot make changes to this partner-provisioned user.')
  185. : t('You do not have access to remove members')
  186. }
  187. icon={<IconSubtract isCircled />}
  188. >
  189. {t('Remove')}
  190. </Button>
  191. )}
  192. {showLeaveButton && memberCanLeave && (
  193. <Confirm
  194. message={tct('Are you sure you want to leave [orgName]?', {
  195. orgName: organization.slug,
  196. })}
  197. onConfirm={this.handleLeave}
  198. >
  199. <Button priority="danger" size="sm" icon={<IconClose />}>
  200. {t('Leave')}
  201. </Button>
  202. </Confirm>
  203. )}
  204. {showLeaveButton && !memberCanLeave && (
  205. <Button
  206. size="sm"
  207. icon={<IconClose />}
  208. disabled
  209. title={
  210. isIdpProvisioned
  211. ? t(
  212. "Your account is managed through your organization's identity provider."
  213. )
  214. : isPartnershipUser
  215. ? t('You cannot make changes as a partner-provisioned user.')
  216. : t(
  217. 'You cannot leave this organization as you are the only organization owner.'
  218. )
  219. }
  220. >
  221. {t('Leave')}
  222. </Button>
  223. )}
  224. </RightColumn>
  225. ) : null}
  226. </StyledPanelItem>
  227. );
  228. }
  229. }
  230. const StyledPanelItem = styled(PanelItem)`
  231. display: grid;
  232. grid-template-columns: minmax(150px, 4fr) minmax(90px, 2fr) minmax(120px, 2fr) minmax(
  233. 100px,
  234. 1fr
  235. );
  236. gap: ${space(2)};
  237. align-items: center;
  238. `;
  239. // Force action button at the end to align to right
  240. const RightColumn = styled('div')`
  241. display: flex;
  242. justify-content: flex-end;
  243. `;
  244. const Section = styled('div')`
  245. display: inline-grid;
  246. grid-template-columns: max-content auto;
  247. gap: ${space(1)};
  248. align-items: center;
  249. `;
  250. const MemberHeading = styled(Section)``;
  251. const MemberDescription = styled(Link)`
  252. overflow: hidden;
  253. `;
  254. const UserName = styled('div')`
  255. display: block;
  256. overflow: hidden;
  257. font-size: ${p => p.theme.fontSizeMedium};
  258. text-overflow: ellipsis;
  259. `;
  260. const Email = styled('div')`
  261. color: ${p => p.theme.subText};
  262. font-size: ${p => p.theme.fontSizeSmall};
  263. overflow: hidden;
  264. text-overflow: ellipsis;
  265. `;
  266. const InvitedRole = styled(Section)``;
  267. const LoadingContainer = styled('div')`
  268. margin-top: 0;
  269. margin-bottom: ${space(1.5)};
  270. `;
  271. const AuthStatus = styled(Section)``;