Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Route for badge image.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Oct 10, 2012
1 parent 27df77b commit 5d7e7fb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
16 changes: 15 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ app.configure(function () {
app.use(middleware.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(user.requireAuth({
whitelist: ['/login', '/logout'],
whitelist: [
'/login',
'/logout',
'/badge/*'
],
redirectTo: '/login'
}));
app.use(app.router);
Expand Down Expand Up @@ -74,6 +78,16 @@ app.get('/admin/behavior', admin.newBehaviorForm);

// create a new behavior
app.post('/admin/behavior', behavior.create);

// get the badge image
app.get('/badge/image/:shortname.png', [
badge.findByShortname({
container: 'param',
field: 'shortname',
required: true
})
], badge.image);

app.get('/login', user.login);
app.post('/login', user.login);
app.get('/logout', user.logout);
Expand Down
6 changes: 6 additions & 0 deletions routes/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ exports.removeBehavior = function removeBehavior(req, res) {
});
};

exports.image = function image(req, res) {
var badge = req.badge;
res.type('image/png');
res.send(badge.image);
};

exports.findByShortname = function (options) {
var required = !!options.required;

Expand Down
15 changes: 13 additions & 2 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@ exports.logout = function logout(req, res) {
};

exports.requireAuth = function requireAuth(options) {
var whitelist = options.whitelist || [];
var whitelist = (options.whitelist || []).map(function (entry) {
entry = entry.replace('*', '.*?');
return RegExp('^' + entry + '$');
});
function isExempt(path) {
var i = whitelist.length;
while (i--) {
if (whitelist[i].test(path))
return true
}
return false;
}
return function (req, res, next) {
var path = req.path;
var user = req.session.user;
if (whitelist.indexOf(path) > -1)
if (isExempt(path))
return next();
if (!user || !userIsAuthorized(user))
return res.redirect(options.redirectTo + '?path=' + path);
Expand Down

0 comments on commit 5d7e7fb

Please sign in to comment.