From 52d05c2910b91738fd25a0141bb38aab03dd1718 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 31 Jul 2023 13:37:01 +0100 Subject: [PATCH] Making sure booleans returned from user role functions in shared core. --- .../shared-core/src/sdk/documents/users.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/shared-core/src/sdk/documents/users.ts b/packages/shared-core/src/sdk/documents/users.ts index d55321ee8b..facf10dfe3 100644 --- a/packages/shared-core/src/sdk/documents/users.ts +++ b/packages/shared-core/src/sdk/documents/users.ts @@ -2,7 +2,7 @@ import { ContextUser, User } from "@budibase/types" import { getProdAppID } from "./applications" // checks if a user is specifically a builder, given an app ID -export function isBuilder(user: User | ContextUser, appId?: string) { +export function isBuilder(user: User | ContextUser, appId?: string): boolean { if (!user) { return false } @@ -14,35 +14,36 @@ export function isBuilder(user: User | ContextUser, appId?: string) { return false } -export function isGlobalBuilder(user: User | ContextUser) { +export function isGlobalBuilder(user: User | ContextUser): boolean { return (isBuilder(user) && !hasAppBuilderPermissions(user)) || isAdmin(user) } // alias for hasAdminPermission, currently do the same thing // in future whether someone has admin permissions and whether they are // an admin for a specific resource could be separated -export function isAdmin(user: User | ContextUser) { +export function isAdmin(user: User | ContextUser): boolean { if (!user) { return false } return hasAdminPermissions(user) } -export function isAdminOrBuilder(user: User | ContextUser, appId?: string) { +export function isAdminOrBuilder(user: User | ContextUser, appId?: string): boolean { return isBuilder(user, appId) || isAdmin(user) } // check if they are a builder within an app (not necessarily a global builder) -export function hasAppBuilderPermissions(user?: User | ContextUser) { +export function hasAppBuilderPermissions(user?: User | ContextUser): boolean { if (!user) { return false } const appLength = user.builder?.apps?.length - return !user.builder?.global && appLength && appLength > 0 + const isGlobalBuilder = !!user.builder?.global + return !isGlobalBuilder && appLength != null && appLength > 0 } // checks if a user is capable of building any app -export function hasBuilderPermissions(user?: User | ContextUser) { +export function hasBuilderPermissions(user?: User | ContextUser): boolean { if (!user) { return false } @@ -50,9 +51,9 @@ export function hasBuilderPermissions(user?: User | ContextUser) { } // checks if a user is capable of being an admin -export function hasAdminPermissions(user?: User | ContextUser) { +export function hasAdminPermissions(user?: User | ContextUser): boolean { if (!user) { return false } - return user.admin?.global + return !!user.admin?.global }