2022-06-15 15:59:29 +08:00
|
|
|
import { getBackendSrv, isFetchError } from '@grafana/runtime';
|
2022-01-17 23:04:54 +08:00
|
|
|
import { Role } from 'app/types';
|
|
|
|
|
2025-04-08 22:15:03 +08:00
|
|
|
import { addDisplayNameForFixedRole, addFilteredDisplayName } from './utils';
|
2023-02-03 18:39:44 +08:00
|
|
|
|
2023-12-18 19:10:26 +08:00
|
|
|
export const fetchRoleOptions = async (orgId?: number): Promise<Role[]> => {
|
2022-01-17 23:04:54 +08:00
|
|
|
let rolesUrl = '/api/access-control/roles?delegatable=true';
|
|
|
|
if (orgId) {
|
|
|
|
rolesUrl += `&targetOrgId=${orgId}`;
|
|
|
|
}
|
|
|
|
const roles = await getBackendSrv().get(rolesUrl);
|
|
|
|
if (!roles || !roles.length) {
|
|
|
|
return [];
|
|
|
|
}
|
2025-04-08 22:15:03 +08:00
|
|
|
return roles.map(addDisplayNameForFixedRole).map(addFilteredDisplayName);
|
2022-01-17 23:04:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchUserRoles = async (userId: number, orgId?: number): Promise<Role[]> => {
|
2024-11-21 01:37:12 +08:00
|
|
|
let userRolesUrl = `/api/access-control/users/${userId}/roles?includeMapped=true`;
|
2022-01-17 23:04:54 +08:00
|
|
|
if (orgId) {
|
2024-11-21 01:37:12 +08:00
|
|
|
userRolesUrl += `&targetOrgId=${orgId}`;
|
2022-01-17 23:04:54 +08:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
const roles = await getBackendSrv().get(userRolesUrl);
|
|
|
|
if (!roles || !roles.length) {
|
|
|
|
return [];
|
|
|
|
}
|
2025-04-08 22:15:03 +08:00
|
|
|
return roles.map(addDisplayNameForFixedRole).map(addFilteredDisplayName);
|
2022-01-17 23:04:54 +08:00
|
|
|
} catch (error) {
|
2022-06-15 15:59:29 +08:00
|
|
|
if (isFetchError(error)) {
|
|
|
|
error.isHandled = true;
|
|
|
|
}
|
2022-01-17 23:04:54 +08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-23 22:13:55 +08:00
|
|
|
export const updateUserRoles = (roles: Role[], userId: number, orgId?: number) => {
|
2022-01-17 23:04:54 +08:00
|
|
|
let userRolesUrl = `/api/access-control/users/${userId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
userRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
2024-11-21 01:37:12 +08:00
|
|
|
const filteredRoles = roles.filter((role) => !role.mapped);
|
|
|
|
const roleUids = filteredRoles.flatMap((x) => x.uid);
|
2022-01-17 23:04:54 +08: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 [];
|
|
|
|
}
|
2025-04-08 22:15:03 +08:00
|
|
|
return roles.map(addDisplayNameForFixedRole).map(addFilteredDisplayName);
|
2022-01-17 23:04:54 +08:00
|
|
|
} catch (error) {
|
2022-06-15 15:59:29 +08:00
|
|
|
if (isFetchError(error)) {
|
|
|
|
error.isHandled = true;
|
|
|
|
}
|
2022-01-17 23:04:54 +08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-23 22:13:55 +08:00
|
|
|
export const updateTeamRoles = (roles: Role[], teamId: number, orgId?: number) => {
|
2022-01-17 23:04:54 +08:00
|
|
|
let teamRolesUrl = `/api/access-control/teams/${teamId}/roles`;
|
|
|
|
if (orgId) {
|
|
|
|
teamRolesUrl += `?targetOrgId=${orgId}`;
|
|
|
|
}
|
2022-05-23 22:13:55 +08:00
|
|
|
const roleUids = roles.flatMap((x) => x.uid);
|
|
|
|
|
2022-01-17 23:04:54 +08:00
|
|
|
return getBackendSrv().put(teamRolesUrl, {
|
|
|
|
orgId,
|
|
|
|
roleUids,
|
|
|
|
});
|
|
|
|
};
|