2020-06-23 17:29:38 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-08-20 10:50:35 +00:00
|
|
|
const { NotAuthorizedError } = require('./Errors')
|
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
let AuthorizationManager
|
|
|
|
module.exports = AuthorizationManager = {
|
|
|
|
assertClientCanViewProject(client, callback) {
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager._assertClientHasPrivilegeLevel(
|
2020-06-23 17:29:44 +00:00
|
|
|
client,
|
|
|
|
['readOnly', 'readAndWrite', 'owner'],
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2014-11-13 17:07:05 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
assertClientCanEditProject(client, callback) {
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager._assertClientHasPrivilegeLevel(
|
2020-06-23 17:29:44 +00:00
|
|
|
client,
|
|
|
|
['readAndWrite', 'owner'],
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
_assertClientHasPrivilegeLevel(client, allowedLevels, callback) {
|
2020-07-07 10:06:02 +00:00
|
|
|
if (allowedLevels.includes(client.ol_context.privilege_level)) {
|
|
|
|
callback(null)
|
2020-06-23 17:29:44 +00:00
|
|
|
} else {
|
2020-08-20 10:50:35 +00:00
|
|
|
callback(new NotAuthorizedError())
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
assertClientCanViewProjectAndDoc(client, doc_id, callback) {
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager.assertClientCanViewProject(client, function (error) {
|
|
|
|
if (error) {
|
2020-06-23 17:29:44 +00:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager._assertClientCanAccessDoc(client, doc_id, callback)
|
2020-06-23 17:29:44 +00:00
|
|
|
})
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
assertClientCanEditProjectAndDoc(client, doc_id, callback) {
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager.assertClientCanEditProject(client, function (error) {
|
|
|
|
if (error) {
|
2020-06-23 17:29:44 +00:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-07-07 10:06:02 +00:00
|
|
|
AuthorizationManager._assertClientCanAccessDoc(client, doc_id, callback)
|
2020-06-23 17:29:44 +00:00
|
|
|
})
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
_assertClientCanAccessDoc(client, doc_id, callback) {
|
|
|
|
if (client.ol_context[`doc:${doc_id}`] === 'allowed') {
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(null)
|
2020-06-23 17:29:44 +00:00
|
|
|
} else {
|
2020-08-20 10:50:35 +00:00
|
|
|
callback(new NotAuthorizedError())
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
addAccessToDoc(client, doc_id, callback) {
|
|
|
|
client.ol_context[`doc:${doc_id}`] = 'allowed'
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(null)
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeAccessToDoc(client, doc_id, callback) {
|
|
|
|
delete client.ol_context[`doc:${doc_id}`]
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(null)
|
2021-07-13 11:04:45 +00:00
|
|
|
},
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|