slouch/scripts/security.js
Geoff Cox 54a60e7a21 feat(auth): cookie authentication in node (#27)
* feat(cookie): request-wrapper

* test(request-wrapper): 100% coverage

* test(browser): 100% coverage

* refactor(request-class): rename to enhanced-request

* feat(get-session)

* feat(enhanced-request): full response

* refactor(enhanced-request): enhanced opts

* feat(example): authentication
2017-09-23 04:49:07 -07:00

51 lines
1 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.onlyAdminCanView = function (dbName) {
return this.onlyRoleCanView(dbName, '_admin');
};
module.exports = Security;