From 9c8ff4ceb0c88b607c55535f5c7c5cb7cc3d88f2 Mon Sep 17 00:00:00 2001 From: Hendrik de Graaf Date: Thu, 12 Oct 2023 14:10:23 +0200 Subject: [PATCH] chore: throw error if hasAuthority is called with invalid arguments --- .../common/getInterpretationAccess.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/Interpretations/common/getInterpretationAccess.js b/src/components/Interpretations/common/getInterpretationAccess.js index 0efbc8369..374e187c9 100644 --- a/src/components/Interpretations/common/getInterpretationAccess.js +++ b/src/components/Interpretations/common/getInterpretationAccess.js @@ -1,12 +1,15 @@ // For backwards compatibility // accept both Set (from the old d2.currentUser object) and array -const hasAuthority = (authorities = [], authority) => { - if (Array.isArray(authorities)) { +const hasAuthority = (authorities, authority) => { + if (!authority || typeof authority !== 'string) { + throw new Error(`"hasAuthority" requires "authority" to be a populated string but received ${authority}`) + } else if (Array.isArray(authorities)) { return authorities.includes(authority) + } else if (typeof authorities.has === 'function') { + return authorities.has(authority) + } else { + throw new Error(`"hasAuthority" requires "authorities" to be an array or set of authorities (strings)`) } - return typeof authorities.has === 'function' - ? authorities.has(authority) - : false } const isSuperuser = (authorities) => hasAuthority(authorities, 'ALL')