grafana/public/app/features/admin/UserSyncInfo.tsx
gotjosh e4afc8d518 LDAP: Fixing sync issues (#19446)
The arching goal of this commit is to enable single user
synchronisation with LDAP. Also, it included minor fixes of style,
error messages and minor bug fixing.

The changes are:

- bug: The `multildap` package has its own errors when the user is
  not found. We fixed the conditional branch on this error by asserting
on the `multildap` errors as opposed to the `ldap` one

- bug: The previous interface usage of `RevokeAllUserTokens` did not
  work as expected. This replaces the manual injection of the service by
leveraging the service injected as part of the `server` struct.

- chore: Better error messages around not finding the user in LDAP.

- fix: Enable the single sync button and disable it when we receive an
  error from LDAP. Please note, that you can enable it by dispatching
the error. This allows you to try again without having to reload the
page.

- fix: Move the sync info to the top, then move the sync button above
  that information and clearfix to have more harmony with the UI.
2019-11-07 14:31:44 +01:00

73 lines
2.0 KiB
TypeScript

import React, { PureComponent } from 'react';
import { dateTime } from '@grafana/data';
import { LdapUserSyncInfo } from 'app/types';
interface Props {
disableSync: boolean;
syncInfo: LdapUserSyncInfo;
onSync?: () => void;
}
interface State {
isSyncing: boolean;
}
const syncTimeFormat = 'dddd YYYY-MM-DD HH:mm zz';
export class UserSyncInfo extends PureComponent<Props, State> {
state = {
isSyncing: false,
};
handleSyncClick = async () => {
const { onSync } = this.props;
this.setState({ isSyncing: true });
try {
if (onSync) {
await onSync();
}
} finally {
this.setState({ isSyncing: false });
}
};
render() {
const { syncInfo, disableSync } = this.props;
const { isSyncing } = this.state;
const nextSyncTime = syncInfo.nextSync ? dateTime(syncInfo.nextSync).format(syncTimeFormat) : '';
const prevSyncSuccessful = syncInfo && syncInfo.prevSync;
const prevSyncTime = prevSyncSuccessful ? dateTime(syncInfo.prevSync).format(syncTimeFormat) : '';
const isDisabled = isSyncing || disableSync;
return (
<>
<button className={`btn btn-secondary pull-right`} onClick={this.handleSyncClick} disabled={isDisabled}>
<span className="btn-title">Sync user</span>
{isSyncing && <i className="fa fa-spinner fa-fw fa-spin run-icon" />}
</button>
<div className="clearfix" />
<h3 className="page-heading">LDAP</h3>
<div className="gf-form-group">
<div className="gf-form">
<table className="filter-table form-inline">
<tbody>
<tr>
<td>Last synchronisation</td>
<td>{prevSyncTime}</td>
{prevSyncSuccessful && <td className="pull-right">Successful</td>}
</tr>
<tr>
<td>Next scheduled synchronisation</td>
<td colSpan={2}>{nextSyncTime}</td>
</tr>
</tbody>
</table>
</div>
</div>
</>
);
}
}