2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:34 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let AuthorizationManager;
|
|
|
|
module.exports = (AuthorizationManager = {
|
|
|
|
assertClientCanViewProject(client, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
return AuthorizationManager._assertClientHasPrivilegeLevel(client, ["readOnly", "readAndWrite", "owner"], callback);
|
|
|
|
},
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
assertClientCanEditProject(client, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
return AuthorizationManager._assertClientHasPrivilegeLevel(client, ["readAndWrite", "owner"], callback);
|
|
|
|
},
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
_assertClientHasPrivilegeLevel(client, allowedLevels, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
2020-06-23 13:29:38 -04:00
|
|
|
if (Array.from(allowedLevels).includes(client.ol_context.privilege_level)) {
|
2020-06-23 13:29:34 -04:00
|
|
|
return callback(null);
|
|
|
|
} else {
|
|
|
|
return callback(new Error("not authorized"));
|
|
|
|
}
|
|
|
|
},
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
assertClientCanViewProjectAndDoc(client, doc_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
return AuthorizationManager.assertClientCanViewProject(client, function(error) {
|
|
|
|
if (error != null) { return callback(error); }
|
|
|
|
return AuthorizationManager._assertClientCanAccessDoc(client, doc_id, callback);
|
|
|
|
});
|
|
|
|
},
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
assertClientCanEditProjectAndDoc(client, doc_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
return AuthorizationManager.assertClientCanEditProject(client, function(error) {
|
|
|
|
if (error != null) { return callback(error); }
|
|
|
|
return AuthorizationManager._assertClientCanAccessDoc(client, doc_id, callback);
|
|
|
|
});
|
|
|
|
},
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
_assertClientCanAccessDoc(client, doc_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
if (client.ol_context[`doc:${doc_id}`] === "allowed") {
|
|
|
|
return callback(null);
|
|
|
|
} else {
|
|
|
|
return callback(new Error("not authorized"));
|
|
|
|
}
|
|
|
|
},
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
addAccessToDoc(client, doc_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
client.ol_context[`doc:${doc_id}`] = "allowed";
|
|
|
|
return callback(null);
|
|
|
|
},
|
2016-09-02 11:35:00 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
removeAccessToDoc(client, doc_id, callback) {
|
|
|
|
if (callback == null) { callback = function(error) {}; }
|
|
|
|
delete client.ol_context[`doc:${doc_id}`];
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
});
|