auditLogList.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import ActivityAvatar from 'sentry/components/activity/item/avatar';
  4. import UserAvatar from 'sentry/components/avatar/userAvatar';
  5. import DateTime from 'sentry/components/dateTime';
  6. import SelectControl from 'sentry/components/forms/controls/selectControl';
  7. import Link from 'sentry/components/links/link';
  8. import Pagination, {CursorHandler} from 'sentry/components/pagination';
  9. import {PanelTable} from 'sentry/components/panels';
  10. import Tag from 'sentry/components/tag';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {AuditLog, Organization, User} from 'sentry/types';
  15. import {shouldUse24Hours} from 'sentry/utils/dates';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import useProjects from 'sentry/utils/useProjects';
  18. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  19. import {knowDynamicSamplingBiases} from 'sentry/views/settings/project/dynamicSampling/dynamicSampling';
  20. const avatarStyle = {
  21. width: 36,
  22. height: 36,
  23. marginRight: 8,
  24. };
  25. const getAvatarDisplay = (logEntryUser: User | undefined) => {
  26. // Display Sentry's avatar for system or superuser-initiated events
  27. if (
  28. logEntryUser?.isSuperuser ||
  29. (logEntryUser?.name === 'Sentry' && logEntryUser?.email === undefined)
  30. ) {
  31. return <SentryAvatar type="system" size={36} />;
  32. }
  33. // Display user's avatar for non-superusers-initiated events
  34. if (logEntryUser !== undefined) {
  35. return <UserAvatar style={avatarStyle} user={logEntryUser} />;
  36. }
  37. return null;
  38. };
  39. const addUsernameDisplay = (logEntryUser: User | undefined) => {
  40. if (logEntryUser?.isSuperuser) {
  41. return (
  42. <Name data-test-id="actor-name">
  43. {logEntryUser.name}
  44. <StaffTag>{t('Sentry Staff')}</StaffTag>
  45. </Name>
  46. );
  47. }
  48. if (logEntryUser !== undefined) {
  49. return <Name data-test-id="actor-name">{logEntryUser.name}</Name>;
  50. }
  51. return null;
  52. };
  53. function AuditNote({
  54. entry,
  55. orgSlug,
  56. }: {
  57. entry: NonNullable<AuditLog>;
  58. orgSlug: Organization['slug'];
  59. }) {
  60. const {projects} = useProjects();
  61. const project = projects.find(p => p.id === String(entry.data.id));
  62. if (!project) {
  63. return <Note>{entry.note}</Note>;
  64. }
  65. if (entry.event === 'project.create') {
  66. return (
  67. <Note>
  68. {tct('Created project [projectSettingsLink]', {
  69. projectSettingsLink: (
  70. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  71. {entry.data.slug}
  72. </Link>
  73. ),
  74. })}
  75. </Note>
  76. );
  77. }
  78. if (entry.event === 'project.edit') {
  79. if (entry.data.old_slug && entry.data.new_slug) {
  80. return (
  81. <Note>
  82. {tct('Renamed project slug from [old-slug] to [new-slug]', {
  83. 'old-slug': entry.data.old_slug,
  84. 'new-slug': (
  85. <Link to={`/settings/${orgSlug}/projects/${entry.data.new_slug}/`}>
  86. {entry.data.new_slug}
  87. </Link>
  88. ),
  89. })}
  90. </Note>
  91. );
  92. }
  93. return (
  94. <Note>
  95. {tct('Edited project [projectSettingsLink] [note]', {
  96. projectSettingsLink: (
  97. <Link to={`/settings/${orgSlug}/projects/${project.slug}/`}>
  98. {entry.data.slug}
  99. </Link>
  100. ),
  101. note: entry.note.replace('edited project settings ', ''),
  102. })}
  103. </Note>
  104. );
  105. }
  106. if (
  107. entry.event === 'sampling_priority.enabled' &&
  108. knowDynamicSamplingBiases[entry.data.name]
  109. ) {
  110. return (
  111. <Note>
  112. {tct(
  113. 'Enabled dynamic sampling priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  114. {
  115. samplingInProjectSettingsLink: (
  116. <Link
  117. to={`/settings/${orgSlug}/projects/${project.slug}/dynamic-sampling/`}
  118. >
  119. {entry.data.slug}
  120. </Link>
  121. ),
  122. biasLabel: knowDynamicSamplingBiases[entry.data.name].label,
  123. }
  124. )}
  125. </Note>
  126. );
  127. }
  128. if (
  129. entry.event === 'sampling_priority.disabled' &&
  130. knowDynamicSamplingBiases[entry.data.name]
  131. ) {
  132. return (
  133. <Note>
  134. {tct(
  135. 'Disabled dynamic sampling priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  136. {
  137. samplingInProjectSettingsLink: (
  138. <Link
  139. to={`/settings/${orgSlug}/projects/${project.slug}/dynamic-sampling/`}
  140. >
  141. {entry.data.slug}
  142. </Link>
  143. ),
  144. biasLabel: knowDynamicSamplingBiases[entry.data.name].label,
  145. }
  146. )}
  147. </Note>
  148. );
  149. }
  150. return <Note>{entry.note}</Note>;
  151. }
  152. type Props = {
  153. entries: AuditLog[] | null;
  154. eventType: string | undefined;
  155. eventTypes: string[] | null;
  156. isLoading: boolean;
  157. onCursor: CursorHandler | undefined;
  158. onEventSelect: (value: string) => void;
  159. pageLinks: string | null;
  160. };
  161. const AuditLogList = ({
  162. isLoading,
  163. pageLinks,
  164. entries,
  165. eventType,
  166. eventTypes,
  167. onCursor,
  168. onEventSelect,
  169. }: Props) => {
  170. const is24Hours = shouldUse24Hours();
  171. const organization = useOrganization();
  172. const hasEntries = entries && entries.length > 0;
  173. const ipv4Length = 15;
  174. const eventOptions = eventTypes?.map(type => ({
  175. label: type,
  176. value: type,
  177. }));
  178. const action = (
  179. <EventSelector
  180. clearable
  181. isDisabled={isLoading}
  182. name="eventFilter"
  183. value={eventType}
  184. placeholder={t('Select Action: ')}
  185. options={eventOptions}
  186. onChange={options => {
  187. onEventSelect(options?.value);
  188. }}
  189. />
  190. );
  191. return (
  192. <div>
  193. <SettingsPageHeader title={t('Audit Log')} action={action} />
  194. <PanelTable
  195. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  196. isEmpty={!hasEntries && entries?.length === 0}
  197. emptyMessage={t('No audit entries available')}
  198. isLoading={isLoading}
  199. >
  200. {(entries ?? []).map(entry => {
  201. if (!entry) {
  202. return null;
  203. }
  204. return (
  205. <Fragment key={entry.id}>
  206. <UserInfo>
  207. <div>{getAvatarDisplay(entry.actor)}</div>
  208. <NameContainer>
  209. {addUsernameDisplay(entry.actor)}
  210. <AuditNote entry={entry} orgSlug={organization.slug} />
  211. </NameContainer>
  212. </UserInfo>
  213. <FlexCenter>
  214. <MonoDetail>{entry.event}</MonoDetail>
  215. </FlexCenter>
  216. <FlexCenter>
  217. {entry.ipAddress && (
  218. <IpAddressOverflow>
  219. <Tooltip
  220. title={entry.ipAddress}
  221. disabled={entry.ipAddress.length <= ipv4Length}
  222. >
  223. <MonoDetail>{entry.ipAddress}</MonoDetail>
  224. </Tooltip>
  225. </IpAddressOverflow>
  226. )}
  227. </FlexCenter>
  228. <TimestampInfo>
  229. <DateTime dateOnly date={entry.dateCreated} />
  230. <DateTime
  231. timeOnly
  232. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  233. date={entry.dateCreated}
  234. />
  235. </TimestampInfo>
  236. </Fragment>
  237. );
  238. })}
  239. </PanelTable>
  240. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  241. </div>
  242. );
  243. };
  244. const SentryAvatar = styled(ActivityAvatar)`
  245. margin-right: ${space(1)};
  246. `;
  247. const Name = styled('strong')`
  248. font-size: ${p => p.theme.fontSizeMedium};
  249. `;
  250. const StaffTag = styled(Tag)`
  251. padding: ${space(1)};
  252. `;
  253. const EventSelector = styled(SelectControl)`
  254. width: 250px;
  255. `;
  256. const UserInfo = styled('div')`
  257. display: flex;
  258. align-items: center;
  259. line-height: 1.2;
  260. font-size: ${p => p.theme.fontSizeSmall};
  261. min-width: 250px;
  262. `;
  263. const NameContainer = styled('div')`
  264. display: flex;
  265. flex-direction: column;
  266. justify-content: center;
  267. `;
  268. const Note = styled('div')`
  269. font-size: ${p => p.theme.fontSizeSmall};
  270. word-break: break-word;
  271. margin-top: ${space(0.5)};
  272. `;
  273. const FlexCenter = styled('div')`
  274. display: flex;
  275. align-items: center;
  276. `;
  277. const IpAddressOverflow = styled('div')`
  278. ${p => p.theme.overflowEllipsis};
  279. min-width: 90px;
  280. `;
  281. const MonoDetail = styled('code')`
  282. font-size: ${p => p.theme.fontSizeMedium};
  283. white-space: no-wrap;
  284. `;
  285. const TimestampInfo = styled('div')`
  286. display: grid;
  287. grid-template-rows: auto auto;
  288. gap: ${space(1)};
  289. font-size: ${p => p.theme.fontSizeMedium};
  290. `;
  291. export default AuditLogList;