mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactor: move displayname logic from backend to frontend (#62845)
* remove fallback from backend * add: displayname logic to frontend * Update public/app/core/components/RolePicker/utils.ts Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> * add: fetchTeamRoles and return earlier * refactor: change to const --------- Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>
This commit is contained in:
@@ -60,7 +60,7 @@ export const RolePickerInput = ({
|
||||
<div className={styles.wrapper}>
|
||||
{showBasicRole && <ValueContainer>{basicRole}</ValueContainer>}
|
||||
{appliedRoles.map((role) => (
|
||||
<ValueContainer key={role.uid}>{role.displayName}</ValueContainer>
|
||||
<ValueContainer key={role.uid}>{role.displayName || role.name}</ValueContainer>
|
||||
))}
|
||||
|
||||
{!disabled && (
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { getBackendSrv, isFetchError } from '@grafana/runtime';
|
||||
import { Role } from 'app/types';
|
||||
|
||||
import { addDisplayNameForFixedRole } from './utils';
|
||||
|
||||
export const fetchRoleOptions = async (orgId?: number, query?: string): Promise<Role[]> => {
|
||||
let rolesUrl = '/api/access-control/roles?delegatable=true';
|
||||
if (orgId) {
|
||||
@@ -10,7 +12,7 @@ export const fetchRoleOptions = async (orgId?: number, query?: string): Promise<
|
||||
if (!roles || !roles.length) {
|
||||
return [];
|
||||
}
|
||||
return roles;
|
||||
return roles.map(addDisplayNameForFixedRole);
|
||||
};
|
||||
|
||||
export const fetchUserRoles = async (userId: number, orgId?: number): Promise<Role[]> => {
|
||||
@@ -23,7 +25,7 @@ export const fetchUserRoles = async (userId: number, orgId?: number): Promise<Ro
|
||||
if (!roles || !roles.length) {
|
||||
return [];
|
||||
}
|
||||
return roles;
|
||||
return roles.map(addDisplayNameForFixedRole);
|
||||
} catch (error) {
|
||||
if (isFetchError(error)) {
|
||||
error.isHandled = true;
|
||||
@@ -54,7 +56,7 @@ export const fetchTeamRoles = async (teamId: number, orgId?: number): Promise<Ro
|
||||
if (!roles || !roles.length) {
|
||||
return [];
|
||||
}
|
||||
return roles;
|
||||
return roles.map(addDisplayNameForFixedRole);
|
||||
} catch (error) {
|
||||
if (isFetchError(error)) {
|
||||
error.isHandled = true;
|
||||
|
||||
@@ -3,3 +3,19 @@ import { Role } from 'app/types';
|
||||
export const isNotDelegatable = (role: Role) => {
|
||||
return role.delegatable !== undefined && !role.delegatable;
|
||||
};
|
||||
|
||||
// addDisplayNameForFixedRole provides a fallback name for fixed roles
|
||||
// this is "incase" a fixed role is introduced but without a displayname set
|
||||
// example: currently this would give:
|
||||
// fixed:datasources:name -> datasources name
|
||||
// fixed:datasources:admin -> datasources admin
|
||||
export const addDisplayNameForFixedRole = (role: Role) => {
|
||||
const fixedRolePrefix = 'fixed:';
|
||||
if (!role.displayName && role.name.startsWith(fixedRolePrefix)) {
|
||||
let newRoleName = '';
|
||||
let rNameWithoutFixedPrefix = role.name.replace(fixedRolePrefix, '');
|
||||
newRoleName = rNameWithoutFixedPrefix.replace(/:/g, ' ');
|
||||
role.displayName = newRoleName;
|
||||
}
|
||||
return role;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user