2022-06-01 10:35:16 +03:00
|
|
|
import { css } from '@emotion/css';
|
2024-06-25 12:43:47 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
2022-06-01 10:35:16 +03:00
|
|
|
|
|
|
|
|
import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data';
|
2023-11-14 17:52:48 +01:00
|
|
|
import { Label, TextLink, useStyles2 } from '@grafana/ui';
|
2023-04-04 16:27:40 +01:00
|
|
|
import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
|
2022-06-01 10:35:16 +03:00
|
|
|
import { contextSrv } from 'app/core/core';
|
|
|
|
|
import { AccessControlAction, Role, ServiceAccountDTO } from 'app/types';
|
|
|
|
|
|
|
|
|
|
import { ServiceAccountProfileRow } from './ServiceAccountProfileRow';
|
|
|
|
|
import { ServiceAccountRoleRow } from './ServiceAccountRoleRow';
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
serviceAccount: ServiceAccountDTO;
|
|
|
|
|
timeZone: TimeZone;
|
|
|
|
|
onChange: (serviceAccount: ServiceAccountDTO) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 16:27:40 +01:00
|
|
|
export function ServiceAccountProfile({ serviceAccount, timeZone, onChange }: Props): JSX.Element {
|
2022-06-01 10:35:16 +03:00
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
|
const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite);
|
2024-06-25 12:43:47 +01:00
|
|
|
const [roles, setRoleOptions] = useState<Role[]>([]);
|
2022-06-01 10:35:16 +03:00
|
|
|
|
|
|
|
|
const onRoleChange = (role: OrgRole) => {
|
|
|
|
|
onChange({ ...serviceAccount, role: role });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onNameChange = (newValue: string) => {
|
|
|
|
|
onChange({ ...serviceAccount, name: newValue });
|
|
|
|
|
};
|
2023-04-12 11:07:06 +01:00
|
|
|
|
2024-06-25 12:43:47 +01:00
|
|
|
useEffect(() => {
|
2023-04-12 11:07:06 +01:00
|
|
|
async function fetchOptions() {
|
|
|
|
|
try {
|
|
|
|
|
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
|
|
|
|
|
let options = await fetchRoleOptions(serviceAccount.orgId);
|
|
|
|
|
setRoleOptions(options);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Error loading options for service account');
|
2023-04-04 16:27:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-12 11:07:06 +01:00
|
|
|
if (contextSrv.licensedAccessControlEnabled()) {
|
|
|
|
|
fetchOptions();
|
|
|
|
|
}
|
2023-04-04 16:27:40 +01:00
|
|
|
}, [serviceAccount.orgId]);
|
2022-06-01 10:35:16 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.section}>
|
2022-07-08 10:53:18 +01:00
|
|
|
<h3>Information</h3>
|
2022-06-01 10:35:16 +03:00
|
|
|
<table className="filter-table">
|
|
|
|
|
<tbody>
|
2024-10-31 15:05:18 +01:00
|
|
|
<ServiceAccountProfileRow label="Numerical identifier" value={serviceAccount.id.toString()} disabled={true} />
|
2022-06-01 10:35:16 +03:00
|
|
|
<ServiceAccountProfileRow
|
|
|
|
|
label="Name"
|
|
|
|
|
value={serviceAccount.name}
|
2023-11-14 17:52:48 +01:00
|
|
|
onChange={!serviceAccount.isExternal ? onNameChange : undefined}
|
2022-06-01 10:35:16 +03:00
|
|
|
disabled={!ableToWrite || serviceAccount.isDisabled}
|
|
|
|
|
/>
|
|
|
|
|
<ServiceAccountProfileRow label="ID" value={serviceAccount.login} disabled={serviceAccount.isDisabled} />
|
|
|
|
|
<ServiceAccountRoleRow
|
|
|
|
|
label="Roles"
|
|
|
|
|
serviceAccount={serviceAccount}
|
|
|
|
|
onRoleChange={onRoleChange}
|
2023-04-04 16:27:40 +01:00
|
|
|
roleOptions={roles}
|
2022-06-01 10:35:16 +03:00
|
|
|
/>
|
|
|
|
|
<ServiceAccountProfileRow
|
|
|
|
|
label="Creation date"
|
|
|
|
|
value={dateTimeFormat(serviceAccount.createdAt, { timeZone })}
|
|
|
|
|
disabled={serviceAccount.isDisabled}
|
|
|
|
|
/>
|
2023-11-14 17:52:48 +01:00
|
|
|
{serviceAccount.isExternal && serviceAccount.requiredBy && (
|
|
|
|
|
<tr>
|
|
|
|
|
<td>
|
|
|
|
|
<Label>Used by</Label>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
<TextLink href={`/plugins/${serviceAccount.requiredBy}`}>{serviceAccount.requiredBy}</TextLink>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
2022-06-01 10:35:16 +03:00
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getStyles = (theme: GrafanaTheme2) => ({
|
2024-10-25 14:50:28 +01:00
|
|
|
section: css({
|
|
|
|
|
marginBottom: theme.spacing(4),
|
|
|
|
|
}),
|
2022-06-01 10:35:16 +03:00
|
|
|
});
|