2021-11-17 09:22:40 -06:00
|
|
|
import React, { FC, useEffect, useState } from 'react';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2020-04-15 09:49:20 -05:00
|
|
|
import { OrgRole } from '@grafana/data';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { Button, ConfirmModal } from '@grafana/ui';
|
2022-01-17 09:04:54 -06:00
|
|
|
import { UserRolePicker } from 'app/core/components/RolePicker/UserRolePicker';
|
2022-08-18 05:25:37 -05:00
|
|
|
import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { contextSrv } from 'app/core/core';
|
|
|
|
import { AccessControlAction, OrgUser, Role } from 'app/types';
|
|
|
|
|
|
|
|
import { OrgRolePicker } from '../admin/OrgRolePicker';
|
2018-10-03 02:43:10 -05:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
users: OrgUser[];
|
2021-11-19 02:50:01 -06:00
|
|
|
orgId?: number;
|
2020-04-15 09:49:20 -05:00
|
|
|
onRoleChange: (role: OrgRole, user: OrgUser) => void;
|
2018-10-03 02:43:10 -05:00
|
|
|
onRemoveUser: (user: OrgUser) => void;
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:59:48 -06:00
|
|
|
const UsersTable: FC<Props> = (props) => {
|
2021-11-19 02:50:01 -06:00
|
|
|
const { users, orgId, onRoleChange, onRemoveUser } = props;
|
2022-01-11 09:27:48 -06:00
|
|
|
const [userToRemove, setUserToRemove] = useState<OrgUser | null>(null);
|
2021-11-17 09:22:40 -06:00
|
|
|
const [roleOptions, setRoleOptions] = useState<Role[]>([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function fetchOptions() {
|
|
|
|
try {
|
2022-01-19 07:15:52 -06:00
|
|
|
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
|
|
|
|
let options = await fetchRoleOptions(orgId);
|
|
|
|
setRoleOptions(options);
|
|
|
|
}
|
2021-11-17 09:22:40 -06:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Error loading options');
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 09:27:53 -06:00
|
|
|
if (contextSrv.licensedAccessControlEnabled()) {
|
2021-11-17 09:22:40 -06:00
|
|
|
fetchOptions();
|
|
|
|
}
|
2021-11-19 02:50:01 -06:00
|
|
|
}, [orgId]);
|
2021-11-17 09:22:40 -06:00
|
|
|
|
2018-10-03 02:43:10 -05:00
|
|
|
return (
|
2022-01-11 09:27:48 -06:00
|
|
|
<>
|
|
|
|
<table className="filter-table form-inline">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th />
|
|
|
|
<th>Login</th>
|
|
|
|
<th>Email</th>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Seen</th>
|
|
|
|
<th>Role</th>
|
|
|
|
<th style={{ width: '34px' }} />
|
2022-08-15 03:58:58 -05:00
|
|
|
<th></th>
|
2022-01-11 09:27:48 -06:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{users.map((user, index) => {
|
|
|
|
return (
|
|
|
|
<tr key={`${user.userId}-${index}`}>
|
|
|
|
<td className="width-2 text-center">
|
|
|
|
<img className="filter-table__avatar" src={user.avatarUrl} alt="User avatar" />
|
|
|
|
</td>
|
|
|
|
<td className="max-width-6">
|
|
|
|
<span className="ellipsis" title={user.login}>
|
|
|
|
{user.login}
|
|
|
|
</span>
|
|
|
|
</td>
|
2020-04-15 09:49:20 -05:00
|
|
|
|
2022-01-11 09:27:48 -06:00
|
|
|
<td className="max-width-5">
|
|
|
|
<span className="ellipsis" title={user.email}>
|
|
|
|
{user.email}
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
<td className="max-width-5">
|
|
|
|
<span className="ellipsis" title={user.name}>
|
|
|
|
{user.name}
|
|
|
|
</span>
|
|
|
|
</td>
|
|
|
|
<td className="width-1">{user.lastSeenAtAge}</td>
|
2021-04-22 05:19:41 -05:00
|
|
|
|
2022-01-11 09:27:48 -06:00
|
|
|
<td className="width-8">
|
2022-02-03 09:27:53 -06:00
|
|
|
{contextSrv.licensedAccessControlEnabled() ? (
|
2022-01-11 09:27:48 -06:00
|
|
|
<UserRolePicker
|
|
|
|
userId={user.userId}
|
|
|
|
orgId={orgId}
|
2022-02-04 07:54:42 -06:00
|
|
|
roleOptions={roleOptions}
|
2022-08-18 05:25:37 -05:00
|
|
|
basicRole={user.role}
|
|
|
|
onBasicRoleChange={(newRole) => onRoleChange(newRole, user)}
|
|
|
|
basicRoleDisabled={!contextSrv.hasPermissionInMetadata(AccessControlAction.OrgUsersWrite, user)}
|
2022-01-11 09:27:48 -06:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<OrgRolePicker
|
|
|
|
aria-label="Role"
|
|
|
|
value={user.role}
|
2022-06-02 07:14:48 -05:00
|
|
|
disabled={!contextSrv.hasPermissionInMetadata(AccessControlAction.OrgUsersWrite, user)}
|
2022-01-11 09:27:48 -06:00
|
|
|
onChange={(newRole) => onRoleChange(newRole, user)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-04-22 05:19:41 -05:00
|
|
|
</td>
|
2022-01-11 09:27:48 -06:00
|
|
|
|
2022-08-15 03:58:58 -05:00
|
|
|
<td className="width-1 text-center">
|
|
|
|
{user.isDisabled && <span className="label label-tag label-tag--gray">Disabled</span>}
|
|
|
|
</td>
|
|
|
|
|
2022-01-11 09:27:48 -06:00
|
|
|
{contextSrv.hasPermissionInMetadata(AccessControlAction.OrgUsersRemove, user) && (
|
2022-08-15 03:58:58 -05:00
|
|
|
<td className="text-right">
|
2022-01-11 09:27:48 -06:00
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
variant="destructive"
|
|
|
|
onClick={() => {
|
|
|
|
setUserToRemove(user);
|
|
|
|
}}
|
|
|
|
icon="times"
|
|
|
|
aria-label="Delete user"
|
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
)}
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
{Boolean(userToRemove) && (
|
|
|
|
<ConfirmModal
|
|
|
|
body={`Are you sure you want to delete user ${userToRemove?.login}?`}
|
|
|
|
confirmText="Delete"
|
|
|
|
title="Delete"
|
|
|
|
onDismiss={() => {
|
|
|
|
setUserToRemove(null);
|
|
|
|
}}
|
|
|
|
isOpen={true}
|
|
|
|
onConfirm={() => {
|
|
|
|
if (!userToRemove) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onRemoveUser(userToRemove);
|
|
|
|
setUserToRemove(null);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2018-10-03 02:43:10 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UsersTable;
|