2019-03-14 03:49:35 -05:00
|
|
|
import { User } from 'app/core/services/context_srv';
|
2022-11-16 08:55:10 -06:00
|
|
|
import { Team, TeamState, TeamMember, OrgRole, TeamPermissionLevel } from 'app/types';
|
2018-09-11 07:14:03 -05:00
|
|
|
|
|
|
|
export const getSearchMemberQuery = (state: TeamState) => state.searchMemberQuery;
|
|
|
|
export const getTeamGroups = (state: TeamState) => state.groups;
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
export const getTeam = (state: TeamState, currentTeamId: any): Team | null => {
|
2018-09-11 07:14:03 -05:00
|
|
|
if (state.team.id === parseInt(currentTeamId, 10)) {
|
|
|
|
return state.team;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getTeamMembers = (state: TeamState) => {
|
|
|
|
const regex = RegExp(state.searchMemberQuery, 'i');
|
|
|
|
|
2021-01-20 00:59:48 -06:00
|
|
|
return state.members.filter((member) => {
|
2019-11-21 04:44:46 -06:00
|
|
|
return regex.test(member.login) || regex.test(member.email) || regex.test(member.name);
|
2018-09-11 07:14:03 -05:00
|
|
|
});
|
|
|
|
};
|
2019-03-14 03:49:35 -05:00
|
|
|
|
|
|
|
export interface Config {
|
|
|
|
members: TeamMember[];
|
|
|
|
editorsCanAdmin: boolean;
|
|
|
|
signedInUser: User;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isSignedInUserTeamAdmin = (config: Config): boolean => {
|
2019-03-14 08:24:13 -05:00
|
|
|
const { members, signedInUser, editorsCanAdmin } = config;
|
2021-01-20 00:59:48 -06:00
|
|
|
const userInMembers = members.find((m) => m.userId === signedInUser.id);
|
2019-03-14 08:24:13 -05:00
|
|
|
const permission = userInMembers ? userInMembers.permission : TeamPermissionLevel.Member;
|
|
|
|
|
|
|
|
return isPermissionTeamAdmin({ permission, signedInUser, editorsCanAdmin });
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface PermissionConfig {
|
|
|
|
permission: TeamPermissionLevel;
|
|
|
|
editorsCanAdmin: boolean;
|
|
|
|
signedInUser: User;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isPermissionTeamAdmin = (config: PermissionConfig): boolean => {
|
|
|
|
const { permission, signedInUser, editorsCanAdmin } = config;
|
|
|
|
const isAdmin = signedInUser.isGrafanaAdmin || signedInUser.orgRole === OrgRole.Admin;
|
|
|
|
const userIsTeamAdmin = permission === TeamPermissionLevel.Admin;
|
2019-03-14 03:49:35 -05:00
|
|
|
const isSignedInUserTeamAdmin = isAdmin || userIsTeamAdmin;
|
|
|
|
|
2019-03-14 08:24:13 -05:00
|
|
|
return isSignedInUserTeamAdmin || !editorsCanAdmin;
|
2019-03-14 03:49:35 -05:00
|
|
|
};
|