Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
merge development, add message middleware, move more things into config
Browse files Browse the repository at this point in the history
  • Loading branch information
rchadwic committed Nov 8, 2016
1 parent dfabbeb commit 64d3e8d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 23 deletions.
32 changes: 29 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var hoganExpress = require('hogan-express');
require('pretty-error').start();
var mongoose = require('mongoose');
var DAL = require("./server/DAL.js").DAL;
var config;
async.series([

function loadConfig(cb)
Expand Down Expand Up @@ -53,10 +54,16 @@ async.series([
}
else return cb();
},
function loadConfig(cb)
{
//serve static files
config = require("./server/config.js").config;
cb();
},
function loadDB(cb)
{

mongoose.connect('mongodb://localhost/xapi-launch');
mongoose.connect(config.connectionString);
var db = mongoose.connection;
db.once('open', function(err)
{
Expand All @@ -68,8 +75,7 @@ async.series([
}
], function startServer()
{
//serve static files
var config = require("./server/config.js").config;


app.use('/static', express.static('public'));
app.use(require("body-parser").json());
Expand Down Expand Up @@ -99,6 +105,26 @@ async.series([
res.locals.user = req.user;
next();
})

//hook up a nice function to allow a prettier string to be sent for errors and such

app.use(function(req, res, next)
{
res.message = function(title, message)
{
if (!message)
{
message = title;
title = null;
}
res.render("message",
{
title: title,
message: message
});
}
next();
});
//setup various routes
require('./server/users.js').setup(app, DB);
require('./server/xapi.js').setup(app, DB);
Expand Down
7 changes: 6 additions & 1 deletion config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"publicSigningKey" : "", // a full RSA public key. Should include the -----BEGIN PUBLIC KEY----- header and footer. Be sure that you include \n chars for newlines
"privateSigningKey" : "", //the associated privte key. Also includes header and footer and newlines.
"demoModeUser":"rchadwic@gmail.com", //the user email of the account that is always logged in during demo mode
"demoMode":true //enable or disable demo mode
"demoMode":true, //enable or disable demo mode
"email_user":null, //the username of the outgoing email server
"email_server":null, //the address of the outgoing email server
"email_pass":null, //the password on the outgoing email server
"connectionString":null //override the default localhost database connnection

}
8 changes: 4 additions & 4 deletions server/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.setup = function(app, DAL)
function mustBeAdmin(req, res, next)
{
if (req.user !== require("./users.js").adminUser)
return res.status(401).send("must be admin");
return res.status(401).message("must be admin");
else
next();
}
Expand All @@ -26,7 +26,7 @@ exports.setup = function(app, DAL)
multi: true
}, function(err, numRemoved)
{
res.status(200).send("DB clear");
res.status(200).message("DB clear");
});
});
})
Expand All @@ -45,7 +45,7 @@ exports.setup = function(app, DAL)
var user = req.user;
if (user && user.email == config.admin_email)
{
return res.send(strings.not_admin);
return res.message(strings.not_admin);
}
next();
}
Expand All @@ -64,7 +64,7 @@ exports.setup = function(app, DAL)
}
else
{
res.send("User not found");
res.message("User not found");
}
})
}
Expand Down
11 changes: 11 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ if(exports.config.host[exports.config.host.length -1] == '/')
}


if(!exports.config.connectionString)
{
console.log("Using default connection string")
exports.config.connectionString = 'mongodb://localhost/xapi-launch';
}

