more cleanup to user routes

v1.18.x
Baris Soner Usakli 11 years ago
parent 905a99e638
commit b82a5461ce

@ -45,13 +45,12 @@ var fs = require('fs'),
app.namespace('/user', function () { app.namespace('/user', function () {
function createRoute(routeName, path, templateName) { function createRoute(routeName, path, templateName, access) {
app.get(routeName, function(req, res, next) {
if (!req.params.userslug) { function isAllowed(req, res, next) {
return next(); var callerUID = req.user ? parseInt(req.user.uid, 10) : 0;
}
if (!req.user && (path === '/favourites' || !!parseInt(meta.config.privateUserInfo, 10))) { if (!callerUID && !!parseInt(meta.config.privateUserInfo, 10)) {
return res.redirect('/403'); return res.redirect('/403');
} }
@ -64,50 +63,12 @@ var fs = require('fs'),
return res.redirect('/404'); return res.redirect('/404');
} }
app.build_header({ if (parseInt(uid, 10) === callerUID) {
req: req, return next();
res: res
}, function (err, header) {
if(err) {
return next(err);
}
res.send(header + app.create_route('user/' + req.params.userslug + path, templateName) + templates['footer']);
});
});
})
}
createRoute('/:userslug', '', 'account');
createRoute('/:userslug/following', '/following', 'following');
createRoute('/:userslug/followers', '/followers', 'followers');
createRoute('/:userslug/favourites', '/favourites', 'favourites');
createRoute('/:userslug/posts', '/posts', 'accountposts');
app.get('/:userslug/edit', function (req, res, next) {
if (!req.user) {
return res.redirect('/403');
}
user.getUserField(req.user.uid, 'userslug', function (err, userslug) {
function done() {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']);
});
}
if(err || !userslug) {
return next(err);
}
if (userslug === req.params.userslug) {
return done();
} }
user.isAdministrator(req.user.uid, function(err, isAdmin) { if (req.path.indexOf('/edit') !== -1) {
user.isAdministrator(callerUID, function(err, isAdmin) {
if(err) { if(err) {
return next(err); return next(err);
} }
@ -116,30 +77,36 @@ var fs = require('fs'),
return res.redirect('/403'); return res.redirect('/403');
} }
done(); next();
});
}); });
} else if (req.path.indexOf('/settings') !== -1 || req.path.indexOf('/favourites') !== -1) {
res.redirect('/403')
} else {
next();
}
}); });
app.get('/:userslug/settings', function (req, res) {
if (!req.user) {
return res.redirect('/403');
} }
user.getUserField(req.user.uid, 'userslug', function (err, userslug) { app.get(routeName, isAllowed, function(req, res, next) {
if (req.params.userslug && userslug === req.params.userslug) {
app.build_header({ app.build_header({
req: req, req: req,
res: res res: res
}, function (err, header) { }, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']); if(err) {
}) return next(err);
} else {
return res.redirect('/404');
} }
res.send(header + app.create_route('user/' + req.params.userslug + path, templateName) + templates['footer']);
}); });
}); });
}
createRoute('/:userslug', '', 'account');
createRoute('/:userslug/following', '/following', 'following');
createRoute('/:userslug/followers', '/followers', 'followers');
createRoute('/:userslug/favourites', '/favourites', 'favourites');
createRoute('/:userslug/posts', '/posts', 'accountposts');
createRoute('/:userslug/edit', '/edit', 'accountedit');
createRoute('/:userslug/settings', '/settings', 'accountsettings');
app.post('/uploadpicture', function (req, res) { app.post('/uploadpicture', function (req, res) {
if (!req.user) { if (!req.user) {
@ -313,7 +280,6 @@ var fs = require('fs'),
}); });
}); });
}); });
}); });
} }
@ -512,8 +478,6 @@ var fs = require('fs'),
}); });
} }
function getUsersSortedByJoinDate(req, res) { function getUsersSortedByJoinDate(req, res) {
user.getUsers('users:joindate', 0, 49, function (err, data) { user.getUsers('users:joindate', 0, 49, function (err, data) {
res.json({ res.json({
@ -677,9 +641,7 @@ var fs = require('fs'),
callback(null, userData); callback(null, userData);
}); });
}); });
} }
}; };
}(exports)); }(exports));

Loading…
Cancel
Save