From 8689c85b54efccbccfa216c800522e5ba1a79dd1 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 3 Feb 2015 17:41:54 -0500 Subject: [PATCH] add error handling when granting admin and moderator privileges --- .../javascripts/admin/models/admin_user.js | 31 +++++++++++++++++-- config/locales/client.en.yml | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/models/admin_user.js b/app/assets/javascripts/admin/models/admin_user.js index 9c1e397e662..95643db7788 100644 --- a/app/assets/javascripts/admin/models/admin_user.js +++ b/app/assets/javascripts/admin/models/admin_user.js @@ -98,7 +98,21 @@ Discourse.AdminUser = Discourse.User.extend({ this.set('admin', true); this.set('can_grant_admin', false); this.set('can_revoke_admin', true); - Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_admin", {type: 'PUT'}); + var self = this; + + Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_admin", {type: 'PUT'}) + .then(null, function(e) { + self.set('admin', false); + self.set('can_grant_admin', true); + self.set('can_revoke_admin', false); + + var error; + if (e.responseJSON && e.responseJSON.error) { + error = e.responseJSON.error; + } + error = error || I18n.t('admin.user.grant_admin_failed', { error: "http: " + e.status + " - " + e.body }); + bootbox.alert(error); + }); }, // Revoke the user's moderation access @@ -113,7 +127,20 @@ Discourse.AdminUser = Discourse.User.extend({ this.set('moderator', true); this.set('can_grant_moderation', false); this.set('can_revoke_moderation', true); - Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_moderation", {type: 'PUT'}); + var self = this; + Discourse.ajax("/admin/users/" + (this.get('id')) + "/grant_moderation", {type: 'PUT'}) + .then(null, function(e) { + self.set('moderator', false); + self.set('can_grant_moderation', true); + self.set('can_revoke_moderation', false); + + var error; + if (e.responseJSON && e.responseJSON.error) { + error = e.responseJSON.error; + } + error = error || I18n.t('admin.user.grant_moderation_failed', { error: "http: " + e.status + " - " + e.body }); + bootbox.alert(error); + }); }, refreshBrowsers: function() { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8876766b179..6988d9a55ef 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2048,6 +2048,8 @@ en: suspended_explanation: "A suspended user can't log in." block_explanation: "A blocked user can't post or start topics." trust_level_change_failed: "There was a problem changing the user's trust level." + grant_admin_failed: "There was a problem granting admin privileges." + grant_moderation_failed: "There was a problem granting moderation privileges." suspend_modal_title: "Suspend User" trust_level_2_users: "Trust Level 2 Users" trust_level_3_requirements: "Trust Level 3 Requirements"