AuthorizationManager.js 1.7 KB

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