slouch/scripts/security.js
standup75 ef15df6bc9 Added onlyUserCanView (#28)
* Added onlyUserCanView

* Added tests

* removed test/README.md

* Beatified and fixed jshint error
2017-09-26 11:13:58 -07:00

64 lines
1.2 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 + '/' + dbName + '/_security',
method: 'PUT',
body: JSON.stringify(security)
});
};
Security.prototype.get = function (dbName) {
return this._slouch._req({
uri: this._slouch._url + '/' + 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;