mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
* remove rbacBuiltInRoleAssignmentEnabled from frontendsettings * RBAC: Remove RBACBuiltInRoleAssignmentEnabled * RBAC: Remove code for builtin role * RolePicker: Remove unused prop * RolePicker: Rename builtinRole to basicRole * RolePicker: Rename onBuiltinRoleChange to onBasicRoleChange * RolePicker: Rename properties
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import React, { FC, useEffect } from 'react';
|
|
import { useAsyncFn } from 'react-use';
|
|
|
|
import { contextSrv } from 'app/core/core';
|
|
import { Role, OrgRole, AccessControlAction } from 'app/types';
|
|
|
|
import { RolePicker } from './RolePicker';
|
|
import { fetchUserRoles, updateUserRoles } from './api';
|
|
|
|
export interface Props {
|
|
basicRole: OrgRole;
|
|
userId: number;
|
|
orgId?: number;
|
|
onBasicRoleChange: (newRole: OrgRole) => void;
|
|
roleOptions: Role[];
|
|
disabled?: boolean;
|
|
basicRoleDisabled?: boolean;
|
|
apply?: boolean;
|
|
onApplyRoles?: (newRoles: Role[], userId: number, orgId: number | undefined) => void;
|
|
pendingRoles?: Role[];
|
|
}
|
|
|
|
export const UserRolePicker: FC<Props> = ({
|
|
basicRole,
|
|
userId,
|
|
orgId,
|
|
onBasicRoleChange,
|
|
roleOptions,
|
|
disabled,
|
|
basicRoleDisabled,
|
|
apply = false,
|
|
onApplyRoles,
|
|
pendingRoles,
|
|
}) => {
|
|
const [{ loading, value: appliedRoles = [] }, getUserRoles] = useAsyncFn(async () => {
|
|
try {
|
|
if (apply) {
|
|
if (pendingRoles?.length! > 0) {
|
|
return pendingRoles;
|
|
}
|
|
}
|
|
if (contextSrv.hasPermission(AccessControlAction.ActionUserRolesList)) {
|
|
return await fetchUserRoles(userId, orgId);
|
|
}
|
|
} catch (e) {
|
|
// TODO handle error
|
|
console.error('Error loading options');
|
|
}
|
|
return [];
|
|
}, [orgId, userId, pendingRoles]);
|
|
|
|
useEffect(() => {
|
|
// only load roles when there is an Org selected
|
|
if (orgId) {
|
|
getUserRoles();
|
|
}
|
|
}, [orgId, getUserRoles, pendingRoles]);
|
|
|
|
const onRolesChange = async (roles: Role[]) => {
|
|
if (!apply) {
|
|
await updateUserRoles(roles, userId, orgId);
|
|
await getUserRoles();
|
|
} else if (onApplyRoles) {
|
|
onApplyRoles(roles, userId, orgId);
|
|
}
|
|
};
|
|
|
|
const canUpdateRoles =
|
|
contextSrv.hasPermission(AccessControlAction.ActionUserRolesAdd) &&
|
|
contextSrv.hasPermission(AccessControlAction.ActionUserRolesRemove);
|
|
|
|
return (
|
|
<RolePicker
|
|
appliedRoles={appliedRoles}
|
|
basicRole={basicRole}
|
|
onRolesChange={onRolesChange}
|
|
onBasicRoleChange={onBasicRoleChange}
|
|
roleOptions={roleOptions}
|
|
isLoading={loading}
|
|
disabled={disabled}
|
|
basicRoleDisabled={basicRoleDisabled}
|
|
showBasicRole
|
|
apply={apply}
|
|
canUpdateRoles={canUpdateRoles}
|
|
/>
|
|
);
|
|
};
|