auditLogList.tsx 8.0 KB

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