// 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']
});