index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {t} from 'sentry/locale';
  4. import {AuthProvider, Organization} from 'sentry/types';
  5. import routeTitleGen from 'sentry/utils/routeTitle';
  6. import withOrganization from 'sentry/utils/withOrganization';
  7. import AsyncView from 'sentry/views/asyncView';
  8. import OrganizationAuthList from './organizationAuthList';
  9. type Props = AsyncView['props'] &
  10. RouteComponentProps<{orgId: string}, {}> & {
  11. organization: Organization;
  12. };
  13. type State = AsyncView['state'] & {
  14. provider: AuthProvider | null;
  15. providerList: AuthProvider[] | null;
  16. };
  17. class OrganizationAuth extends AsyncView<Props, State> {
  18. componentDidUpdate() {
  19. const access = this.props.organization.access;
  20. if (this.state.provider && access.includes('org:write')) {
  21. // If SSO provider is configured, keep showing loading while we redirect
  22. // to django configuration view
  23. const path = `/organizations/${this.props.params.orgId}/auth/configure/`;
  24. // Don't break the back button by first replacing the current history
  25. // state so pressing back skips this react view.
  26. this.props.router.replace(path);
  27. window.location.assign(path);
  28. }
  29. }
  30. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  31. return [
  32. ['providerList', `/organizations/${this.props.params.orgId}/auth-providers/`],
  33. ['provider', `/organizations/${this.props.params.orgId}/auth-provider/`],
  34. ];
  35. }
  36. getTitle() {
  37. return routeTitleGen(t('Auth Settings'), this.props.organization.slug, false);
  38. }
  39. /**
  40. * TODO(epurkhiser): This does not work right now as we still fallback to the
  41. * old SSO auth configuration page
  42. */
  43. handleSendReminders = (_provider: AuthProvider) => {
  44. this.setState({sendRemindersBusy: true});
  45. this.api.request(
  46. `/organizations/${this.props.params.orgId}/auth-provider/send-reminders/`,
  47. {
  48. method: 'POST',
  49. data: {},
  50. success: () => addSuccessMessage(t('Sent reminders to members')),
  51. error: () => addErrorMessage(t('Failed to send reminders')),
  52. complete: () => this.setState({sendRemindersBusy: false}),
  53. }
  54. );
  55. };
  56. /**
  57. * TODO(epurkhiser): This does not work right now as we still fallback to the
  58. * old SSO auth configuration page
  59. */
  60. handleConfigure = (provider: AuthProvider) => {
  61. this.setState({busy: true});
  62. this.api.request(`/organizations/${this.props.params.orgId}/auth-provider/`, {
  63. method: 'POST',
  64. data: {provider, init: true},
  65. success: data => {
  66. // Redirect to auth provider URL
  67. if (data && data.auth_url) {
  68. window.location.href = data.auth_url;
  69. }
  70. },
  71. error: () => {
  72. this.setState({busy: false});
  73. },
  74. });
  75. };
  76. /**
  77. * TODO(epurkhiser): This does not work right now as we still fallback to the
  78. * old SSO auth configuration page
  79. */
  80. handleDisableProvider = (provider: AuthProvider) => {
  81. this.setState({busy: true});
  82. this.api.request(`/organizations/${this.props.params.orgId}/auth-provider/`, {
  83. method: 'DELETE',
  84. data: {provider},
  85. success: () => {
  86. this.setState({provider: null, busy: false});
  87. },
  88. error: () => {
  89. this.setState({busy: false});
  90. },
  91. });
  92. };
  93. renderBody() {
  94. const {providerList, provider} = this.state;
  95. if (providerList === null) {
  96. return null;
  97. }
  98. if (this.props.organization.access.includes('org:write') && provider) {
  99. // If SSO provider is configured, keep showing loading while we redirect
  100. // to django configuration view
  101. return this.renderLoading();
  102. }
  103. const activeProvider = providerList?.find(p => p.key === provider?.key);
  104. return (
  105. <OrganizationAuthList activeProvider={activeProvider} providerList={providerList} />
  106. );
  107. }
  108. }
  109. export default withOrganization(OrganizationAuth);