current-user/UserAuthorities.js

  1. const auths = Symbol('authorities')
  2. /**
  3. * Simple wrapper class for the user authorities list
  4. *
  5. * @memberof module:current-user
  6. */
  7. class UserAuthorities {
  8. /**
  9. * Creates the UserAuthorities object based off the given set of the user's authorities.
  10. *
  11. * @param {string[]} authorities A set of the user's authorities.
  12. */
  13. constructor(authorities = []) {
  14. this[auths] = new Set(authorities)
  15. }
  16. /**
  17. * Checks if the given authority is in the user's authority list.
  18. *
  19. * If the user has the 'ALL' authority any request for a authority will return `true`.
  20. *
  21. * @param {string} authority The authority to check for
  22. */
  23. has(authority) {
  24. if (this[auths].has('ALL')) {
  25. return true
  26. }
  27. return this[auths].has(authority)
  28. }
  29. /**
  30. * Factory method for a UserAuthorities instance
  31. *
  32. * @param {string[]} authorities A set of the user's authorities as recieved from the api.
  33. */
  34. static create(authorities) {
  35. return new UserAuthorities(authorities)
  36. }
  37. }
  38. export default UserAuthorities