selectors.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { User } from 'app/core/services/context_srv';
  2. import { Team, TeamsState, TeamState, TeamMember, OrgRole, TeamPermissionLevel } from 'app/types';
  3. export const getSearchQuery = (state: TeamsState) => state.searchQuery;
  4. export const getSearchMemberQuery = (state: TeamState) => state.searchMemberQuery;
  5. export const getTeamGroups = (state: TeamState) => state.groups;
  6. export const getTeamsCount = (state: TeamsState) => state.teams.length;
  7. export const getTeamsSearchPage = (state: TeamsState) => state.searchPage;
  8. export const getTeam = (state: TeamState, currentTeamId: any): Team | null => {
  9. if (state.team.id === parseInt(currentTeamId, 10)) {
  10. return state.team;
  11. }
  12. return null;
  13. };
  14. export const getTeams = (state: TeamsState) => {
  15. const regex = RegExp(state.searchQuery, 'i');
  16. return state.teams.filter((team) => {
  17. return regex.test(team.name);
  18. });
  19. };
  20. export const getTeamMembers = (state: TeamState) => {
  21. const regex = RegExp(state.searchMemberQuery, 'i');
  22. return state.members.filter((member) => {
  23. return regex.test(member.login) || regex.test(member.email) || regex.test(member.name);
  24. });
  25. };
  26. export interface Config {
  27. members: TeamMember[];
  28. editorsCanAdmin: boolean;
  29. signedInUser: User;
  30. }
  31. export const isSignedInUserTeamAdmin = (config: Config): boolean => {
  32. const { members, signedInUser, editorsCanAdmin } = config;
  33. const userInMembers = members.find((m) => m.userId === signedInUser.id);
  34. const permission = userInMembers ? userInMembers.permission : TeamPermissionLevel.Member;
  35. return isPermissionTeamAdmin({ permission, signedInUser, editorsCanAdmin });
  36. };
  37. export interface PermissionConfig {
  38. permission: TeamPermissionLevel;
  39. editorsCanAdmin: boolean;
  40. signedInUser: User;
  41. }
  42. export const isPermissionTeamAdmin = (config: PermissionConfig): boolean => {
  43. const { permission, signedInUser, editorsCanAdmin } = config;
  44. const isAdmin = signedInUser.isGrafanaAdmin || signedInUser.orgRole === OrgRole.Admin;
  45. const userIsTeamAdmin = permission === TeamPermissionLevel.Admin;
  46. const isSignedInUserTeamAdmin = isAdmin || userIsTeamAdmin;
  47. return isSignedInUserTeamAdmin || !editorsCanAdmin;
  48. };