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
|
|
|
|
2023-03-20 14:10:40 +00:00
|
|
|
assertClientCanViewProjectAndDoc(client, docId, 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)
|
|
|
|
}
|
2023-03-20 14:10:40 +00:00
|
|
|
AuthorizationManager._assertClientCanAccessDoc(client, docId, callback)
|
2020-06-23 17:29:44 +00:00
|
|
|
})
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2023-03-20 14:10:40 +00:00
|
|
|
assertClientCanEditProjectAndDoc(client, docId, 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)
|
|
|
|
}
|
2023-03-20 14:10:40 +00:00
|
|
|
AuthorizationManager._assertClientCanAccessDoc(client, docId, callback)
|
2020-06-23 17:29:44 +00:00
|
|
|
})
|
|
|
|
},
|
2016-09-02 15:35:00 +00:00
|
|
|
|
2023-03-20 14:10:40 +00:00
|
|
|
_assertClientCanAccessDoc(client, docId, callback) {
|
|
|
|
if (client.ol_context[`doc:${docId}`] === '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
|
|
|
|
2023-03-20 14:10:40 +00:00
|
|
|
addAccessToDoc(client, docId, callback) {
|
|
|
|
client.ol_context[`doc:${docId}`] = 'allowed'
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(null)
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
|
|
|
|
2023-03-20 14:10:40 +00:00
|
|
|
removeAccessToDoc(client, docId, callback) {
|
|
|
|
delete client.ol_context[`doc:${docId}`]
|
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
|
|
|
}
|