if(!exports.config.email_user || !exports.config.email_server || !exports.config.email_pass)
{
console.log("Email is not set up in the config file. User accounts need to be verified manually by the admin.")
}

}catch(e)
{
exports.config = null;
Expand Down
8 changes: 4 additions & 4 deletions server/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ exports.setup = function(app, DAL)
{
if (!content)
{
return res.status(500).send("bad content key");
return res.status(500).message("bad content key");
}
i.contentURL = content.url;
i.contentTitle = content.title;
Expand Down Expand Up @@ -452,11 +452,11 @@ exports.setup = function(app, DAL)
DAL.getLaunchByGuid(req.params.key, function(err, launch)
{
if (content && !checkOwner(content,req.user))
return res.status(401).send("You are not the owner of this content.");
return res.status(401).message("You are not the owner of this content.");
if (launch && launch.email !== req.user.email)
return res.status(401).send("You are not the owner of this launch.");
return res.status(401).message("You are not the owner of this launch.");
if (!content && !launch)
return res.status(400).send("Unknown Identifier.");
return res.status(400).message("Unknown Identifier.");
var content_or_launch = content || launch;
console.log(req.lrsConfig.endpoint);
var resultLink = req.lrsConfig.endpoint + "statements?format=exact&activity=" + encodeURIComponent(content_or_launch.xapiForm().id) + "&related_activities=true&limit=1000";
Expand Down
10 changes: 5 additions & 5 deletions server/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ exports.setup = function(app, DAL)
DAL.getMedia(req.params.key, function(err, media)
{
if (!media)
return res.status(500).send("invalid media key");
return res.status(500).message("invalid media key");
if (!checkOwner(media,req.user))
{
return res.status(500).send("you are not the owner of this media");
return res.status(500).message("you are not the owner of this media");
}
media.remove(function(err)
{
Expand All @@ -135,10 +135,10 @@ exports.setup = function(app, DAL)
DAL.getMedia(req.params.key, function(err, media)
{
if (!media)
return res.status(500).send("invalid media key");
return res.status(500).message("invalid media key");
if (!checkOwner(media,req.user))
{
return res.status(500).send("you are not the owner of this media");
return res.status(500).message("you are not the owner of this media");
}
DAL.getAllMediaTypes(function(err, types)
{
Expand Down Expand Up @@ -223,7 +223,7 @@ exports.setup = function(app, DAL)
{
if (!content)
{
return res.status(500).send("bad content key");
return res.status(500).message("bad content key");
}
i.contentURL = content.url;
i.contentTitle = content.title;
Expand Down
12 changes: 6 additions & 6 deletions server/mediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.setup = function(app, DAL)
DAL.getMediaType(req.params.key || "", function(err, mediaType)
{
if (!mediaType)
return res.status(401).send("unknown media type");
return res.status(401).message("unknown media type");

res.locals.pageTitle = "Browse All " + mediaType.name + " Media";
DAL.getAllMediaByType(req.params.key, function(err, results)
Expand Down Expand Up @@ -86,7 +86,7 @@ exports.setup = function(app, DAL)
if (err)
return res.status(500).send(err)
if (!type)
return res.status(401).send("unknown type")
return res.status(401).message("unknown type")



Expand All @@ -102,9 +102,9 @@ exports.setup = function(app, DAL)
if (err)
return res.status(500).send(err)
if (!type)
return res.status(401).send("unknown type")
return res.status(401).message("unknown type")
if (!checkOwner(type,req.user))
return res.status(401).send("You are not the owner of this type")
return res.status(401).message("You are not the owner of this type")

req.formSchema = JSON.parse(require('fs').readFileSync("./server/forms/mediaType.json").toString());
req.formSchema.title = "Edit " + type.name;
Expand Down Expand Up @@ -148,9 +148,9 @@ exports.setup = function(app, DAL)
if (err)
return res.status(500).send(err)
if (!type)
return res.status(401).send("unknown type")
return res.status(401).message("unknown type")
if (!checkOwner(type,req.user))
return res.status(401).send("You are not the owner of this type")
return res.status(401).message("You are not the owner of this type")

type.remove(function(err)
{
Expand Down
1 change: 1 addition & 0 deletions views/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
{{#isAdmin}}
<hr>
<li ><a class="danger" href="/admin/clearDB/">Clear DB</a></li>
<li ><a class="danger" href="/admin/users/">Manage Users</a></li>
{{/isAdmin}}


Expand Down
16 changes: 16 additions & 0 deletions views/message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{>header}}

<script src='/static/scripts/dropzone.js'></script>
<script src='/static/scripts/sign.js'></script>
<link rel='stylesheet' href='/static/css/dropzone.css'/>
<link rel='stylesheet' href='/static/css/sign.css'/>
<br>
<br>
<br>
<div class='container'>
<h1>{{title}}</h1>
<p> {{{message}}}</p>
{{>footer}}
</div>


0 comments on commit 64d3e8d

Please sign in to comment.