mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
create/list subjects from mongo
This commit is contained in:
@@ -24,6 +24,16 @@ function init (env, authorization) {
|
||||
}));
|
||||
});
|
||||
|
||||
endpoints.post('/subjects', authorization.isPermitted('authorization:subjects:create'), function createSubjectt (req, res) {
|
||||
authorization.storage.createSubject(req.body, function created (err, created) {
|
||||
if (err) {
|
||||
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
|
||||
} else {
|
||||
res.json(created);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
endpoints.get('/roles', authorization.isPermitted('authorization:roles:list'), function getRoles (req, res) {
|
||||
res.json(authorization.storage.roles);
|
||||
});
|
||||
|
||||
@@ -3,10 +3,66 @@
|
||||
var _ = require('lodash');
|
||||
var crypto = require('crypto');
|
||||
var shiroTrie = require('shiro-trie');
|
||||
var ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
var find_options = require('../query');
|
||||
|
||||
function init (env, ctx) {
|
||||
var storage = { };
|
||||
|
||||
var rolesCollection = ctx.store.collection(env.authentication_collections_prefix + 'roles');
|
||||
var subjectsCollection = ctx.store.collection(env.authentication_collections_prefix + 'subjects');
|
||||
|
||||
storage.queryOpts = {
|
||||
dateField: 'created_at'
|
||||
};
|
||||
|
||||
function query_for (opts) {
|
||||
return find_options(opts, storage.queryOpts);
|
||||
}
|
||||
|
||||
storage.createSubject = function createSubject (obj, fn) {
|
||||
if (! obj.hasOwnProperty('created_at')){
|
||||
obj.created_at = (new Date()).toISOString();
|
||||
}
|
||||
subjectsCollection.insert(obj, function (err, doc) {
|
||||
storage.reload(function loaded ( ) {
|
||||
fn(null, doc.ops);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// query for entries from storage
|
||||
storage.listSubjects = function listSubjects (opts, fn) {
|
||||
// these functions, find, sort, and limit, are used to
|
||||
// dynamically configure the request, based on the options we've
|
||||
// been given
|
||||
|
||||
// determine sort options
|
||||
function sort ( ) {
|
||||
return opts && opts.sort || {date: -1};
|
||||
}
|
||||
|
||||
// configure the limit portion of the current query
|
||||
function limit ( ) {
|
||||
if (opts && opts.count) {
|
||||
return this.limit(parseInt(opts.count));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// handle all the results
|
||||
function toArray (err, entries) {
|
||||
fn(err, entries);
|
||||
}
|
||||
|
||||
// now just stitch them all together
|
||||
limit.call(subjectsCollection
|
||||
.find(query_for(opts))
|
||||
.sort(sort( ))
|
||||
).toArray(toArray);
|
||||
};
|
||||
|
||||
storage.reload = function reload (callback) {
|
||||
//TODO: load from mongo
|
||||
storage.roles = [
|
||||
@@ -15,45 +71,42 @@ function init (env, ctx) {
|
||||
{name: 'careportal', permissions: ['api:treatments:create', 'alarms:default:ack']}
|
||||
];
|
||||
|
||||
storage.subjects = _.map([
|
||||
{_id: '579cf4ad65110bbfc193acc3', name: 'Dad Mac', roles: ['admin']}
|
||||
,
|
||||
{_id: '579cf4ad65410bbfc193acc3', name: 'Dad Phone', roles: ['admin']}
|
||||
,
|
||||
{_id: '579cf48d65110bbfc193acc3', name: 'Dad Tablet', roles: ['admin']}
|
||||
,
|
||||
{_id: '579cef9265110bbfc193acbd', name: 'Mom Mac', roles: ['admin']}
|
||||
,
|
||||
{_id: '579cef9265110bbfc193abad', name: 'Mom Phone', roles: ['admin']}
|
||||
,
|
||||
{_id: '579ce3dd65110bbfc193acaf', name: 'Health Office', roles: ['careportal']}
|
||||
], function eachSubject (subject) {
|
||||
storage.listSubjects(null, function listResults (err, results) {
|
||||
|
||||
if (env.api_secret) {
|
||||
var shasum = crypto.createHash('sha1');
|
||||
shasum.update(env.api_secret);
|
||||
shasum.update(subject._id);
|
||||
var digest = shasum.digest('base64').substring(0, 10);
|
||||
var abbrev = subject.name.toLowerCase().replace(/[\W]/g, '').substring(0, 10);
|
||||
subject.digest = digest;
|
||||
subject.accessToken = abbrev + '-' + digest;
|
||||
if (!err) {
|
||||
storage.subjects = _.map(results, function eachSubject (subject) {
|
||||
console.info('>>>db subject', subject);
|
||||
if (env.api_secret) {
|
||||
var shasum = crypto.createHash('sha1');
|
||||
shasum.update(env.api_secret);
|
||||
shasum.update(subject._id.toString());
|
||||
var digest = shasum.digest('base64').substring(0, 10);
|
||||
var abbrev = subject.name.toLowerCase().replace(/[\W]/g, '').substring(0, 10);
|
||||
subject.digest = digest;
|
||||
subject.accessToken = abbrev + '-' + encodeURIComponent(digest);
|
||||
}
|
||||
|
||||
return subject;
|
||||
});
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
|
||||
return subject;
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback(null);
|
||||
}
|
||||
};
|
||||
|
||||
storage.rolesToPermissions = function rolesToPermissions (roleNames) {
|
||||
var permissions = [ ];
|
||||
|
||||
_.forEach(roleNames, function eachRoleName (roleName) {
|
||||
var role = _.find(storage.roles, {name: roleName});
|
||||
if (role) {
|
||||
permissions = permissions.concat(role.permissions);
|
||||
if (storage.roles) {
|
||||
var role = _.find(storage.roles, {name: roleName});
|
||||
if (role) {
|
||||
permissions = permissions.concat(role.permissions);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
if (serverSettings === undefined) {
|
||||
if (this.serverSettings === undefined) {
|
||||
console.error('server settings were not loaded, will not call init');
|
||||
} else {
|
||||
if (authorized) {
|
||||
authorized.lat = Date.now();
|
||||
if (this.authorized) {
|
||||
this.authorized.lat = Date.now();
|
||||
}
|
||||
window.Nightscout.client.init(serverSettings, Nightscout.plugins, authorized);
|
||||
window.Nightscout.client.init(this.serverSettings, Nightscout.plugins, this.authorized);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@ describe('Notifications API', function ( ) {
|
||||
, ddata: {
|
||||
lastUpdated: Date.now()
|
||||
}
|
||||
, store: {
|
||||
collection: function ( ) {
|
||||
return { };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ctx.authorization = require('../lib/authorization')(env, ctx);
|
||||
|
||||
Reference in New Issue
Block a user