Skip to content

Commit

Permalink
Add plugin loader access permission check.
Browse files Browse the repository at this point in the history
  • Loading branch information
rottmann committed Jan 13, 2017
1 parent 91f9a1e commit 31f17cd
Showing 1 changed file with 59 additions and 45 deletions.
104 changes: 59 additions & 45 deletions lib/plugin_loader.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var util = require('util');
var glob = require('glob');

var app = {};

function PluginLoader(_app) {
var self = this;
var self = this;

// global variables
app = _app;
// global variables
app = _app;

// class variables
self.plugins = {};
// class variables
self.plugins = {};

// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local)
this.detectPugins(__dirname);
// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local)
this.detectPugins(__dirname);

// Try to load local apidoc-plugins
this.detectPugins( path.join(process.cwd(), '/node_modules') );
// Try to load local apidoc-plugins
this.detectPugins( path.join(process.cwd(), '/node_modules') );

if (Object.keys(this.plugins).length === 0)
app.log.debug('No plugins found.');
if (Object.keys(this.plugins).length === 0) {
app.log.debug('No plugins found.');
}

this.loadPlugins();
this.loadPlugins();
}
/**
* Inherit
Expand All @@ -40,52 +42,64 @@ module.exports = PluginLoader;
* Search up to root until found a plugin.
*/
PluginLoader.prototype.detectPugins = function(dir) {
var self = this;

// Search from the given dir up to root.
// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test.
var plugins = glob.sync(dir + '/apidoc-plugin-*');
if (plugins.length === 0) {
dir = path.join(dir, '..');
if (dir === '/' || dir.substr(1) === ':\\')
return;
return this.detectPugins(dir);
var self = this;

try {
if (dir === '/') {
fs.accessSync(dir + '.')
} else {
fs.accessSync(dir + '/.')
}
} catch (e) {
app.log.warn(e);
return;
}

// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test.
var plugins = glob.sync(dir + '/apidoc-plugin-*');
if (plugins.length === 0) {
dir = path.join(dir, '..');
if (dir === '/' || dir.substr(1) === ':\\') {
return;
}
return this.detectPugins(dir);
}

var offset = dir.length + 1;
plugins.forEach( function(plugin) {
var name = plugin.substr(offset);
var filename = path.relative(__dirname, plugin);
app.log.debug('add plugin: ' + name + ', ' + filename);
self.addPlugin(name, plugin);
});
var offset = dir.length + 1;
plugins.forEach( function(plugin) {
var name = plugin.substr(offset);
var filename = path.relative(__dirname, plugin);
app.log.debug('add plugin: ' + name + ', ' + filename);
self.addPlugin(name, plugin);
});
};

/**
* Add Plugin to plugin list.
*/
PluginLoader.prototype.addPlugin = function(name, filename) {
if (this.plugins[name])
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]);
if (this.plugins[name]) {
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]);
}

this.plugins[name] = filename;
this.plugins[name] = filename;
};

/**
* Load and initialize Plugins.
*/
PluginLoader.prototype.loadPlugins = function() {
_.forEach(this.plugins, function(filename, name) {
app.log.debug('load plugin: ' + name + ', ' + filename);
var plugin;
try {
plugin = require(filename);
} catch(e) {
}
if (plugin && plugin.init) {
plugin.init(app);
} else {
app.log.debug('Ignored, no init function found.');
}
});
_.forEach(this.plugins, function(filename, name) {
app.log.debug('load plugin: ' + name + ', ' + filename);
var plugin;
try {
plugin = require(filename);
} catch(e) {
}
if (plugin && plugin.init) {
plugin.init(app);
} else {
app.log.debug('Ignored, no init function found.');
}
});
};

0 comments on commit 31f17cd

Please sign in to comment.