UserAdminPage.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Page from 'app/core/components/Page/Page';
  6. import { contextSrv } from 'app/core/core';
  7. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  8. import { getNavModel } from 'app/core/selectors/navModel';
  9. import { StoreState, UserDTO, UserOrg, UserSession, SyncInfo, UserAdminError, AccessControlAction } from 'app/types';
  10. import { UserLdapSyncInfo } from './UserLdapSyncInfo';
  11. import { UserOrgs } from './UserOrgs';
  12. import { UserPermissions } from './UserPermissions';
  13. import { UserProfile } from './UserProfile';
  14. import { UserSessions } from './UserSessions';
  15. import {
  16. loadAdminUserPage,
  17. revokeSession,
  18. revokeAllSessions,
  19. updateUser,
  20. setUserPassword,
  21. disableUser,
  22. enableUser,
  23. deleteUser,
  24. updateUserPermissions,
  25. addOrgUser,
  26. updateOrgUserRole,
  27. deleteOrgUser,
  28. syncLdapUser,
  29. } from './state/actions';
  30. interface OwnProps extends GrafanaRouteComponentProps<{ id: string }> {
  31. navModel: NavModel;
  32. user?: UserDTO;
  33. orgs: UserOrg[];
  34. sessions: UserSession[];
  35. ldapSyncInfo?: SyncInfo;
  36. isLoading: boolean;
  37. error?: UserAdminError;
  38. }
  39. export class UserAdminPage extends PureComponent<Props> {
  40. async componentDidMount() {
  41. const { match, loadAdminUserPage } = this.props;
  42. loadAdminUserPage(parseInt(match.params.id, 10));
  43. }
  44. onUserUpdate = (user: UserDTO) => {
  45. this.props.updateUser(user);
  46. };
  47. onPasswordChange = (password: string) => {
  48. const { user, setUserPassword } = this.props;
  49. user && setUserPassword(user.id, password);
  50. };
  51. onUserDelete = (userId: number) => {
  52. this.props.deleteUser(userId);
  53. };
  54. onUserDisable = (userId: number) => {
  55. this.props.disableUser(userId);
  56. };
  57. onUserEnable = (userId: number) => {
  58. this.props.enableUser(userId);
  59. };
  60. onGrafanaAdminChange = (isGrafanaAdmin: boolean) => {
  61. const { user, updateUserPermissions } = this.props;
  62. user && updateUserPermissions(user.id, isGrafanaAdmin);
  63. };
  64. onOrgRemove = (orgId: number) => {
  65. const { user, deleteOrgUser } = this.props;
  66. user && deleteOrgUser(user.id, orgId);
  67. };
  68. onOrgRoleChange = (orgId: number, newRole: string) => {
  69. const { user, updateOrgUserRole } = this.props;
  70. user && updateOrgUserRole(user.id, orgId, newRole);
  71. };
  72. onOrgAdd = (orgId: number, role: string) => {
  73. const { user, addOrgUser } = this.props;
  74. user && addOrgUser(user, orgId, role);
  75. };
  76. onSessionRevoke = (tokenId: number) => {
  77. const { user, revokeSession } = this.props;
  78. user && revokeSession(tokenId, user.id);
  79. };
  80. onAllSessionsRevoke = () => {
  81. const { user, revokeAllSessions } = this.props;
  82. user && revokeAllSessions(user.id);
  83. };
  84. onUserSync = () => {
  85. const { user, syncLdapUser } = this.props;
  86. user && syncLdapUser(user.id);
  87. };
  88. render() {
  89. const { navModel, user, orgs, sessions, ldapSyncInfo, isLoading } = this.props;
  90. const isLDAPUser = user && user.isExternal && user.authLabels && user.authLabels.includes('LDAP');
  91. const canReadSessions = contextSrv.hasPermission(AccessControlAction.UsersAuthTokenList);
  92. const canReadLDAPStatus = contextSrv.hasPermission(AccessControlAction.LDAPStatusRead);
  93. return (
  94. <Page navModel={navModel}>
  95. <Page.Contents isLoading={isLoading}>
  96. {user && (
  97. <>
  98. <UserProfile
  99. user={user}
  100. onUserUpdate={this.onUserUpdate}
  101. onUserDelete={this.onUserDelete}
  102. onUserDisable={this.onUserDisable}
  103. onUserEnable={this.onUserEnable}
  104. onPasswordChange={this.onPasswordChange}
  105. />
  106. {isLDAPUser && featureEnabled('ldapsync') && ldapSyncInfo && canReadLDAPStatus && (
  107. <UserLdapSyncInfo ldapSyncInfo={ldapSyncInfo} user={user} onUserSync={this.onUserSync} />
  108. )}
  109. <UserPermissions isGrafanaAdmin={user.isGrafanaAdmin} onGrafanaAdminChange={this.onGrafanaAdminChange} />
  110. </>
  111. )}
  112. {orgs && (
  113. <UserOrgs
  114. user={user}
  115. orgs={orgs}
  116. isExternalUser={user?.isExternal}
  117. onOrgRemove={this.onOrgRemove}
  118. onOrgRoleChange={this.onOrgRoleChange}
  119. onOrgAdd={this.onOrgAdd}
  120. />
  121. )}
  122. {sessions && canReadSessions && (
  123. <UserSessions
  124. sessions={sessions}
  125. onSessionRevoke={this.onSessionRevoke}
  126. onAllSessionsRevoke={this.onAllSessionsRevoke}
  127. />
  128. )}
  129. </Page.Contents>
  130. </Page>
  131. );
  132. }
  133. }
  134. const mapStateToProps = (state: StoreState) => ({
  135. navModel: getNavModel(state.navIndex, 'global-users'),
  136. user: state.userAdmin.user,
  137. sessions: state.userAdmin.sessions,
  138. orgs: state.userAdmin.orgs,
  139. ldapSyncInfo: state.ldap.syncInfo,
  140. isLoading: state.userAdmin.isLoading,
  141. error: state.userAdmin.error,
  142. });
  143. const mapDispatchToProps = {
  144. loadAdminUserPage,
  145. updateUser,
  146. setUserPassword,
  147. disableUser,
  148. enableUser,
  149. deleteUser,
  150. updateUserPermissions,
  151. addOrgUser,
  152. updateOrgUserRole,
  153. deleteOrgUser,
  154. revokeSession,
  155. revokeAllSessions,
  156. syncLdapUser,
  157. };
  158. const connector = connect(mapStateToProps, mapDispatchToProps);
  159. type Props = OwnProps & ConnectedProps<typeof connector>;
  160. export default connector(UserAdminPage);