LdapPage.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { NavModel } from '@grafana/data';
  5. import { FormField } from '@grafana/ui';
  6. import { getNavModel } from 'app/core/selectors/navModel';
  7. import config from 'app/core/config';
  8. import Page from 'app/core/components/Page/Page';
  9. import { AlertBox } from 'app/core/components/AlertBox/AlertBox';
  10. import { LdapConnectionStatus } from './LdapConnectionStatus';
  11. import { LdapSyncInfo } from './LdapSyncInfo';
  12. import { LdapUserInfo } from './LdapUserInfo';
  13. import { AppNotificationSeverity, LdapError, LdapUser, StoreState, SyncInfo, LdapConnectionInfo } from 'app/types';
  14. import {
  15. loadLdapState,
  16. loadLdapSyncStatus,
  17. loadUserMapping,
  18. clearUserError,
  19. clearUserMappingInfo,
  20. } from '../state/actions';
  21. interface Props {
  22. navModel: NavModel;
  23. ldapConnectionInfo: LdapConnectionInfo;
  24. ldapUser: LdapUser;
  25. ldapSyncInfo: SyncInfo;
  26. ldapError: LdapError;
  27. userError?: LdapError;
  28. loadLdapState: typeof loadLdapState;
  29. loadLdapSyncStatus: typeof loadLdapSyncStatus;
  30. loadUserMapping: typeof loadUserMapping;
  31. clearUserError: typeof clearUserError;
  32. clearUserMappingInfo: typeof clearUserMappingInfo;
  33. }
  34. interface State {
  35. isLoading: boolean;
  36. }
  37. export class LdapPage extends PureComponent<Props, State> {
  38. state = {
  39. isLoading: true,
  40. };
  41. async componentDidMount() {
  42. await this.props.clearUserMappingInfo();
  43. await this.fetchLDAPStatus();
  44. this.setState({ isLoading: false });
  45. }
  46. async fetchLDAPStatus() {
  47. const { loadLdapState, loadLdapSyncStatus } = this.props;
  48. return Promise.all([loadLdapState(), loadLdapSyncStatus()]);
  49. }
  50. async fetchUserMapping(username: string) {
  51. const { loadUserMapping } = this.props;
  52. return await loadUserMapping(username);
  53. }
  54. search = (event: any) => {
  55. event.preventDefault();
  56. const username = event.target.elements['username'].value;
  57. if (username) {
  58. this.fetchUserMapping(username);
  59. }
  60. };
  61. onClearUserError = () => {
  62. this.props.clearUserError();
  63. };
  64. render() {
  65. const { ldapUser, userError, ldapError, ldapSyncInfo, ldapConnectionInfo, navModel } = this.props;
  66. const { isLoading } = this.state;
  67. return (
  68. <Page navModel={navModel}>
  69. <Page.Contents isLoading={isLoading}>
  70. <>
  71. {ldapError && ldapError.title && (
  72. <div className="gf-form-group">
  73. <AlertBox title={ldapError.title} severity={AppNotificationSeverity.Error} body={ldapError.body} />
  74. </div>
  75. )}
  76. <LdapConnectionStatus ldapConnectionInfo={ldapConnectionInfo} />
  77. {config.buildInfo.isEnterprise && ldapSyncInfo && <LdapSyncInfo ldapSyncInfo={ldapSyncInfo} />}
  78. <h3 className="page-heading">User mapping</h3>
  79. <div className="gf-form-group">
  80. <form onSubmit={this.search} className="gf-form-inline">
  81. <FormField label="User name" labelWidth={8} inputWidth={30} type="text" id="username" name="username" />
  82. <button type="submit" className="btn btn-primary">
  83. Test LDAP mapping
  84. </button>
  85. </form>
  86. </div>
  87. {userError && userError.title && (
  88. <div className="gf-form-group">
  89. <AlertBox
  90. title={userError.title}
  91. severity={AppNotificationSeverity.Error}
  92. body={userError.body}
  93. onClose={this.onClearUserError}
  94. />
  95. </div>
  96. )}
  97. {ldapUser && <LdapUserInfo ldapUser={ldapUser} showAttributeMapping={true} />}
  98. </>
  99. </Page.Contents>
  100. </Page>
  101. );
  102. }
  103. }
  104. const mapStateToProps = (state: StoreState) => ({
  105. navModel: getNavModel(state.navIndex, 'ldap'),
  106. ldapConnectionInfo: state.ldap.connectionInfo,
  107. ldapUser: state.ldap.user,
  108. ldapSyncInfo: state.ldap.syncInfo,
  109. userError: state.ldap.userError,
  110. ldapError: state.ldap.ldapError,
  111. });
  112. const mapDispatchToProps = {
  113. loadLdapState,
  114. loadLdapSyncStatus,
  115. loadUserMapping,
  116. clearUserError,
  117. clearUserMappingInfo,
  118. };
  119. export default hot(module)(
  120. connect(
  121. mapStateToProps,
  122. mapDispatchToProps
  123. )(LdapPage)
  124. );