Skip to content

Instantly share code, notes, and snippets.

@carlos8f
Created October 5, 2012 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlos8f/3842709 to your computer and use it in GitHub Desktop.
Save carlos8f/3842709 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']
});
relations.repos('%s is the owner of %s', carlos, buffet);
relations.repos('%s is a collaborator of %s', carlos, views);
relations.repos('%s is a watcher', carlos);
relations.repos('%s is an owner of %s', brian, views);
relations.repos('%s is a watcher', brian);
relations.repos('%s is a watcher', sagar);
// check if user can perform an action
relations.repos('can :user administrate :repo', {user: brian, repo: views}, function (can) {
// can = true
});
relations.repos('can %s push to %s', carlos, views, function (can) {
// can = true
});
relations.repos('can %s pull', sagar, function (can) {
// can = true
});
// check if user has a role
relations.repos('is %s a collaborator of %s', brian, buffet, function (is) {
// is = false
});
relations.repos('is %s a watcher', sagar, function (is) {
// is = true
});
// fetching ids for a permission:
relations.repos('what can %s pull from', carlos, function (err, list) {
// list = ['carlos8f/node-buffet', 'cpsubrian/node-views']
});
brian.repos('can administrate what', function (err, list) {
// list = ['cpsubrian/node-views']
});
relations.repos('what can %s pull from', sagar, function (err, list) {
// list = []
});
// fetching ids for a role:
relations.repos('what is %s a collaborator of', carlos, function (err, list) {
// list = ['cpsubrian/node-views']
});
// revoking a role:
relations.repos('%s is not a collaborator of %s', carlos, views);
// collaborator for cpsubrian/node-views is revoked!
relations.repos('can %s push to %s', carlos, views, function (can) {
// can = false
});
// blogging situation
relations.define('posts', {
author: ['edit', 'post', 'read'],
collaborator: ['edit', 'read'],
reader: ['read'],
comment: []
});
var post = '2012-10-08-hello-world.md';
var comment = '#123';
relations.posts('%s is the author of %s', carlos, post);
relations.posts('%s is a collaborator of %s', brian, post);
relations.posts('%s is a reader', sagar);
relations.posts('%s is a comment of %s', comment, post);
// comment relationship and reverse relationship
relations.posts('what is a comment of %s', post, function (err, list) {
// list = ['#123']
});
relations.posts('what is %s a comment of', comment, function (err, list) {
// list = ['2012-10-08-hello-world.md']
});
// followers
relations.define('followers', {
follower: ['view feed']
});
relations.followers('%s is a follower of %s', carlos, brian);
relations.followers('%s is a follower of %s', brian, sagar);
relations.followers('%s is a follower of %s', sagar, carlos);
relations.followers('what is a follower of %s', carlos, function (err, list) {
// list = ['astrosag_ngc4414']
});
relations.followers('what is %s a follower of', carlos, function (err, list) {
// list = ['cpsubrian']
});
relations.followers('can %s view feed of %s', brian, carlos, function (can) {
// can = false
});
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