123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- import { css, cx } from '@emotion/css';
- import React, { FC, PureComponent, useRef, useState } from 'react';
- import { GrafanaTheme } from '@grafana/data';
- import { Button, ConfirmButton, ConfirmModal, Input, LegacyInputStatus, stylesFactory } from '@grafana/ui';
- import { config } from 'app/core/config';
- import { contextSrv } from 'app/core/core';
- import { AccessControlAction, UserDTO } from 'app/types';
- interface Props {
- user: UserDTO;
- onUserUpdate: (user: UserDTO) => void;
- onUserDelete: (userId: number) => void;
- onUserDisable: (userId: number) => void;
- onUserEnable: (userId: number) => void;
- onPasswordChange(password: string): void;
- }
- export function UserProfile({
- user,
- onUserUpdate,
- onUserDelete,
- onUserDisable,
- onUserEnable,
- onPasswordChange,
- }: Props) {
- const [showDeleteModal, setShowDeleteModal] = useState(false);
- const [showDisableModal, setShowDisableModal] = useState(false);
- const deleteUserRef = useRef<HTMLButtonElement | null>(null);
- const showDeleteUserModal = (show: boolean) => () => {
- setShowDeleteModal(show);
- if (!show && deleteUserRef.current) {
- deleteUserRef.current.focus();
- }
- };
- const disableUserRef = useRef<HTMLButtonElement | null>(null);
- const showDisableUserModal = (show: boolean) => () => {
- setShowDisableModal(show);
- if (!show && disableUserRef.current) {
- disableUserRef.current.focus();
- }
- };
- const handleUserDelete = () => onUserDelete(user.id);
- const handleUserDisable = () => onUserDisable(user.id);
- const handleUserEnable = () => onUserEnable(user.id);
- const onUserNameChange = (newValue: string) => {
- onUserUpdate({
- ...user,
- name: newValue,
- });
- };
- const onUserEmailChange = (newValue: string) => {
- onUserUpdate({
- ...user,
- email: newValue,
- });
- };
- const onUserLoginChange = (newValue: string) => {
- onUserUpdate({
- ...user,
- login: newValue,
- });
- };
- const authSource = user.authLabels?.length && user.authLabels[0];
- const lockMessage = authSource ? `Synced via ${authSource}` : '';
- const styles = getStyles(config.theme);
- const editLocked = user.isExternal || !contextSrv.hasPermissionInMetadata(AccessControlAction.UsersWrite, user);
- const passwordChangeLocked =
- user.isExternal || !contextSrv.hasPermissionInMetadata(AccessControlAction.UsersPasswordUpdate, user);
- const canDelete = contextSrv.hasPermissionInMetadata(AccessControlAction.UsersDelete, user);
- const canDisable = contextSrv.hasPermissionInMetadata(AccessControlAction.UsersDisable, user);
- const canEnable = contextSrv.hasPermissionInMetadata(AccessControlAction.UsersEnable, user);
- return (
- <>
- <h3 className="page-heading">User information</h3>
- <div className="gf-form-group">
- <div className="gf-form">
- <table className="filter-table form-inline">
- <tbody>
- <UserProfileRow
- label="Name"
- value={user.name}
- locked={editLocked}
- lockMessage={lockMessage}
- onChange={onUserNameChange}
- />
- <UserProfileRow
- label="Email"
- value={user.email}
- locked={editLocked}
- lockMessage={lockMessage}
- onChange={onUserEmailChange}
- />
- <UserProfileRow
- label="Username"
- value={user.login}
- locked={editLocked}
- lockMessage={lockMessage}
- onChange={onUserLoginChange}
- />
- <UserProfileRow
- label="Password"
- value="********"
- inputType="password"
- locked={passwordChangeLocked}
- lockMessage={lockMessage}
- onChange={onPasswordChange}
- />
- </tbody>
- </table>
- </div>
- <div className={styles.buttonRow}>
- {canDelete && (
- <>
- <Button variant="destructive" onClick={showDeleteUserModal(true)} ref={deleteUserRef}>
- Delete user
- </Button>
- <ConfirmModal
- isOpen={showDeleteModal}
- title="Delete user"
- body="Are you sure you want to delete this user?"
- confirmText="Delete user"
- onConfirm={handleUserDelete}
- onDismiss={showDeleteUserModal(false)}
- />
- </>
- )}
- {user.isDisabled && canEnable && (
- <Button variant="secondary" onClick={handleUserEnable}>
- Enable user
- </Button>
- )}
- {!user.isDisabled && canDisable && (
- <>
- <Button variant="secondary" onClick={showDisableUserModal(true)} ref={disableUserRef}>
- Disable user
- </Button>
- <ConfirmModal
- isOpen={showDisableModal}
- title="Disable user"
- body="Are you sure you want to disable this user?"
- confirmText="Disable user"
- onConfirm={handleUserDisable}
- onDismiss={showDisableUserModal(false)}
- />
- </>
- )}
- </div>
- </div>
- </>
- );
- }
- const getStyles = stylesFactory((theme: GrafanaTheme) => {
- return {
- buttonRow: css`
- margin-top: 0.8rem;
- > * {
- margin-right: 16px;
- }
- `,
- };
- });
- interface UserProfileRowProps {
- label: string;
- value?: string;
- locked?: boolean;
- lockMessage?: string;
- inputType?: string;
- onChange?: (value: string) => void;
- }
- interface UserProfileRowState {
- value: string;
- editing: boolean;
- }
- export class UserProfileRow extends PureComponent<UserProfileRowProps, UserProfileRowState> {
- inputElem?: HTMLInputElement;
- static defaultProps: Partial<UserProfileRowProps> = {
- value: '',
- locked: false,
- lockMessage: '',
- inputType: 'text',
- };
- state = {
- editing: false,
- value: this.props.value || '',
- };
- setInputElem = (elem: any) => {
- this.inputElem = elem;
- };
- onEditClick = () => {
- if (this.props.inputType === 'password') {
- // Reset value for password field
- this.setState({ editing: true, value: '' }, this.focusInput);
- } else {
- this.setState({ editing: true }, this.focusInput);
- }
- };
- onCancelClick = () => {
- this.setState({ editing: false, value: this.props.value || '' });
- };
- onInputChange = (event: React.ChangeEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
- if (status === LegacyInputStatus.Invalid) {
- return;
- }
- this.setState({ value: event.target.value });
- };
- onInputBlur = (event: React.FocusEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
- if (status === LegacyInputStatus.Invalid) {
- return;
- }
- this.setState({ value: event.target.value });
- };
- focusInput = () => {
- if (this.inputElem && this.inputElem.focus) {
- this.inputElem.focus();
- }
- };
- onSave = () => {
- if (this.props.onChange) {
- this.props.onChange(this.state.value);
- }
- };
- render() {
- const { label, locked, lockMessage, inputType } = this.props;
- const { value } = this.state;
- const labelClass = cx(
- 'width-16',
- css`
- font-weight: 500;
- `
- );
- if (locked) {
- return <LockedRow label={label} value={value} lockMessage={lockMessage} />;
- }
- const inputId = `${label}-input`;
- return (
- <tr>
- <td className={labelClass}>
- <label htmlFor={inputId}>{label}</label>
- </td>
- <td className="width-25" colSpan={2}>
- {this.state.editing ? (
- <Input
- id={inputId}
- type={inputType}
- defaultValue={value}
- onBlur={this.onInputBlur}
- onChange={this.onInputChange}
- ref={this.setInputElem}
- width={30}
- />
- ) : (
- <span>{this.props.value}</span>
- )}
- </td>
- <td>
- <ConfirmButton
- confirmText="Save"
- onClick={this.onEditClick}
- onConfirm={this.onSave}
- onCancel={this.onCancelClick}
- >
- Edit
- </ConfirmButton>
- </td>
- </tr>
- );
- }
- }
- interface LockedRowProps {
- label: string;
- value?: any;
- lockMessage?: string;
- }
- export const LockedRow: FC<LockedRowProps> = ({ label, value, lockMessage }) => {
- const lockMessageClass = css`
- font-style: italic;
- margin-right: 0.6rem;
- `;
- const labelClass = cx(
- 'width-16',
- css`
- font-weight: 500;
- `
- );
- return (
- <tr>
- <td className={labelClass}>{label}</td>
- <td className="width-25" colSpan={2}>
- {value}
- </td>
- <td>
- <span className={lockMessageClass}>{lockMessage}</span>
- </td>
- </tr>
- );
- };
|