superuserAccessForm.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {logout} from 'sentry/actionCreators/account';
  4. import {Client} from 'sentry/api';
  5. import Alert from 'sentry/components/alert';
  6. import Form from 'sentry/components/forms/form';
  7. import Hook from 'sentry/components/hook';
  8. import ThemeAndStyleProvider from 'sentry/components/themeAndStyleProvider';
  9. import {ErrorCodes} from 'sentry/constants/superuserAccessErrors';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import withApi from 'sentry/utils/withApi';
  13. import Button from './button';
  14. type Props = {
  15. api: Client;
  16. };
  17. type State = {
  18. error: boolean;
  19. errorType: string;
  20. };
  21. class SuperuserAccessForm extends Component<Props, State> {
  22. state: State = {
  23. error: false,
  24. errorType: '',
  25. };
  26. handleSubmit = async () => {
  27. const {api} = this.props;
  28. const data = {
  29. isSuperuserModal: true,
  30. superuserAccessCategory: 'cops_csm',
  31. superuserReason: 'COPS and CSM use',
  32. };
  33. try {
  34. await api.requestPromise('/auth/', {method: 'PUT', data});
  35. this.handleSuccess();
  36. } catch (err) {
  37. this.handleError(err);
  38. }
  39. };
  40. handleSuccess = () => {
  41. window.location.reload();
  42. };
  43. handleError = err => {
  44. let errorType = '';
  45. if (err.status === 403) {
  46. errorType = ErrorCodes.invalidPassword;
  47. } else if (err.status === 401) {
  48. errorType = ErrorCodes.invalidSSOSession;
  49. } else if (err.status === 400) {
  50. errorType = ErrorCodes.invalidAccessCategory;
  51. } else {
  52. errorType = ErrorCodes.unknownError;
  53. }
  54. this.setState({
  55. error: true,
  56. errorType,
  57. });
  58. };
  59. handleLogout = async () => {
  60. const {api} = this.props;
  61. try {
  62. await logout(api);
  63. } catch {
  64. // ignore errors
  65. }
  66. window.location.assign('/auth/login/');
  67. };
  68. render() {
  69. const {error, errorType} = this.state;
  70. if (errorType === ErrorCodes.invalidSSOSession) {
  71. this.handleLogout();
  72. return null;
  73. }
  74. return (
  75. <ThemeAndStyleProvider>
  76. <Form
  77. apiMethod="PUT"
  78. apiEndpoint="/auth/"
  79. submitLabel={t('Continue')}
  80. onSubmitSuccess={this.handleSuccess}
  81. onSubmitError={this.handleError}
  82. initialData={{isSuperuserModal: true}}
  83. extraButton={
  84. <BackWrapper>
  85. <Button onClick={this.handleSubmit}>{t('COPS/CSM')}</Button>
  86. </BackWrapper>
  87. }
  88. resetOnError
  89. >
  90. {error && (
  91. <StyledAlert type="error" showIcon>
  92. {t(errorType)}
  93. </StyledAlert>
  94. )}
  95. <Hook name="component:superuser-access-category" />
  96. </Form>
  97. </ThemeAndStyleProvider>
  98. );
  99. }
  100. }
  101. const StyledAlert = styled(Alert)`
  102. margin-bottom: 0;
  103. `;
  104. const BackWrapper = styled('div')`
  105. width: 100%;
  106. margin-left: ${space(4)};
  107. `;
  108. export default withApi(SuperuserAccessForm);