LdapPage.tsx 4.8 KB

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