diff --git a/data/config.ini b/data/config.ini index 7aafbecd..56393c8e 100644 --- a/data/config.ini +++ b/data/config.ini @@ -9,10 +9,10 @@ minPasswordLength = 5 [security.privileges] anonymous = register, viewUser -regularUser = listUsers, viewUser -powerUser = listUsers, viewUser -moderator = listUsers, viewUser -administrator = listUsers, viewUser +regularUser = listUsers, viewUser, deleteOwnAccount +powerUser = listUsers, viewUser, deleteOwnAccount +moderator = listUsers, viewUser, deleteOwnAccount +administrator = listUsers, viewUser, deleteOwnAccount, deleteUsers [users] minUserNameLength = 1 diff --git a/public_html/js/Presenters/UserPresenter.js b/public_html/js/Presenters/UserPresenter.js index 8f7e7cae..987468a4 100644 --- a/public_html/js/Presenters/UserPresenter.js +++ b/public_html/js/Presenters/UserPresenter.js @@ -14,6 +14,7 @@ App.Presenters.UserPresenter = function( var $messages = $el; var template; var accountSettingsTemplate; + var accountRemovalTemplate; var browsingSettingsTemplate; var user; var userName; @@ -25,11 +26,18 @@ App.Presenters.UserPresenter = function( promise.waitAll( util.promiseTemplate('user'), util.promiseTemplate('account-settings'), + util.promiseTemplate('account-removal'), util.promiseTemplate('browsing-settings'), api.get('/users/' + userName)) - .then(function(userHtml, accountSettingsHtml, browsingSettingsHtml, response) { + .then(function( + userHtml, + accountSettingsHtml, + accountRemovalHtml, + browsingSettingsHtml, + response) { template = _.template(userHtml); accountSettingsTemplate = _.template(accountSettingsHtml); + accountRemovalTemplate = _.template(accountRemovalHtml); browsingSettingsTemplate = _.template(browsingSettingsHtml); user = response.json; @@ -41,12 +49,37 @@ App.Presenters.UserPresenter = function( } function render() { - $el.html(template({user: user})); - $el.find('.browsing-settings').html(browsingSettingsTemplate({user: user})); - $el.find('.account-settings').html(accountSettingsTemplate({user: user})); + var context = { + user: user, + canDeleteAccount: auth.hasPrivilege('deleteAccounts') || + (auth.hasPrivilege('deleteOwnAccount') && auth.getCurrentUser().name == userName), + }; + $el.html(template(context)); + $el.find('.browsing-settings').html(browsingSettingsTemplate(context)); + $el.find('.account-settings').html(accountSettingsTemplate(context)); + $el.find('.account-removal').html(accountRemovalTemplate(context)); + $el.find('.account-removal form').submit(accountRemovalFormSubmitted); $messages = $el.find('.messages'); }; + function accountRemovalFormSubmitted(e) { + e.preventDefault(); + $messages = $el.find('.account-removal .messages'); + messagePresenter.hideMessages($messages); + if (!$el.find('.account-removal input[name=confirmation]:visible').prop('checked')) { + messagePresenter.showError($messages, 'Must confirm to proceed.'); + return; + } + api.delete('/users/' + user.name) + .then(function() { + auth.logout(); + var $messageDiv = messagePresenter.showInfo($messages, 'Account deleted. Back to main page'); + $messageDiv.find('a').click(mainPageLinkClicked); + }).fail(function(response) { + messagePresenter.showError($messages, response.json && response.json.error || response); + }); + } + return { init: init, render: render diff --git a/public_html/templates/account-removal.tpl b/public_html/templates/account-removal.tpl new file mode 100644 index 00000000..7792b45a --- /dev/null +++ b/public_html/templates/account-removal.tpl @@ -0,0 +1,21 @@ +
diff --git a/public_html/templates/user-list.tpl b/public_html/templates/user-list.tpl index f009f657..f7b5e217 100644 --- a/public_html/templates/user-list.tpl +++ b/public_html/templates/user-list.tpl @@ -16,7 +16,7 @@ <% _.each(userList, function(user) { %>