Skip to content

Commit

Permalink
As per Zren request moving to alternate admin route from OpenUserJS#318
Browse files Browse the repository at this point in the history
Applies to OpenUserJS#296
  • Loading branch information
Martii committed Aug 23, 2014
1 parent 87d3620 commit 9f77511
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ app_route('/:type(scripts|libs)/:username/:namespace?/:scriptname/issues/:topic/
app.get('/admin', admin.adminPage);
app.get('/admin/authas', admin.authAsUser);
app.get('/admin/json', admin.adminJsonView);
app.get('/admin/npmls', admin.adminNpmLsView);
app.get('/admin/user/:id', admin.adminUserView);
app.get('/admin/api', admin.adminApiKeysPage);
app.post('/admin/api/update', admin.apiAdminUpdate);
Expand Down
52 changes: 33 additions & 19 deletions controllers/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var async = require('async');
var exec = require('child_process').exec;

var Comment = require('../models/comment').Comment;
var Discussion = require('../models/discussion').Discussion;
Expand Down Expand Up @@ -111,11 +112,32 @@ var jsonModelMap = {
'Discussion': Discussion,
'Comment': Comment,
'Vote': Vote,
'Flag': Flag,

// Virtual model
'NpmLs': 'NpmLs'
'Flag': Flag
};

// View everything about current modules for the server
// This is mostly for debugging in production
exports.adminNpmLsView = function (aReq, aRes, aNext) {
var authedUser = aReq.session.user;

//
var options = {};

// Session
authedUser = options.authedUser = modelParser.parseUser(authedUser);
options.isMod = authedUser && authedUser.isMod;
options.isAdmin = authedUser && authedUser.isAdmin;

if (!options.isAdmin)
return aRes.send(403, { status: 403, message: 'Not an admin.' });

exec('npm ls --json', function(aErr, aStdout, aStderr) {
if (aErr) return aRes.send(501, { status: 501, message: 'Not implemented.' });

aRes.json(JSON.parse(aStdout));
});
}

// View everything about a particular user
// This is mostly for debugging in production
exports.adminJsonView = function (aReq, aRes, aNext) {
Expand All @@ -139,22 +161,14 @@ exports.adminJsonView = function (aReq, aRes, aNext) {
if (!model)
return aRes.send(400, { status: 400, message: 'Invalid model.' });

if (model === 'NpmLs') {
require('child_process').exec('npm ls --json', function(aErr, aStdout, aStderr) {
if (aErr) return;
model.findOne({
_id: id
}, function (aErr, aObj) {
if (aErr || !aObj)
return aRes.send(404, { status: 404, message: 'Id doesn\'t exist.' });

aRes.json(JSON.parse(aStdout));
});
} else {
model.findOne({
_id: id
}, function (aErr, aObj) {
if (aErr || !aObj)
return aRes.send(404, { status: 404, message: 'Id doesn\'t exist.' });

aRes.json(aObj);
});
}
aRes.json(aObj);
});
};

// Make changes to users listed
Expand Down
2 changes: 1 addition & 1 deletion views/pages/adminPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<a href="/admin/api" class="list-group-item">
API Keys
</a>
<a href="/admin/json?model=NpmLs" class="list-group-item">
<a href="/admin/npmls" class="list-group-item">
npm ls --json
</a>
</div>
Expand Down

0 comments on commit 9f77511

Please sign in to comment.