From b114d12c7f98179082803eb86fb051b4507279b7 Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Wed, 22 Jun 2011 09:36:10 -0700
Subject: [PATCH] cancel account

---
 authority/server/db.js         | 13 +++++++++++++
 authority/server/wsapi.js      | 15 +++++++++++++++
 authority/static/css/style.css |  8 ++++++++
 authority/static/manage.html   | 12 ++++++++++++
 4 files changed, 48 insertions(+)

diff --git a/authority/server/db.js b/authority/server/db.js
index 4d362888e..2c4275d09 100644
--- a/authority/server/db.js
+++ b/authority/server/db.js
@@ -292,4 +292,17 @@ exports.removeEmail = function(authenticated_email, email, cb) {
             else cb();
         });
     });
+};
+
+exports.cancelAccount = function(authenticated_email, cb) {
+    emailToUserID(authenticated_email, function(user_id) {
+        executeTransaction([
+            [ "delete from emails where user = ?", [ user_id ] ] ,
+            [ "delete from keys where email in (select address from emails where user = ?)", [ user_id ] ],
+            [ "delete from users where id = ?", [ user_id ] ],
+        ], function (error) {
+            if (error) cb(error);
+            else cb();
+        });
+    });
 };
\ No newline at end of file
diff --git a/authority/server/wsapi.js b/authority/server/wsapi.js
index 0bfb38b9e..1a900b85f 100644
--- a/authority/server/wsapi.js
+++ b/authority/server/wsapi.js
@@ -168,6 +168,21 @@ exports.remove_email = function(req, resp) {
         }});
 };
 
+exports.account_cancel = function(req, resp) {
+    // this should really be POST
+    if (!checkAuthed(req, resp)) return;
+    
+    logRequest("account_cancel");
+    
+    db.cancelAccount(req.session.authenticatedUser, function(error) {
+        if (error) {
+            console.log("error cancelling account : " + error.toString());
+            httputils.badRequest(resp, error.toString());
+        } else {
+            httputils.jsonResponse(resp, true);            
+        }});
+};
+
 exports.set_key = function (req, resp) {
   var urlobj = url.parse(req.url, true);
   var getArgs = urlobj.query;
diff --git a/authority/static/css/style.css b/authority/static/css/style.css
index 589531c73..9f52ecb71 100644
--- a/authority/static/css/style.css
+++ b/authority/static/css/style.css
@@ -195,6 +195,14 @@ pre code {
   font-weight:bold;
   margin-top:32px;
 }
+
+#cancelaccount {
+  font-size: 1.0em; 
+  width: 500px; 
+  margin: auto; 
+  margin-top:35px;
+}
+
 .email {
   display:inline-block;
 }
diff --git a/authority/static/manage.html b/authority/static/manage.html
index 0a3be8403..2eb38b793 100644
--- a/authority/static/manage.html
+++ b/authority/static/manage.html
@@ -21,6 +21,9 @@
     </div>
     <div id="emailList">
     </div>
+    <div id="cancelaccount">
+      You may, at any time, <a href="#" id="cancellink">cancel your account</a>.
+    </div>
   </div>
   <div class="footer">
     <div>
@@ -46,6 +49,15 @@ function display_saved_ids()
     emails = JSON.parse(window.localStorage.emails);
   }
 
+  $('#cancellink').click(function() {
+    if (confirm('Are you sure you want to cancel your account?')) {
+      $.post("/wsapi/account_cancel", {}, function(result) {
+        window.localStorage.emails = null;
+        document.location="/";
+      });
+    }
+  });
+
   $("#emailList").empty();
   _(emails).each(function(data, e) {
       var block = $("<div>").addClass("emailblock");
-- 
GitLab