slouch/scripts/security.js
urdeveloper 21c73ffb26 Encode database names and document Ids used n Urls (#55)
* Encode database names and document Ids used n Urls

* Encode viewDocId as well and add the corresponding test cases.

* Fix jshint errors
2017-12-06 16:44:06 -08:00

64 lines
1.3 KiB
JavaScript

'use strict';
var Security = function (slouch) {
this._slouch = slouch;
};
// For example:
// {
// "admins" : {
// "names" : ["joe", "phil"],
// "roles" : ["boss"]
// },
// "members" : {
// "names" : ["dave"],
// "roles" : ["producer", "consumer"]
// }
// }
Security.prototype.set = function (dbName, security) {
return this._slouch._req({
uri: this._slouch._url + '/' + encodeURIComponent(dbName) + '/_security',
method: 'PUT',
body: JSON.stringify(security)
});
};
Security.prototype.get = function (dbName) {
return this._slouch._req({
uri: this._slouch._url + '/' + encodeURIComponent(dbName) + '/_security',
method: 'GET',
parseBody: true
});
};
Security.prototype.onlyRoleCanView = function (dbName, role) {
return this.set(dbName, {
admins: {
names: ['_admin'],
roles: []
},
members: {
names: [],
roles: [role]
}
});
};
Security.prototype.onlyUserCanView = function (dbName, user) {
return this.set(dbName, {
admins: {
names: ['_admin'],
roles: []
},
members: {
names: [user],
roles: []
}
});
};
Security.prototype.onlyAdminCanView = function (dbName) {
return this.onlyRoleCanView(dbName, '_admin');
};
module.exports = Security;