mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Access control: fetch role options only if user has permissions (#44201)
* Access control: fetch role options only if user has permissions * Fix org/users page
This commit is contained in:
parent
46280848d8
commit
dc913f2311
@ -1,6 +1,7 @@
|
|||||||
import React, { FC, useState } from 'react';
|
import React, { FC, useState } from 'react';
|
||||||
import { useAsync } from 'react-use';
|
import { useAsync } from 'react-use';
|
||||||
import { Role } from 'app/types';
|
import { contextSrv } from 'app/core/core';
|
||||||
|
import { AccessControlAction, Role } from 'app/types';
|
||||||
import { RolePicker } from './RolePicker';
|
import { RolePicker } from './RolePicker';
|
||||||
import { fetchRoleOptions, fetchTeamRoles, updateTeamRoles } from './api';
|
import { fetchRoleOptions, fetchTeamRoles, updateTeamRoles } from './api';
|
||||||
|
|
||||||
@ -18,8 +19,12 @@ export const TeamRolePicker: FC<Props> = ({ teamId, orgId, getRoleOptions, disab
|
|||||||
|
|
||||||
const { loading } = useAsync(async () => {
|
const { loading } = useAsync(async () => {
|
||||||
try {
|
try {
|
||||||
let options = await (getRoleOptions ? getRoleOptions() : fetchRoleOptions(orgId));
|
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
|
||||||
setRoleOptions(options.filter((option) => !option.name?.startsWith('managed:')));
|
let options = await (getRoleOptions ? getRoleOptions() : fetchRoleOptions(orgId));
|
||||||
|
setRoleOptions(options.filter((option) => !option.name?.startsWith('managed:')));
|
||||||
|
} else {
|
||||||
|
setRoleOptions([]);
|
||||||
|
}
|
||||||
|
|
||||||
const teamRoles = await fetchTeamRoles(teamId, orgId);
|
const teamRoles = await fetchTeamRoles(teamId, orgId);
|
||||||
setAppliedRoles(teamRoles);
|
setAppliedRoles(teamRoles);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import React, { FC, useState } from 'react';
|
import React, { FC, useState } from 'react';
|
||||||
import { useAsync } from 'react-use';
|
import { useAsync } from 'react-use';
|
||||||
import { Role, OrgRole } from 'app/types';
|
import { contextSrv } from 'app/core/core';
|
||||||
|
import { Role, OrgRole, AccessControlAction } from 'app/types';
|
||||||
import { RolePicker } from './RolePicker';
|
import { RolePicker } from './RolePicker';
|
||||||
import { fetchBuiltinRoles, fetchRoleOptions, fetchUserRoles, updateUserRoles } from './api';
|
import { fetchBuiltinRoles, fetchRoleOptions, fetchUserRoles, updateUserRoles } from './api';
|
||||||
|
|
||||||
@ -31,14 +32,26 @@ export const UserRolePicker: FC<Props> = ({
|
|||||||
|
|
||||||
const { loading } = useAsync(async () => {
|
const { loading } = useAsync(async () => {
|
||||||
try {
|
try {
|
||||||
let options = await (getRoleOptions ? getRoleOptions() : fetchRoleOptions(orgId));
|
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
|
||||||
setRoleOptions(options.filter((option) => !option.name?.startsWith('managed:')));
|
let options = await (getRoleOptions ? getRoleOptions() : fetchRoleOptions(orgId));
|
||||||
|
setRoleOptions(options.filter((option) => !option.name?.startsWith('managed:')));
|
||||||
|
} else {
|
||||||
|
setRoleOptions([]);
|
||||||
|
}
|
||||||
|
|
||||||
const builtInRoles = await (getBuiltinRoles ? getBuiltinRoles() : fetchBuiltinRoles(orgId));
|
if (contextSrv.hasPermission(AccessControlAction.ActionBuiltinRolesList)) {
|
||||||
setBuiltinRoles(builtInRoles);
|
const builtInRoles = await (getBuiltinRoles ? getBuiltinRoles() : fetchBuiltinRoles(orgId));
|
||||||
|
setBuiltinRoles(builtInRoles);
|
||||||
|
} else {
|
||||||
|
setBuiltinRoles({});
|
||||||
|
}
|
||||||
|
|
||||||
const userRoles = await fetchUserRoles(userId, orgId);
|
if (contextSrv.hasPermission(AccessControlAction.ActionUserRolesList)) {
|
||||||
setAppliedRoles(userRoles);
|
const userRoles = await fetchUserRoles(userId, orgId);
|
||||||
|
setAppliedRoles(userRoles);
|
||||||
|
} else {
|
||||||
|
setAppliedRoles([]);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// TODO handle error
|
// TODO handle error
|
||||||
console.error('Error loading options');
|
console.error('Error loading options');
|
||||||
|
@ -23,10 +23,19 @@ const UsersTable: FC<Props> = (props) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchOptions() {
|
async function fetchOptions() {
|
||||||
try {
|
try {
|
||||||
let options = await fetchRoleOptions(orgId);
|
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
|
||||||
setRoleOptions(options);
|
let options = await fetchRoleOptions(orgId);
|
||||||
const builtInRoles = await fetchBuiltinRoles(orgId);
|
setRoleOptions(options);
|
||||||
setBuiltinRoles(builtInRoles);
|
} else {
|
||||||
|
setRoleOptions([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contextSrv.hasPermission(AccessControlAction.ActionBuiltinRolesList)) {
|
||||||
|
const builtInRoles = await fetchBuiltinRoles(orgId);
|
||||||
|
setBuiltinRoles(builtInRoles);
|
||||||
|
} else {
|
||||||
|
setBuiltinRoles({});
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error loading options');
|
console.error('Error loading options');
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,10 @@ export enum AccessControlAction {
|
|||||||
ActionServerStatsRead = 'server.stats:read',
|
ActionServerStatsRead = 'server.stats:read',
|
||||||
|
|
||||||
ActionTeamsCreate = 'teams:create',
|
ActionTeamsCreate = 'teams:create',
|
||||||
|
|
||||||
|
ActionRolesList = 'roles:list',
|
||||||
|
ActionBuiltinRolesList = 'roles.builtin:list',
|
||||||
|
ActionUserRolesList = 'users.roles:list',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Role {
|
export interface Role {
|
||||||
|
Loading…
Reference in New Issue
Block a user