Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
Activedirectory bugfixes + store model loading enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Waldmann committed Jun 17, 2015
1 parent 7f24e39 commit 2c0d39c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 56 deletions.
60 changes: 34 additions & 26 deletions lib/store.js
Expand Up @@ -23,14 +23,15 @@ var Store = function(config){
STORE_COUNTER++;

config = config || {};

this.models = {};
this.config = config;
this.global = config.global || false;
this.type = config.type || 'base';
this.logger = config.logger || pseudo_logger;
this.name = config.name || 'store' + STORE_COUNTER;
this.throw = config.throw_errors || process.env.NODE_ENV === 'test';

this.definitions = {};
this.models = {};
this.config = config;
this.global = config.global || false;
this.type = config.type || 'base';
this.logger = config.logger || pseudo_logger;
this.name = config.name || 'store' + STORE_COUNTER;
this.throw = config.throw_errors || process.env.NODE_ENV === 'test';

this.models_waiting = 0;
this.middleware = [];
Expand Down Expand Up @@ -84,42 +85,49 @@ Store.prototype.Model = function(name, fn){
if(!fn){
return this.models[name.toLowerCase()];
}

if(this.models[name.toLowerCase()]){
//extend the existing model
this.models[name.toLowerCase()].definition.use(fn);
}else{
//create a new model

if(this.definitions[name.toLowerCase()]){
this.definitions[name.toLowerCase()].use(fn);
return;
}

//create a new model
this.models_waiting++;

var self = this;
var definition = new Definition(this, name);


definition.include(this.mixinPaths);

definition.include(this.mixinPaths);
definition.use(fn);

definition.define(function(Model){
self.models[name.toLowerCase()] = Model;

this.definitions[name.toLowerCase()] = definition;
process.nextTick(function(){
definition.define(function(Model){
self.models[name.toLowerCase()] = Model;

if(self.global){
if(self.config.global_prefix){
global[self.config.global_prefix + name] = Model;
}else{
global[name] = Model;
if(self.global){
if(self.config.global_prefix){
global[self.config.global_prefix + name] = Model;
}else{
global[name] = Model;
}
}
}


//emit `model_created` and `<model-name>_created` events
self.emit('model_created', Model);
self.emit(name + '_created', Model);
//emit `model_created` and `<model-name>_created` events
self.emit('model_created', Model);
self.emit(name + '_created', Model);

self.ready();
self.ready();
});
});


}

};
Expand Down
4 changes: 1 addition & 3 deletions lib/stores/activedirectory/schema/organizational_unit.js
Expand Up @@ -46,8 +46,6 @@ exports.store = {
});

});

this.models.ou = this.Model('OrganizationalUnit');


}
}
12 changes: 7 additions & 5 deletions lib/stores/ldap/attributes.js
Expand Up @@ -49,11 +49,13 @@ exports.store = {
var tmp = [];

for(var i in this.models){
for(var name in this.models[i].definition.attributes){
if(this.models[i].definition.attributes.hasOwnProperty(name)){
if(!binary_only || (binary_only && this.models[i].definition.attributes[name].type.binary)){
tmp.push(name);
}
if(this.models[i].definition){
for(var name in this.models[i].definition.attributes){
if(this.models[i].definition.attributes.hasOwnProperty(name)){
if(!binary_only || (binary_only && this.models[i].definition.attributes[name].type.binary)){
tmp.push(name);
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "openrecord",
"version": "1.9.3",
"version": "1.9.4",
"description": "Active record like ORM for nodejs",
"keywords": ["orm", "record", "sql", "sqlite3", "postgres", "pg", "mysql", "database", "activerecord", "promise", "rest", "restify", "nested set", "ldap", "activedirectory", "active directory", "ad"],
"author": "Philipp Waldmann <philipp.waldmann@s-team.at>",
Expand Down
16 changes: 8 additions & 8 deletions test/ldap/client/__shared/create-test.js
Expand Up @@ -21,7 +21,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){
describe('OU', function(){
it('creates a new ou', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
var ou = Ou.new({
name: 'new_ou',
parent_dn: 'ou=create_test,ou=openrecord,' + LDAP_BASE
Expand All @@ -47,7 +47,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('creates a new ou with all attributes', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
var ou = Ou.new({
name: 'all_attribute_ou',
description: 'Description with utf-8 chars öäü',
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('creates nested ous', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');

var child_ou = Ou.new({
name: 'sub_nested_ou'
Expand Down Expand Up @@ -106,7 +106,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('creates reverse nested ous (bottom up)', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');

var ou = Ou.new({
name: 'level5',
Expand Down Expand Up @@ -155,7 +155,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on missing ou name', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
var ou = Ou.new({
parent_dn: 'ou=create_test,ou=openrecord,' + LDAP_BASE
});
Expand All @@ -170,7 +170,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on missing parent_dn', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
var ou = Ou.new({
name: 'foo'
});
Expand All @@ -185,7 +185,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on empty ou record', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
var ou = Ou.new({});

ou.save(function(success){
Expand Down Expand Up @@ -344,7 +344,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on missing parent_dn', function(next){
store.ready(function(){
var Group = store.Model('Ou');
var Group = store.Model('OrganizationalUnit');
var group = Group.new({
name: 'foo'
});
Expand Down
6 changes: 3 additions & 3 deletions test/ldap/client/__shared/destroy-test.js
Expand Up @@ -24,7 +24,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('destroys an ou', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=destroy_me_ou,ou=destroy_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.destroy(function(success){
Expand All @@ -44,7 +44,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on ou destroys with children', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=destroy_me_sub_ou,ou=destroy_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.destroy(function(success){
Expand All @@ -60,7 +60,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('destroys ou with all children', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=destroy_me_sub_ou,ou=destroy_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.destroyAll(function(success){
Expand Down
6 changes: 3 additions & 3 deletions test/ldap/client/__shared/exec-test.js
Expand Up @@ -20,7 +20,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('get all ou objects of the base ou', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.searchRoot('ou=openrecord,' + LDAP_BASE).exec(function(ous){
ous.length.should.be.equal(1);
ous[0].name.should.be.equal('exec_test');
Expand All @@ -32,7 +32,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('get all ou objects of the base ou incl. child objects', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.searchRoot('ou=openrecord,' + LDAP_BASE, true).exec(function(ous){
ous.length.should.be.equal(6);
ous[0].name.should.be.equal('openrecord');
Expand All @@ -43,7 +43,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('get all ou objects of the base ou inkl. child objects (recursive)', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.searchRoot('ou=openrecord,' + LDAP_BASE).recursive().exec(function(ous){
ous.length.should.be.equal(6);
ous[0].name.should.be.equal('openrecord');
Expand Down
4 changes: 2 additions & 2 deletions test/ldap/client/__shared/include-test.js
Expand Up @@ -20,7 +20,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('get child objects (one level)!', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.searchRoot('ou=openrecord,' + LDAP_BASE).include('children').exec(function(ous){
ous.length.should.be.equal(1);
ous[0].children.length.should.be.equal(4);
Expand All @@ -32,7 +32,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('get all recursive child objects!', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.searchRoot('ou=openrecord,' + LDAP_BASE).include('all_children').exec(function(ous){
ous.length.should.be.equal(1);
ous[0].all_children.length.should.not.be.equal(0);
Expand Down
10 changes: 5 additions & 5 deletions test/ldap/client/__shared/update-test.js
Expand Up @@ -24,7 +24,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on invalid move', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=move_me_ou,ou=update_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.parent_dn = 'ou=somewhere,' + LDAP_BASE;
Expand All @@ -42,7 +42,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('returns an error on invalid name', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=move_me_ou,ou=update_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.name = 'cn=foooo';
Expand All @@ -60,7 +60,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('moves an ou to another parent', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=move_me_ou,ou=update_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.parent_dn = target_ou
Expand All @@ -84,7 +84,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('renames an ou', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=rename_me_ou,ou=update_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.name = 'new_name';
Expand All @@ -109,7 +109,7 @@ module.exports = function(title, beforeFn, afterFn, store_conf){

it('changes an ou', function(next){
store.ready(function(){
var Ou = store.Model('Ou');
var Ou = store.Model('OrganizationalUnit');
Ou.find('ou=change_me_ou,ou=update_test,ou=openrecord,' + LDAP_BASE).exec(function(ou){

ou.description = 'very important';
Expand Down

0 comments on commit 2c0d39c

Please sign in to comment.