2022-06-15 02:59:29 -05:00
|
|
|
import { getBackendSrv, isFetchError } from '@grafana/runtime';
|
2022-01-17 09:04:54 -06:00
|
|
|
import { Role } from 'app/types';
|
|
|
|
|
2023-02-03 04:39:44 -06:00
|
|
|
import { addDisplayNameForFixedRole } from './utils';
|
|
|
|
|
2022-01-17 09:04:54 -06:00
|
|
|
export const fetchRoleOptions = async (orgId?: number, query?: string): Promise<Role[]> => {
|
|
|
|
let rolesUrl = '/api/access-control/roles?delegatable=true';
|
|
|
|
if (orgId) {
|
|
|
|
rolesUrl += `&targetOrgId=${orgId}`;
|
|
|
|
}
|
|
|
|
const roles = await getBackendSrv().get(rolesUrl);
|
|
|
|
if (!roles || !roles.length) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-03 04:39:44 -06:00
|
|
|
return roles.map(addDisplayNameForFixedRole);
|
2022-01-17 09:04:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchUserRoles = async (userId: number, orgId?: number): Promise<Role[]> => {
|
|
|
|
let userRolesUrl = `/api/access-control/users/${userId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
userRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const roles = await getBackendSrv().get(userRolesUrl);
|
|
|
|
if (!roles || !roles.length) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-03 04:39:44 -06:00
|
|
|
return roles.map(addDisplayNameForFixedRole);
|
2022-01-17 09:04:54 -06:00
|
|
|
} catch (error) {
|
2022-06-15 02:59:29 -05:00
|
|
|
if (isFetchError(error)) {
|
|
|
|
error.isHandled = true;
|
|
|
|
}
|
2022-01-17 09:04:54 -06:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-23 09:13:55 -05:00
|
|
|
export const updateUserRoles = (roles: Role[], userId: number, orgId?: number) => {
|
2022-01-17 09:04:54 -06:00
|
|
|
let userRolesUrl = `/api/access-control/users/${userId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
userRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
2022-05-23 09:13:55 -05:00
|
|
|
const roleUids = roles.flatMap((x) => x.uid);
|
2022-01-17 09:04:54 -06:00
|
|
|
return getBackendSrv().put(userRolesUrl, {
|
|
|
|
orgId,
|
|
|
|
roleUids,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchTeamRoles = async (teamId: number, orgId?: number): Promise<Role[]> => {
|
|
|
|
let teamRolesUrl = `/api/access-control/teams/${teamId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
teamRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const roles = await getBackendSrv().get(teamRolesUrl);
|
|
|
|
if (!roles || !roles.length) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-03 04:39:44 -06:00
|
|
|
return roles.map(addDisplayNameForFixedRole);
|
2022-01-17 09:04:54 -06:00
|
|
|
} catch (error) {
|
2022-06-15 02:59:29 -05:00
|
|
|
if (isFetchError(error)) {
|
|
|
|
error.isHandled = true;
|
|
|
|
}
|
2022-01-17 09:04:54 -06:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-23 09:13:55 -05:00
|
|
|
export const updateTeamRoles = (roles: Role[], teamId: number, orgId?: number) => {
|
2022-01-17 09:04:54 -06:00
|
|
|
let teamRolesUrl = `/api/access-control/teams/${teamId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
teamRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
2022-05-23 09:13:55 -05:00
|
|
|
const roleUids = roles.flatMap((x) => x.uid);
|
|
|
|
|
2022-01-17 09:04:54 -06:00
|
|
|
return getBackendSrv().put(teamRolesUrl, {
|
|
|
|
orgId,
|
|
|
|
roleUids,
|
|
|
|
});
|
|
|
|
};
|