mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-23 04:47:26 +08:00
initial graph plugin
This commit is contained in:
11
lib/db.js
11
lib/db.js
@@ -29,7 +29,12 @@ function collection(db, model, fn) {
|
||||
module.exports = {
|
||||
|
||||
find: ready(function(model) {
|
||||
var query = model.toQuery() || {};
|
||||
var query = model.toQuery() || {}
|
||||
, id = model.get('_id')
|
||||
, _id = id && ObjectID(id)
|
||||
;
|
||||
|
||||
if(_id) query._id = _id;
|
||||
|
||||
collection(db, model, function(err, collection) {
|
||||
// TODO limit 1 for models, allow all for collections
|
||||
@@ -44,11 +49,15 @@ module.exports = {
|
||||
var query = model.toQuery()
|
||||
, changes = model.attributes
|
||||
, options = {safe: true, upsert: true}
|
||||
, id = model.get('_id')
|
||||
, _id = id && ObjectID(id)
|
||||
, callback = function(err, result) {
|
||||
model.refresh(changes);
|
||||
}
|
||||
;
|
||||
|
||||
if(_id && query) query._id = _id;
|
||||
|
||||
collection(db, model, function(err, collection) {
|
||||
if(query) {
|
||||
collection.update(query, changes, options, callback);
|
||||
|
||||
47
lib/model.js
47
lib/model.js
@@ -145,30 +145,12 @@ Model = module.exports = emitter.spawn({
|
||||
var permissions = this.allowed
|
||||
, special = permissions.special
|
||||
, rights = permissions[action]
|
||||
, requiresUser = action === 'create' && rights === 'user'
|
||||
, requiresUser = rights === 'user'
|
||||
, actor = this.actor()
|
||||
, allowed = true
|
||||
, model = this
|
||||
;
|
||||
|
||||
if(special) {
|
||||
Object.getOwnPropertyNames(special).forEach(function(key) {
|
||||
var perms = special[key]
|
||||
, right = perms[action]
|
||||
, allowed = group === 'public' || group === right || root
|
||||
;
|
||||
|
||||
if(!allowed) {
|
||||
if(action === 'read') {
|
||||
// TODO build select object where {key: 0}
|
||||
delete model.attributes[key];
|
||||
} else {
|
||||
model.error('The current user cannot ' + action + ' the key: ', key, 'Not Allowed');
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if(requiresUser && !actor) {
|
||||
model.error('Only logged in users can ' + action);
|
||||
fn();
|
||||
@@ -189,6 +171,24 @@ Model = module.exports = emitter.spawn({
|
||||
, allowed = root || (groups && groups[rights]) || (requiresCreator && isCreator)
|
||||
;
|
||||
|
||||
if(special) {
|
||||
Object.getOwnPropertyNames(special).forEach(function(key) {
|
||||
var perms = special[key]
|
||||
, right = perms[action]
|
||||
, allowed = right === 'public' || (groups && groups[right]) || root
|
||||
;
|
||||
|
||||
if(!allowed) {
|
||||
if(action === 'read') {
|
||||
// TODO build select object where {key: 0}
|
||||
delete model.attributes[key];
|
||||
} else {
|
||||
model.error('The current user cannot ' + action + ' the key: ', key, 'Not Allowed');
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if(requiresCreator && !isCreator) {
|
||||
model.error('The current user must be the creator to ' + action, 'Not Allowed');
|
||||
}
|
||||
@@ -398,11 +398,18 @@ Model = module.exports = emitter.spawn({
|
||||
|
||||
});
|
||||
|
||||
var spawn = module.exports.spawn;
|
||||
var spawn = module.exports.spawn
|
||||
, _models = {}
|
||||
;
|
||||
|
||||
module.exports.refreshSettings = function(collection) {
|
||||
_models[collection].updateSettings();
|
||||
}
|
||||
|
||||
module.exports.spawn = function(model) {
|
||||
var instance = spawn.apply(this, arguments);
|
||||
if(model && model.collection) {
|
||||
_models[instance.collection] = instance;
|
||||
instance.updateSettings();
|
||||
}
|
||||
return instance;
|
||||
|
||||
@@ -2,17 +2,10 @@ var app = require('../../app')
|
||||
, config = require('../../config').load()
|
||||
, Settings = require('./settings')
|
||||
, Setting = require('./setting')
|
||||
, graph = require('../graph')
|
||||
, Model = require('../../model')
|
||||
;
|
||||
|
||||
app.post('/setting', function (req, res) {
|
||||
Setting
|
||||
.spawn()
|
||||
.set(req.body)
|
||||
.notify(res)
|
||||
.save()
|
||||
;
|
||||
});
|
||||
|
||||
app.get('/settings', function(req, res) {
|
||||
Settings
|
||||
.spawn()
|
||||
@@ -26,9 +19,13 @@ app.post('/settings', function(req, res) {
|
||||
Settings
|
||||
.spawn()
|
||||
.for(req)
|
||||
.find({name: req.body.name, plugin: req.body.name})
|
||||
.find({name: req.body.name, plugin: req.body.plugin})
|
||||
.set(req.body)
|
||||
.notify(res)
|
||||
.notify(function(json) {
|
||||
if(req.body.plugin === 'graph') graph.refresh();
|
||||
else if(json.collection) Model.refreshSettings(json.collection);
|
||||
res.send(json);
|
||||
})
|
||||
.save()
|
||||
;
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ var app = require('../../app')
|
||||
, Group = require('./group')
|
||||
, User = require('./user')
|
||||
, Users = require('./users')
|
||||
, ObjectID = require('mongodb').BSON
|
||||
;
|
||||
|
||||
function user(action, params, req, res) {
|
||||
@@ -42,8 +43,14 @@ app.del('/me', function(req, res) {
|
||||
if(u) user('remove', u, req, res);
|
||||
});
|
||||
|
||||
app.get('/user/:email', function(req, res) {
|
||||
user('fetch', {email: req.param('email')}, req, res);
|
||||
app.get('/user/:id', function(req, res) {
|
||||
User
|
||||
.spawn()
|
||||
.for(req)
|
||||
.find({_id: req.param('id')})
|
||||
.notify(res)
|
||||
.fetch()
|
||||
;
|
||||
});
|
||||
|
||||
app.post('/user/:email/group', function(req, res) {
|
||||
|
||||
@@ -24,7 +24,8 @@ module.exports = Model.spawn({
|
||||
remove: 'creator',
|
||||
create: 'public',
|
||||
special: {
|
||||
groups: 'root'
|
||||
groups: {read: 'public', write: 'root'},
|
||||
email: {read: 'creator'}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -33,16 +33,15 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'2. find user by id': {
|
||||
route: '/user/' + user.email,
|
||||
'3. add a user to group': {
|
||||
route: '/user/' + user.email + '/group',
|
||||
data: {group: 'root'},
|
||||
expect: {
|
||||
_id: 'toExist',
|
||||
password: 'toNotExist',
|
||||
errors: 'toNotExist'
|
||||
}
|
||||
},
|
||||
|
||||
'3. login a user': {
|
||||
'4. login a user': {
|
||||
route: '/user/login',
|
||||
data: user,
|
||||
expect: {
|
||||
@@ -56,7 +55,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'4. get current user': {
|
||||
'5. get current user': {
|
||||
route: '/me',
|
||||
expect: {
|
||||
email: user.email,
|
||||
@@ -66,7 +65,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'5. searching users': {
|
||||
'6. searching users': {
|
||||
route: '/search?type=users&find={"email": "skawful@gmail.com"}',
|
||||
expect: {
|
||||
results: 'toExist',
|
||||
@@ -110,15 +109,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'10. add a user to group': {
|
||||
route: '/user/' + user.email + '/group',
|
||||
data: {group: 'root'},
|
||||
expect: {
|
||||
errors: 'toNotExist'
|
||||
}
|
||||
},
|
||||
|
||||
'11. only 1 user per email': {
|
||||
'10. only 1 user per email': {
|
||||
route: '/search/users',
|
||||
data: {email: user.email},
|
||||
expect: {
|
||||
@@ -126,7 +117,7 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'12. only 1 app per name': {
|
||||
'11. only 1 app per name': {
|
||||
route: '/search/apps',
|
||||
data: {name: app.name},
|
||||
expect: {
|
||||
@@ -134,15 +125,15 @@ var tests = {
|
||||
}
|
||||
},
|
||||
|
||||
'13. only 1 user': {
|
||||
'12. only 1 user': {
|
||||
route: '/search/users',
|
||||
data: {},
|
||||
expect: {
|
||||
results: 'toContainOne'
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// '14. delete a user': {
|
||||
// '13. delete a user': {
|
||||
// route: '/me?method=delete',
|
||||
// expect: {
|
||||
// errors: 'toNotExist'
|
||||
|
||||
Reference in New Issue
Block a user