Skip to content

Commit

Permalink
[ci none]
Browse files Browse the repository at this point in the history
Update jade
  • Loading branch information
richtera committed Sep 10, 2012
1 parent d185d5c commit 3ca3da8
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 48 deletions.
122 changes: 120 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,67 @@ var rootpath = process.cwd() + '/',
colors = require('colors'),
calipso = require(path.join(rootpath, 'lib/calipso')),
translate = require(path.join(rootpath, 'i18n/translate')),
logo = require(path.join(rootpath, 'logo'));
logo = require(path.join(rootpath, 'logo')),
everyauth = require("everyauth");

var conf = {
google: {
clientId: '3335216477.apps.googleusercontent.com',
clientSecret: 'PJMW_uP39nogdu0WpBuqMhtB'
}
, fb: {
appId: '111565172259433'
, appSecret: '85f7e0a0cc804886180b887c1f04a3c1'
}
, twit: {
consumerKey: 'JLCGyLzuOK1BjnKPKGyQ'
, consumerSecret: 'GNqKfPqtzOcsCtFbGTMqinoATHvBcy1nzCTimeA9M0'
}
};

everyauth.debug = true;

everyauth.everymodule
.findUserById( function (req, id, callback) {
var User = calipso.db.model('User');
User.findById(id, function (err, user) {
req.session.user = user;
callback(err, user);
});
});

function calipsoFindOrCreateUser(username, promise) {
var User = calipso.db.model('User');
function finishUser(sess, user) {
if (!sess._pending) return promise.fulfill(user);
return calipso.lib.user.createUserSession(sess._pending, null, user, function(err) {
if(err) { calipso.error("Error saving session: " + err); return promise.fail(err); }
promise.fulfill(user);
});
}

User.findOne({username:username}, function (err, user) {
if (err) return promise.fail(err);
if (user) return promise.fulfill(user);
var u = new User({
username: 'google:' + googleUser.email,
fullname: googleUser.name,
email: googleUser.email,
hash: 'external:auth'
});
u.roles = ['Guest']; // Todo - need to make sure guest role can't be deleted?

calipso.e.pre_emit('USER_CREATE',u);

u.save(function(err) {
if (err) return promise.fail(err);
calipso.e.post_emit('USER_CREATE',u);
// If not already redirecting, then redirect
finishUser(sess, u);
});
});
return promise;
}

// Local App Variables
var path = rootpath,
Expand Down Expand Up @@ -145,12 +205,70 @@ function bootApplication(next) {
var temporarySession = app.config.get('installed') ? {} : express.session({ secret: "installing calipso is great fun" });
temporarySession.tag = "session";
app.use(temporarySession);

// Create holders for theme dependent middleware
// These are here because they need to be in the connect stack before the calipso router
// THese helpers are re-used when theme switching.
app.mwHelpers = {};

calipso.auth = {password: app.config.get('server:authentication:password')};

var appId = app.config.get('server:authentication:facebookAppId');
var appSecret = app.config.get('server:authentication:facebookAppSecret');
if (appId && appSecret) {
calipso.auth.facebook = true;
everyauth
.facebook
.appId(appId)
.appSecret(appSecret)
.findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) {
console.log(fbUserMetadata);
})
.redirectPath('/');
}

var consumerKey = app.config.get('server:authentication:twitterConsumerKey');
var consumerSecret = app.config.get('server:authentication:twitterConsumerSecret');
if (consumerKey && consumerSecret) {
calipso.auth.twitter = true;
everyauth
.twitter
.consumerKey(consumerKey)
.consumerSecret(consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, twitUser) {
console.log(twitUser);
})
.redirectPath('/');
}

