Permalink
Browse files

migrating the architecture from events to callbacks in views

  • Loading branch information...
1 parent 84a00dd commit 0ce4e6cdd00a65e115a8de9fb949cd6d58500a91 @geek-cetas committed May 3, 2011
Showing with 81 additions and 45 deletions.
  1. +4 −3 cms/models/group.js
  2. +2 −1 cms/models/permissions.js
  3. +9 −4 cms/models/user.js
  4. +4 −4 cms/views/groups.js
  5. +1 −1 cms/views/permissions.js
  6. +41 −14 cms/views/users.js
  7. +20 −18 test.js
View
@@ -1,14 +1,15 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
- Permission = require('./permissions').Permission;
+ Permission = require('./permissions').Permission_model;
var Group = new Schema({
name : String,
- Permissions : [Permission]
+ permissions : [Schema.ObjectId]
});
mongoose.model( 'Group', Group );
module.exports = {Group : function() {
- return mongoose.model('Group'); }
+ return mongoose.model('Group'); },
+ Group_model : Group,
};
@@ -10,5 +10,6 @@ mongoose.model( 'Permission', Permission );
module.exports = {Permission : function() {
return mongoose.model('Permission');
- }
+ },
+ Permission_model : Permission
}
View
@@ -1,20 +1,25 @@
-var mongoose = require('mongoose'),
+var inherits = require('sys').inherits,
+ mongoose = require('mongoose'),
Group = require('./group').Group,
Permission = require('./permissions').Permission,
Schema = mongoose.Schema;
+var UserSchema = function(obj) {
+}
+
var User = new Schema({
name : String,
pwd : String,
is_staff : {type : Number, min : 0, max : 1, Default : 0},
is_active : {type : Number, min : 0, max : 1, Default : 1},
is_superuser : {type : Number, min : 0, max : 1, Default : 0},
- Groups : [Group],
- Permissions : [Permission]
+ groups : [Schema.ObjectId],
+ permissions : [Schema.ObjectId]
});
mongoose.model( 'User', User );
module.exports = {User : function() {
return mongoose.model('User');
- }
+ },
+ User_model : User
}
View
@@ -10,15 +10,15 @@ inherits( Operations, ev.EventEmitter );
Operations.prototype.create = function( name, permissions ) {
var $ = this;
- var grp = new group;
+ var grp = new group();
grp.name = name;
if( permissions )
- grp.Permissions.push( {name : permissions.name,
- code : permissions.code} );
+ for( var index = 0; index < permissions.length; index++ )
+ grp.permissions.push( permissions[index]._id );
grp.save( function( err ) {
ex( $, err, function() {
- $.emit( 'create', $.grp );
+ $.emit( 'create', grp );
});
});
}
View
@@ -10,7 +10,7 @@ inherits( Operations, ev.EventEmitter );
Operations.prototype.create = function( alias, name ) {
var $ = this;
- var perm = new permission({ 'name' : name, 'code' : alias });
+ var perm = new permission( {'name' : name, 'code' : alias} );
perm.save( function( error ) {
ex( $, error, function() {
$.emit( 'create', perm );
View
@@ -1,36 +1,63 @@
var ev = require('events'),
inherits = require('sys').inherits,
- ex = require('../../../webcetas.node/ex').ex,
+ safe = require('../../../webcetas.node/safe');
user = require('../models/user').User();
-var Operations = function() {
-}
+var Operations = function( uname ) {
+ if( !uname ) return;
-inherits( Operations, ev.EventEmitter );
+ var $ = this;
+ $.search( uname, function( user ) {
+ if( user.__proto__ == Array.prototype && user.length > 1 )
+ throw new Error("Invalid username. Duplicate entries");
+ else
+ $.user = user;
+ } );
+
+}
-Operations.prototype.create = function( name, pwd ) {
+Operations.prototype.create = function( name, pwd, callback ) {
var $ = this;
var usr = new user;
usr.name = name;
usr.pwd = pwd;
usr.is_active = 1;
usr.save( function( err ) {
- ex( $, err, function() {
- $.emit( 'create', usr );
- });
+ $.user = usr;
+ safe.ex( err, callback, usr );
});
}
-Operations.prototype.search = function( name ) {
+Operations.prototype.add_permission = function( permission, callback ) {
var $ = this;
- user.find({'name' : name}, function( err, docs ) {
- ex( $, err, function() {
- $.emit( 'search', docs.length );
- });
+ if( this.user == null )
+ {
+ throw new Error("Invalid user");
+ return false;
+ }
+ this.user.permissions.push( permission );
+ this.user.save( function( err ) {
+ safe.ex( err, callback, $.user );
});
}
+Operations.prototype.search = function( name, callback ) {
+ var $ = this;
+ user.find( {'name' : name}, function( err, docs ) {
+ safe.ex( err, callback, docs );
+ });
+}
+
+var has_perm = function( user, perm ) {
+ if( user.permissions )
+ for( var index = 0; index < user.permissions.length; index ++ )
+ if( permissions[index] == perm.code )
+ return true;
+ return false;
+}
+
module.exports = {UserManager : function() {
return new Operations();
- }
+ },
+ has_permission : has_perm,
}
View
38 test.js
@@ -1,25 +1,27 @@
-var um = require('./cms/views/users').UserManager;
-var gm = require('./cms/views/groups').GroupManager;
-var pm = require('./cms/views/permissions').PermissionsManager;
+var uo = require('./cms/views/users'),
+ um = uo.UserManager,
+ go = require('./cms/views/groups'),
+ gm = go.GroupManager,
+ po = require('./cms/views/permissions'),
+ pm = po.PermissionsManager;
-var newuser = (new um).on( 'create',
+var newuser = new um;
+var newpm = new pm;
+
+newpm.on( 'create', function( perm ) {
+ newgrp.create('dummy', new Array(perm));
+ console.log( newuser );
+ newuser.add_permission( perm, function() { console.log("Added"); } );
+ }).on( 'error', function( err ) {
+ console.log("ERROR : " + err);
+});
+
+newuser.create('kailash', 'kailash',
function( obj ) {
- var v = obj.is_active;
- console.log(v + 1)
- }).on( 'search', function( docs ) {
- console.log(typeof( docs ));
+ newpm.create('bla', 'bla');
});
var newgrp = (new gm).on( 'create', function( group ) {
- });
+});
-var newpm = (new pm).on( 'create', function( perm ) {
- newgrp.create('dummy', perm);
- }).on( 'error', function( err ) {
- console.log("ERROR : " + err);
- });
-newuser.create('kailash', 'kals');
-newuser.search('kailash');
-newpm.create('hello', "Hello");
-newgrp.search('dummy');

0 comments on commit 0ce4e6c

Please sign in to comment.