auditLogList.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 {retentionPrioritiesLabels} from 'sentry/views/settings/projectPerformance/projectPerformance';
  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 (entry.event === 'sampling_priority.enabled') {
  107. return (
  108. <Note>
  109. {tct(
  110. 'Enabled retention priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  111. {
  112. samplingInProjectSettingsLink: (
  113. <Link to={`/settings/${orgSlug}/projects/${project.slug}/performance/`}>
  114. {entry.data.slug}
  115. </Link>
  116. ),
  117. biasLabel: retentionPrioritiesLabels[entry.data.name],
  118. }
  119. )}
  120. </Note>
  121. );
  122. }
  123. if (entry.event === 'sampling_priority.disabled') {
  124. return (
  125. <Note>
  126. {tct(
  127. 'Disabled retention priority "[biasLabel]" in project [samplingInProjectSettingsLink]',
  128. {
  129. samplingInProjectSettingsLink: (
  130. <Link to={`/settings/${orgSlug}/projects/${project.slug}/performance/`}>
  131. {entry.data.slug}
  132. </Link>
  133. ),
  134. biasLabel: retentionPrioritiesLabels[entry.data.name],
  135. }
  136. )}
  137. </Note>
  138. );
  139. }
  140. return <Note>{entry.note}</Note>;
  141. }
  142. type Props = {
  143. entries: AuditLog[] | null;
  144. eventType: string | undefined;
  145. eventTypes: string[] | null;
  146. isLoading: boolean;
  147. onCursor: CursorHandler | undefined;
  148. onEventSelect: (value: string) => void;
  149. pageLinks: string | null;
  150. };
  151. function AuditLogList({
  152. isLoading,
  153. pageLinks,
  154. entries,
  155. eventType,
  156. eventTypes,
  157. onCursor,
  158. onEventSelect,
  159. }: Props) {
  160. const is24Hours = shouldUse24Hours();
  161. const organization = useOrganization();
  162. const hasEntries = entries && entries.length > 0;
  163. const ipv4Length = 15;
  164. const eventOptions = eventTypes?.map(type => ({
  165. label: type,
  166. value: type,
  167. }));
  168. const action = (
  169. <EventSelector
  170. clearable
  171. isDisabled={isLoading}
  172. name="eventFilter"
  173. value={eventType}
  174. placeholder={t('Select Action: ')}
  175. options={eventOptions}
  176. onChange={options => {
  177. onEventSelect(options?.value);
  178. }}
  179. />
  180. );
  181. return (
  182. <div>
  183. <SettingsPageHeader title={t('Audit Log')} action={action} />
  184. <PanelTable
  185. headers={[t('Member'), t('Action'), t('IP'), t('Time')]}
  186. isEmpty={!hasEntries && entries?.length === 0}
  187. emptyMessage={t('No audit entries available')}
  188. isLoading={isLoading}
  189. >
  190. {(entries ?? []).map(entry => {
  191. if (!entry) {
  192. return null;
  193. }
  194. return (
  195. <Fragment key={entry.id}>
  196. <UserInfo>
  197. <div>{getAvatarDisplay(entry.actor)}</div>
  198. <NameContainer>
  199. {addUsernameDisplay(entry.actor)}
  200. <AuditNote entry={entry} orgSlug={organization.slug} />
  201. </NameContainer>
  202. </UserInfo>
  203. <FlexCenter>
  204. <MonoDetail>{entry.event}</MonoDetail>
  205. </FlexCenter>
  206. <FlexCenter>
  207. {entry.ipAddress && (
  208. <IpAddressOverflow>
  209. <Tooltip
  210. title={entry.ipAddress}
  211. disabled={entry.ipAddress.length <= ipv4Length}
  212. >
  213. <MonoDetail>{entry.ipAddress}</MonoDetail>
  214. </Tooltip>
  215. </IpAddressOverflow>
  216. )}
  217. </FlexCenter>
  218. <TimestampInfo>
  219. <DateTime dateOnly date={entry.dateCreated} />
  220. <DateTime
  221. timeOnly
  222. format={is24Hours ? 'HH:mm zz' : 'LT zz'}
  223. date={entry.dateCreated}
  224. />
  225. </TimestampInfo>
  226. </Fragment>
  227. );
  228. })}
  229. </PanelTable>
  230. {pageLinks && <Pagination pageLinks={pageLinks} onCursor={onCursor} />}
  231. </div>
  232. );
  233. }
  234. const SentryAvatar = styled(ActivityAvatar)`
  235. margin-right: ${space(1)};
  236. `;
  237. const Name = styled('strong')`
  238. font-size: ${p => p.theme.fontSizeMedium};
  239. `;
  240. const StaffTag = styled(Tag)`
  241. padding: ${space(1)};
  242. `;
  243. const EventSelector = styled(SelectControl)`
  244. width: 250px;
  245. `;
  246. const UserInfo = styled('div')`
  247. display: flex;
  248. align-items: center;
  249. line-height: 1.2;
  250. font-size: ${p => p.theme.fontSizeSmall};
  251. min-width: 250px;
  252. `;
  253. const NameContainer = styled('div')`
  254. display: flex;
  255. flex-direction: column;
  256. justify-content: center;
  257. `;
  258. const Note = styled('div')`
  259. font-size: ${p => p.theme.fontSizeSmall};
  260. word-break: break-word;
  261. margin-top: ${space(0.5)};
  262. `;
  263. const FlexCenter = styled('div')`
  264. display: flex;
  265. align-items: center;
  266. `;
  267. const IpAddressOverflow = styled('div')`
  268. ${p => p.theme.overflowEllipsis};
  269. min-width: 90px;
  270. `;
  271. const MonoDetail = styled('code')`
  272. font-size: ${p => p.theme.fontSizeMedium};
  273. white-space: no-wrap;
  274. `;
  275. const TimestampInfo = styled('div')`
  276. display: grid;
  277. grid-template-rows: auto auto;
  278. gap: ${space(1)};
  279. font-size: ${p => p.theme.fontSizeMedium};
  280. `;
  281. export default AuditLogList;