Permalink
Browse files

Frontend for removing users [closes #45]

  • Loading branch information...
1 parent f4aa11f commit 9f27c68764d53cbc4c0f60b117186f2f0b1612d7 @brianloveswords brianloveswords committed Nov 11, 2012
Showing with 82 additions and 1 deletion.
  1. +5 −0 app.js
  2. +11 −1 routes/admin.js
  3. +38 −0 routes/user.js
  4. +28 −0 views/admin/user-list.html
View
5 app.js
@@ -137,6 +137,11 @@ app.get('/login', admin.login);
app.post('/login', user.login);
app.post('/logout', user.logout);
+app.get('/admin/users',[
+ user.findAll()
+], admin.userList);
+
+app.delete('/admin/users', user.deleteInstancesByEmail);
// API endpoints
// -------------
View
@@ -128,4 +128,14 @@ exports.showFlushDbForm = function (req, res) {
user: req.session.user,
csrf: req.session._csrf
});
-};
+};
+
+exports.userList = function userList(req, res, next) {
+ return res.render('admin/user-list.html', {
+ page: 'user-list',
+ issuer: req.issuer,
+ user: req.session.user,
+ csrf: req.session._csrf,
+ users: req.users
+ });
+};
View
@@ -1,3 +1,4 @@
+var BadgeInstance = require('../models/badge-instance.js');
var env = require('../lib/environment');
var persona = require('../lib/persona');
var util = require('util');
@@ -21,6 +22,15 @@ exports.logout = function logout(req, res) {
});
};
+exports.deleteInstancesByEmail = function deleteInstancesByEmail(req, res, next) {
+ var form = req.body;
+ var email = form.email;
+ BadgeInstance.deleteAllByUser(email, function (err, instances) {
+ if (err) return next(err);
+ return res.redirect('back');
+ });
+};
+
exports.requireAuth = function requireAuth(options) {
var whitelist = (options.whitelist || []).map(function (entry) {
if (typeof entry === 'string') {
@@ -48,6 +58,34 @@ exports.requireAuth = function requireAuth(options) {
};
};
+function getElem(key) {
+ return function (obj) { return obj[key] }
+}
+
+exports.findAll = function findAll(options) {
+ return function (req, res, next) {
+ BadgeInstance.find(function (err, instances) {
+ if (err) return next(err);
+ var users = instances.reduce(function (users, instance) {
+ var user = users[instance.user];
+ if (user)
+ user.push(instance)
+ else
+ users[instance.user] = [instance];
+ return users;
+ }, {});
+ req.users = Object.keys(users).map(function (email) {
+ var instances = users[email];
+ return {
+ email: email,
+ badges: instances.map(getElem('badge'))
+ };
+ });
+ return next();
+ });
+ };
+};
+
function userIsAuthorized(email) {
var admins = env.get('admins');
var authorized = false;
View
@@ -0,0 +1,28 @@
+{% extends "admin/base.html" %}
+{% block main %}
+<h1>Badge Earners</h1>
+
+{% if not users.length %}
+<h2>No badges awarded</h2>
+
+{% else %}
+<ul>
+ {% for earner in users %}
+ <li>
+ <form method="post">
+ <input type="hidden" name="_method" value="delete">
+ <input type="hidden" name="email" value="{{ earner.email }}">
+ <input type="hidden" name="csrf" value="{{ csrf }}">
+ <dt>{{ earner.email }}</dt>
+ <dd>
+ {{ earner.badges.join(', ') }}
+ <br><input type="submit" class="btn btn-danger btn-small" value="Delete User Data">
+ </dd>
+ </form>
+ </li>
+ {% endfor %}
+</ul>
+
+{% endif %}
+
+{% endblock %}

0 comments on commit 9f27c68

Please sign in to comment.