Skip to content

Commit

Permalink
chore: throw error if hasAuthority is called with invalid arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric authored Oct 12, 2023
1 parent b286dee commit 9c8ff4c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/components/Interpretations/common/getInterpretationAccess.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit 9c8ff4c

Please sign in to comment.