mirror of
https://github.com/donl/slouch.git
synced 2026-05-25 22:07:24 -06:00
* Encode database names and document Ids used n Urls * Encode viewDocId as well and add the corresponding test cases. * Fix jshint errors
64 lines
1.3 KiB
JavaScript
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;
|