Skip to content

Instantly share code, notes, and snippets.

@cpsubrian
Forked from carlos8f/api.js
Created October 8, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsubrian/3854779 to your computer and use it in GitHub Desktop.
Save cpsubrian/3854779 to your computer and use it in GitHub Desktop.
relations api
// relations api
// users
var carlos = 'carlos8f'
, brian = 'cpsubrian'
, sagar = 'astrosag_ngc4414'
// repos
var buffet = 'carlos8f/node-buffet'
, views = 'cpsubrian/node-views'
// relations api
var relations = require('relations');
relations.on('init', function (cb) {
// connect to database...
cb();
});
relations.define('repos', {
owner: ['pull', 'push', 'administrate'],
collaborator: ['pull', 'push'],
watcher: ['pull']
});
// Give roles to users:
// --------------------
relations.repos.give(carlos, 'owner', buffet);
relations.repos.give(carlos, 'collaborator', views);
relations.repos.give(carlos, 'watcher');
relations.repos.give(brian, 'owner', views);
relations.repos.give(brian, 'watcher');
relations.repos.give(sagar, 'watcher');
// Could bind give to a user context:
User.prototype.repos.give = function (role, obj) {
relations.repos.give.call(relations.repos, this.id, role, obj);
}
var brian = new User(...);
brian.repos.give('collaborator', buffet);
// Revoking a role:
// ----------------
relations.repos.revoke(carlos, 'collaborator', views);
// collaborator for cpsubrian/node-views is revoked!
// Check if user can perform an action:
// ------------------------------------
relations.repos.can(brian, 'administrate', views, function (can) {
// can = true
});
relations.repos.can(carlos, 'push', views, function (can) {
// can = true
});
relations.repos.can(sagar, 'pull', function (can) {
// can = true
});
// Could bind can to a user context:
User.prototype.repos.can = function (role, obj) {
relations.repos.can.call(relations.repos, this.id, role, obj);
}
var brian = new User(...);
brian.repos.can('pull', buffet);
// Check if user has a role:
// -------------------------
relations.repos.is(brian, 'collaborator', buffet, function (is) {
// is = false
});
relations.repos.is(sagar, 'watcher', function (is) {
// is = true
});
// Could bind is to a user context:
User.prototype.repos.is = function (role, obj) {
relations.repos.is.call(relations.repos, this.id, role, obj);
}
var brian = new User(...);
brian.repos.is('watcher');
// Fetching ids for a permission:
// ------------------------------
relations.repos.list(carlos, 'pull', function (err, list) {
// list = ['carlos8f/node-buffet', 'cpsubrian/node-views']
});
relations.repos.list(sagar, 'pull', function (err, list) {
// list = []
});
// Fetching ids for a role: (would required no conflicts
// between roles and permisison).
// -----------------------------------------------------
relations.repos.list(carlos, 'collaborator', function (err, list) {
// list = ['cpsubrian/node-views']
});
// Fetching users for a role:
// --------------------------
relations.repos.who('administrate', views, function (err, users) {
// users = ['brian']
});
var archy = require('archy');
var users = {
label: 'users',
nodes: [
{
label: 'carlos8f',
nodes: [
{
label: 'repos',
nodes: [
{ label: 'owner', nodes: ['carlos8f/node-buffet'] },
{ label: 'collaborator', nodes: ['cpsubrian/node-views'] },
'watcher'
]
}
]
},
{
label: 'cpsubrian',
nodes: [
{
label: 'repos',
nodes: [
{ label: 'owner', nodes: ['cpsubrian/node-views'] },
'watcher'
]
}
]
},
{
label: 'astrosag_ngc4414',
nodes: [ 'watcher' ]
}
]
};
var realms = {
label: 'realms',
nodes: [
{
label: 'repos',
nodes: [
{ label: 'owner', nodes: ['pull', 'push', 'administrate'] },
{ label: 'collaborator', nodes: ['pull', 'push'] },
{ label: 'watcher', nodes: ['pull'] }
]
}
]
};
console.log(archy(users));
console.log();
console.log(archy(realms));
users
├─┬ carlos8f
│ └─┬ repos
│ ├─┬ owner
│ │ └── carlos8f/node-buffet
│ ├─┬ collaborator
│ │ └── cpsubrian/node-views
│ └── watcher
├─┬ cpsubrian
│ └─┬ repos
│ ├─┬ owner
│ │ └── cpsubrian/node-views
│ └── watcher
└─┬ astrosag_ngc4414
└── watcher
realms
└─┬ repos
├─┬ owner
│ ├── pull
│ ├── push
│ └── administrate
├─┬ collaborator
│ ├── pull
│ └── push
└─┬ watcher
└── pull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment