Access control: use delegatable flag to check if role can be granted (#42070)

* Access control: use delegatable flag to check if role can be granted or not

* Fix naming
This commit is contained in:
Alexander Zobnin 2021-11-22 17:44:03 +03:00 committed by GitHub
parent e7e5c54148
commit da2c99a2e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -64,6 +64,7 @@ type RoleDTO struct {
Description string `json:"description"`
Group string `xorm:"group_name" json:"group"`
Permissions []Permission `json:"permissions,omitempty"`
Delegatable *bool `json:"delegatable,omitempty"`
ID int64 `json:"-" xorm:"pk autoincr 'id'"`
OrgID int64 `json:"-" xorm:"org_id"`

View File

@ -194,6 +194,7 @@ export const RolePickerMenu = ({
key={i}
isSelected={groupSelected(option.value) || groupPartiallySelected(option.value)}
partiallySelected={groupPartiallySelected(option.value)}
disabled={option.options?.every(isNotDelegatable)}
onChange={onGroupChange}
onOpenSubMenu={onOpenSubMenu}
onCloseSubMenu={onCloseSubMenu}
@ -221,6 +222,7 @@ export const RolePickerMenu = ({
data={option}
key={i}
isSelected={!!(option.uid && !!selectedOptions.find((opt) => opt.uid === option.uid))}
disabled={isNotDelegatable(option)}
onChange={onChange}
hideDescription
/>
@ -237,6 +239,7 @@ export const RolePickerMenu = ({
data={option}
key={i}
isSelected={!!(option.uid && !!selectedOptions.find((opt) => opt.uid === option.uid))}
disabled={isNotDelegatable(option)}
onChange={onChange}
hideDescription
/>
@ -329,7 +332,9 @@ export const RolePickerSubMenu = ({
disabledOptions?.find((opt) => opt.uid === option.uid))
)
}
disabled={!!(option.uid && disabledOptions?.find((opt) => opt.uid === option.uid))}
disabled={
!!(option.uid && disabledOptions?.find((opt) => opt.uid === option.uid)) || isNotDelegatable(option)
}
onChange={onSelect}
hideDescription
/>
@ -507,6 +512,10 @@ const capitalize = (s: string): string => {
const sortRolesByName = (a: Role, b: Role) => a.name.localeCompare(b.name);
const isNotDelegatable = (role: Role) => {
return role.delegatable !== undefined && !role.delegatable;
};
export const getStyles = (theme: GrafanaTheme2) => {
return {
menuWrapper: css`

View File

@ -36,9 +36,9 @@ export const UserRolePicker: FC<Props> = ({
};
export const fetchRoleOptions = async (orgId?: number, query?: string): Promise<Role[]> => {
let rolesUrl = '/api/access-control/roles';
let rolesUrl = '/api/access-control/roles?delegatable=true';
if (orgId) {
rolesUrl += `?targetOrgId=${orgId}`;
rolesUrl += `&targetOrgId=${orgId}`;
}
const roles = await getBackendSrv().get(rolesUrl);
if (!roles || !roles.length) {

View File

@ -57,6 +57,7 @@ export interface Role {
description: string;
group: string;
global: boolean;
delegatable?: boolean;
version: number;
created: string;
updated: string;