diff --git a/public/templates/admin/users.tpl b/public/templates/admin/users.tpl
index 341306f727..71a802b4a8 100644
--- a/public/templates/admin/users.tpl
+++ b/public/templates/admin/users.tpl
@@ -1,6 +1,7 @@
Users
+Download CSV
- Latest Users
diff --git a/src/routes/admin.js b/src/routes/admin.js
index 6a16b23710..8577e8aae3 100644
--- a/src/routes/admin.js
+++ b/src/routes/admin.js
@@ -159,6 +159,14 @@ var nconf = require('nconf'),
uploadImage(filename, req, res);
});
+
+ app.get('/users/csv', function(req, res) {
+ user.getUsersCSV(function(err, data) {
+ res.attachment('users.csv');
+ res.setHeader('Content-Type', 'text/csv');
+ res.end(data);
+ });
+ });
});
function uploadImage(filename, req, res) {
diff --git a/src/user.js b/src/user.js
index cfc42f8564..5914107add 100644
--- a/src/user.js
+++ b/src/user.js
@@ -409,6 +409,30 @@ var bcrypt = require('bcrypt'),
});
};
+ // thanks to @akhoury
+ User.getUsersCSV = function(callback) {
+ var csvContent = "";
+
+ db.getObjectValues('username:uid', function(err, uids) {
+ async.each(uids, function(uid, next) {
+ User.getUserFields(uid, ['email', 'username'], function(err, userData) {
+ if(err) {
+ return next(err);
+ }
+
+ csvContent += userData.email+ ',' + userData.username + ',' + uid +'\n';
+ next();
+ });
+ }, function(err) {
+ if (err) {
+ throw err;
+ }
+
+ callback(err, csvContent);
+ });
+ });
+ }
+
User.search = function(username, callback) {
if (!username) {
callback([]);