var clientId = app.config.get('server:authentication:googleClientId');
var clientSecret = app.config.get('server:authentication:googleClientSecret');
if (clientId && clientSecret) {
calipso.auth.google = true;
everyauth.google
.appId(clientId)
.appSecret(clientSecret)
.scope('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
.getSession( function (req) {
if (!req.session)
req.session = { _pending: req };
return req.session;
})
.findOrCreateUser( function (sess, accessToken, extra, googleUser) {
googleUser.refreshToken = extra.refresh_token;
googleUser.expiresIn = extra.expires_in;

var promise = this.Promise();

return calipsoFindOrCreateUser('google:' + googleUser.email, promise);
})
.redirectPath('/');
}

app.use(everyauth.middleware());

console.log(calipso.auth);

// Load placeholder, replaced later
if(app.config.get('libraries:stylus:enable')) {
console.log('enabling stylus');
Expand Down
72 changes: 72 additions & 0 deletions modules/core/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,78 @@ function coreConfig(req, res, template, block, next) {
}
]
},
{
id:'form-section-authentication',
label:'Authentication',
fields:[
{ label:'Password Login and Registration',
legend:'Password Login and Registration',
type:'fieldset',
fields: [
{
label:'Enable password authentication and registration',
type:'checkbox',
name:'server:authentication:password',
defaultValue:true
}
]
},
{ label:'Facebook Authentication',
legend:'Set this information to enable Facebook Authentication',
type:'fieldset',
fields:[
{
label:'AppId',
description:'Set AppId and Secret to enable facebook authentication',
name:'server:authentication:facebookAppId',
type:'password'
},
{
label:'AppSecret',
description:'AppSecret for this application to allow facebook authentication',
name:'server:authentication:facebookAppSecret',
type:'password'
}
]
},
{ label:'Google Authentication',
legend:'Set this information to enable Google Authentication',
type:'fieldset',
fields: [
{
label:'ClientId',
description:'Set ClientId and ClientSecret to enable google authentication',
name:'server:authentication:googleClientId',
type:'password'
},
{
label:'ClientSecret',
description:'ClientSecret for this application to allow google authentication',
name:'server:authentication:googleClientSecret',
type:'password'
}
]
},
{ label:'Twitter Authentication',
legend:'Set this information to enable Twitter Authentication',
type:'fieldset',
fields: [
{
label:'Twitter ConsumerKey',
description:'Set ConsumerKey and ConsumerSecret to allow twitter authentication',
name:'server:authentication:twitterConsumerKey',
type:'password'
},
{
label:'Twitter ConsumerSecret',
description:'ConsumerSecret for this application to allow twitter authentication',
name:'server:authentication:twitterConsumerSecret',
type:'password'
}
]
}
]
},
{
id:'form-section-theme',
label:'Theme',
Expand Down
44 changes: 31 additions & 13 deletions modules/core/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var rootpath = process.cwd() + '/',
path = require('path'),
calipso = require(path.join(rootpath, 'lib/calipso')),
roles = require('./user.roles'),
Query = require("mongoose").Query;
Query = require("mongoose").Query,
everyauth = require("everyauth");

exports = module.exports = {
init: init,
Expand Down Expand Up @@ -44,7 +45,7 @@ function init(module, app, next) {
calipso.e.addEvent('USER_UNLOCK');
calipso.e.addEvent('USER_LOGIN');
calipso.e.addEvent('USER_LOGOUT');

// Define permissions
calipso.permission.Helper.addPermission("admin:user","Users",true);
calipso.permission.Helper.addPermission("admin:user:register","Register other users.");
Expand Down Expand Up @@ -169,17 +170,31 @@ function userDisplay(req, username, next) {

}

function userFields() {
var fields = [
{label:'Username', name:'user[username]', type:'text'},
{label:'Password', name:'user[password]', type:'password'}
];
if (true /* enable google */) {
fields.push({name:'google',text:'Use Google Login', type:'link', href:'/auth/google', cls:'googleicon'});
}
if (true /* enable twitter */) {
fields.push({name:'twitter',text:'Use Twitter Login', type:'link', href:'/auth/facebook', cls:'facebookicon'});
}
if (true /* enable facebook */) {
fields.push({name:'facebook',text:'Use Facebook Login', type:'link', href:'/auth/twitter', cls:'twittericon'});
}
return fields;
}

/**
* Login form
*/
function loginForm(req, res, template, block, next) {

var fields = userFields();
var userForm = {
id:'login-form',cls:'login',title:'Log In',type:'form',method:'POST',action:'/user/login',
fields:[
{label:'Username', name:'user[username]', type:'text'},
{label:'Password', name:'user[password]', type:'password'}
],
fields:fields,
buttons:[
{name:'submit', type:'submit', value:'Login'},
{name:'register', type:'link', href:'/user/register', value:'Register'}
Expand All @@ -196,13 +211,10 @@ function loginForm(req, res, template, block, next) {
* Login form
*/
function loginPage(req, res, template, block, next) {

var fields = userFields();
var userForm = {
id:'login-form',cls:'login',title:'Log In',type:'form',method:'POST',action:'/user/login',
fields:[
{label:'Username', name:'user[username]', type:'text'},
{label:'Password', name:'user[password]', type:'password'}
],
fields:fields,
buttons:[
{name:'submit', type:'submit', value:'Login'}
]
Expand Down Expand Up @@ -329,6 +341,7 @@ function updateUserForm(req, res, template, block, next) {
var User = calipso.db.model('User');
var username = req.moduleParams.username;
var roleSection = 3; // Update if changing sections
var passwordSection = 1; // Update if changing sections

if(isAdmin) {
res.menu.adminToolbar.addMenuItem(req, {name:'Return',path:'return',url:'/user/profile/'+username,description:'Show user ...',security:[]});
Expand Down Expand Up @@ -417,6 +430,10 @@ function updateUserForm(req, res, template, block, next) {
// remove the section
delete userForm.sections[roleSection];
}
console.log(u);
if (u.hash === 'external:auth') {
delete userForm.sections[passwordSection];
}

var values = {user:u};

Expand Down Expand Up @@ -756,7 +773,8 @@ function logoutUser(req, res, template, block, next) {
if(req.session && req.session.user) {

var User = calipso.db.model('User');

if (req.logout)
req.logout();
User.findOne({username:req.session.user.username}, function(err, u) {

req.session.user = null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"formidable": "1.0.x",
"connect": "2.3.x",
"ejs": "0.6.x",
"jade": "0.20.x",
"jade": "0.27.x",
"stylus": "0.28.x",
"underscore":"1.3.x",
"mongoose": "2.5.x",
Expand Down
Loading

0 comments on commit 3ca3da8

Please sign in to comment.