AuthorizationManager.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { NotAuthorizedError } = require('./Errors')
  2. let AuthorizationManager
  3. module.exports = AuthorizationManager = {
  4. assertClientCanViewProject(client, callback) {
  5. AuthorizationManager._assertClientHasPrivilegeLevel(
  6. client,
  7. ['readOnly', 'readAndWrite', 'owner'],
  8. callback
  9. )
  10. },
  11. assertClientCanEditProject(client, callback) {
  12. AuthorizationManager._assertClientHasPrivilegeLevel(
  13. client,
  14. ['readAndWrite', 'owner'],
  15. callback
  16. )
  17. },
  18. _assertClientHasPrivilegeLevel(client, allowedLevels, callback) {
  19. if (allowedLevels.includes(client.ol_context.privilege_level)) {
  20. callback(null)
  21. } else {
  22. callback(new NotAuthorizedError())
  23. }
  24. },
  25. assertClientCanViewProjectAndDoc(client, docId, callback) {
  26. AuthorizationManager.assertClientCanViewProject(client, function (error) {
  27. if (error) {
  28. return callback(error)
  29. }
  30. AuthorizationManager._assertClientCanAccessDoc(client, docId, callback)
  31. })
  32. },
  33. assertClientCanEditProjectAndDoc(client, docId, callback) {
  34. AuthorizationManager.assertClientCanEditProject(client, function (error) {
  35. if (error) {
  36. return callback(error)
  37. }
  38. AuthorizationManager._assertClientCanAccessDoc(client, docId, callback)
  39. })
  40. },
  41. _assertClientCanAccessDoc(client, docId, callback) {
  42. if (client.ol_context[`doc:${docId}`] === 'allowed') {
  43. callback(null)
  44. } else {
  45. callback(new NotAuthorizedError())
  46. }
  47. },
  48. addAccessToDoc(client, docId, callback) {
  49. client.ol_context[`doc:${docId}`] = 'allowed'
  50. callback(null)
  51. },
  52. removeAccessToDoc(client, docId, callback) {
  53. delete client.ol_context[`doc:${docId}`]
  54. callback(null)
  55. },
  56. }