fleet/frontend/utilities/permissions/permissions.ts
Jacob Shandling 376be83062
UI – Add support for Windows powershell scripts (#15128)
## Addresses #14752
### (see issue for detailed list of features implemented)

![script list
alpha](https://github.com/fleetdm/fleet/assets/61553566/1b35da72-5ff9-47e2-9d4b-0e0334e2c2b8)


![details-scripts-mac](https://github.com/fleetdm/fleet/assets/61553566/6ccad298-d4bd-47fa-bd0b-193f87b68881)

![details-scripts-windows](https://github.com/fleetdm/fleet/assets/61553566/208bb2c4-eaf8-45c4-8a9b-dfd7590f2117)

![error](https://github.com/fleetdm/fleet/assets/61553566/c0f1ad90-345b-4356-922a-ad76da96db0e)

- Also addresses #15140:

![fixed-dropdown-table-issue](https://github.com/fleetdm/fleet/assets/61553566/6a0d951d-156a-4d86-a1ab-9b00cd731e94)
- Align host details > scripts > Status cells' icon alignments (see
misaligned "pending" icon above):
![Screenshot 2023-11-14 at 4 08
01 PM](https://github.com/fleetdm/fleet/assets/61553566/a354d8c4-f56a-4cf0-8d58-1fc0ad662180)

## Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-11-15 11:28:57 -08:00

162 lines
4.1 KiB
TypeScript

import { IUser } from "interfaces/user";
import { IConfig } from "interfaces/config";
export const isSandboxMode = (config: IConfig): boolean => {
return !!config.sandbox_enabled; // TODO: confirm null/undefined config should treated as false based on final API spec
};
export const isFreeTier = (config: IConfig): boolean => {
return config.license.tier === "free";
};
export const isPremiumTier = (config: IConfig): boolean => {
return config.license.tier === "premium";
};
export const isMdmEnabledAndConfigured = (config: IConfig): boolean => {
return Boolean(config.mdm.enabled_and_configured);
};
export const isGlobalAdmin = (user: IUser): boolean => {
return user.global_role === "admin";
};
export const isGlobalMaintainer = (user: IUser): boolean => {
return user.global_role === "maintainer";
};
export const isGlobalObserver = (user: IUser): boolean => {
return (
user.global_role === "observer" || user.global_role === "observer_plus"
);
};
export const isOnGlobalTeam = (user: IUser): boolean => {
return user.global_role !== null;
};
// This checks against a specific team
export const isTeamObserver = (
user: IUser | null,
teamId: number | null
): boolean => {
const userTeamRole = user?.teams.find((team) => team.id === teamId)?.role;
return userTeamRole === "observer" || userTeamRole === "observer_plus";
};
export const isTeamMaintainer = (
user: IUser | null,
teamId: number | null
): boolean => {
const userTeamRole = user?.teams.find((team) => team.id === teamId)?.role;
return userTeamRole === "maintainer";
};
export const isTeamAdmin = (
user: IUser | null,
teamId: number | null
): boolean => {
const userTeamRole = user?.teams.find((team) => team.id === teamId)?.role;
return userTeamRole === "admin";
};
const isTeamMaintainerOrTeamAdmin = (
user: IUser | null,
teamId: number | null
): boolean => {
const userTeamRole = user?.teams.find((team) => team.id === teamId)?.role;
return userTeamRole === "admin" || userTeamRole === "maintainer";
};
// This checks against all teams
const isAnyTeamObserverPlus = (user: IUser): boolean => {
if (!isOnGlobalTeam(user)) {
return user.teams.some((team) => team?.role === "observer_plus");
}
return false;
};
const isAnyTeamMaintainer = (user: IUser): boolean => {
if (!isOnGlobalTeam(user)) {
return user.teams.some((team) => team?.role === "maintainer");
}
return false;
};
const isAnyTeamAdmin = (user: IUser): boolean => {
if (!isOnGlobalTeam(user)) {
return user.teams.some((team) => team?.role === "admin");
}
return false;
};
const isAnyTeamMaintainerOrTeamAdmin = (user: IUser): boolean => {
if (!isOnGlobalTeam(user)) {
return user.teams.some(
(team) => team?.role === "maintainer" || team?.role === "admin"
);
}
return false;
};
const isOnlyObserver = (user: IUser): boolean => {
if (isGlobalObserver(user)) {
return true;
}
// Return false if any role is team maintainer or team admin
if (!isOnGlobalTeam(user)) {
return !user.teams.some(
(team) => team?.role === "maintainer" || team?.role === "admin"
);
}
return false;
};
export const isGlobalObserverPlus = (user: IUser): boolean => {
return user.global_role === "observer_plus";
};
export const isTeamObserverPlus = (
user: IUser | null,
teamId: number | null
): boolean => {
const userTeamRole = user?.teams.find((team) => team.id === teamId)?.role;
return userTeamRole === "observer_plus";
};
export const isObserverPlus = (user: IUser, teamId: number | null): boolean => {
return isGlobalObserverPlus(user) || isTeamObserverPlus(user, teamId);
};
const isNoAccess = (user: IUser): boolean => {
return user.global_role === null && user.teams.length === 0;
};
export default {
isSandboxMode,
isFreeTier,
isPremiumTier,
isMdmEnabledAndConfigured,
isGlobalAdmin,
isGlobalMaintainer,
isGlobalObserver,
isOnGlobalTeam,
isTeamObserver,
isTeamMaintainer,
isTeamMaintainerOrTeamAdmin,
isAnyTeamObserverPlus,
isAnyTeamMaintainer,
isAnyTeamMaintainerOrTeamAdmin,
isTeamAdmin,
isAnyTeamAdmin,
isOnlyObserver,
isObserverPlus,
isNoAccess,
};