From b0b5160de44b09d6d95a81e13102e0088e8b2e71 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 22 Jul 2014 10:03:52 -0400 Subject: [PATCH] closed #4 --- library.js | 52 ++++++++++++++++++++++++++++++++++++++-------------- plugin.json | 4 ++-- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/library.js b/library.js index 82e557e..b4827f5 100644 --- a/library.js +++ b/library.js @@ -2,6 +2,7 @@ "use strict"; var User = module.parent.require('./user'), + Groups = module.parent.require('./groups'), meta = module.parent.require('./meta'), db = module.parent.require('../src/database'), passport = module.parent.require('passport'), @@ -21,13 +22,15 @@ var OAuth = {}; - OAuth.init = function(app, middleware, controllers) { + OAuth.init = function(app, middleware, controllers, callback) { function render(req, res, next) { res.render('admin/plugins/sso-oauth', {}); } app.get('/admin/plugins/sso-oauth', middleware.admin.buildHeader, render); app.get('/api/admin/plugins/sso-oauth', render); + + callback(); }; OAuth.getStrategy = function(strategies, callback) { @@ -104,18 +107,22 @@ var profile = { provider: 'generic' }; // Alter this section to include whatever data is necessary - // NodeBB requires the following: id, displayName, emails, e.g.: + // NodeBB *requires* the following: id, displayName, emails. + // Everything else is optional. profile.id = json.id; profile.displayName = json.name; profile.emails = [{ value: json.email }]; + // Do you want to automatically make somebody an admin? This block might help you do that... + // profile.isAdmin = json.isAdmin ? true : false; + // Find out what is available by uncommenting this line: // console.log(json); // Delete or comment out the next TWO (2) lines when you are ready to proceed - // console.log('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n==='); - // return done(new Error('Congrats! So far so good -- please see server log for details')); + process.stdout.write('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n==='); + return done(new Error('Congrats! So far so good -- please see server log for details')); done(null, profile); } catch(e) { @@ -126,7 +133,12 @@ } passport.use('Generic OAuth', new passportOAuth(opts, function(token, secret, profile, done) { - OAuth.login(profile.id, profile.displayName, profile.emails[0].value, function(err, user) { + OAuth.login({ + oAuthid: profile.id, + handle: profile.displayName, + email: profile.emails[0].value, + isAdmin: profile.isAdmin + }, function(err, user) { if (err) { return done(err); } @@ -150,8 +162,8 @@ }); }; - OAuth.login = function(oAuthid, handle, email, callback) { - OAuth.getUidByOAuthid(oAuthid, function(err, uid) { + OAuth.login = function(payload, callback) { + OAuth.getUidByOAuthid(payload.oAuthid, function(err, uid) { if(err) { return callback(err); } @@ -165,20 +177,32 @@ // New User var success = function(uid) { // Save provider-specific information to the user - User.setUserField(uid, 'oAuthid', oAuthid); - db.setObjectField('oAuthid:uid', oAuthid, uid); - callback(null, { - uid: uid - }); + User.setUserField(uid, 'oAuthid', payload.oAuthid); + db.setObjectField('oAuthid:uid', payload.oAuthid, uid); + + if (payload.isAdmin) { + Groups.join('administrators', uid, function(err) { + callback(null, { + uid: uid + }); + }); + } else { + callback(null, { + uid: uid + }); + } }; - User.getUidByEmail(email, function(err, uid) { + User.getUidByEmail(payload.email, function(err, uid) { if(err) { return callback(err); } if (!uid) { - User.create({username: handle, email: email}, function(err, uid) { + User.create({ + username: payload.handle, + email: payload.email + }, function(err, uid) { if(err) { return callback(err); } diff --git a/plugin.json b/plugin.json index ead46d0..b31ebad 100644 --- a/plugin.json +++ b/plugin.json @@ -5,10 +5,10 @@ "url": "https://github.com/julianlam/nodebb-plugin-sso-oauth", "library": "./library.js", "hooks": [ - { "hook": "action:app.load", "method": "init" }, + { "hook": "filter:app.load", "method": "init" }, { "hook": "filter:auth.init", "method": "getStrategy" }, { "hook": "filter:admin.header.build", "method": "addMenuItem" } ], "templates": "./templates", - "minver": "0.4.0" + "minver": "0.5.0" }