From f35a44aeae389cf7c76064b67ee660f46e8c31d2 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 7 May 2013 21:58:34 -0400 Subject: [PATCH] Add ability for admins and mods to send another activation email to a user, to activate an account, and deactivate an account --- .../javascripts/admin/models/admin_user.js | 33 +++++++++++++++++++ .../admin/templates/user.js.handlebars | 31 +++++++++++++++++ app/controllers/admin/users_controller.rb | 14 ++++++++ app/models/user.rb | 15 +++++++++ app/serializers/admin_user_serializer.rb | 17 +++++++++- config/locales/client.en.yml | 7 ++++ config/routes.rb | 2 ++ lib/guardian.rb | 3 ++ 8 files changed, 121 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/models/admin_user.js b/app/assets/javascripts/admin/models/admin_user.js index 8d82e275d04..d9c2c8e475f 100644 --- a/app/assets/javascripts/admin/models/admin_user.js +++ b/app/assets/javascripts/admin/models/admin_user.js @@ -127,6 +127,39 @@ Discourse.AdminUser = Discourse.User.extend({ }); }, + activate: function() { + Discourse.ajax('/admin/users/' + this.id + '/activate', {type: 'PUT'}).then(function() { + // succeeded + window.location.reload(); + }, function(e) { + // failed + var error = Em.String.i18n('admin.user.activate_failed', { error: "http: " + e.status + " - " + e.body }); + bootbox.alert(error); + }); + }, + + deactivate: function() { + Discourse.ajax('/admin/users/' + this.id + '/deactivate', {type: 'PUT'}).then(function() { + // succeeded + window.location.reload(); + }, function(e) { + // failed + var error = Em.String.i18n('admin.user.deactivate_failed', { error: "http: " + e.status + " - " + e.body }); + bootbox.alert(error); + }); + }, + + sendActivationEmail: function() { + Discourse.ajax('/users/' + this.get('username') + '/send_activation_email').then(function() { + // succeeded + bootbox.alert( Em.String.i18n('admin.user.activation_email_sent') ); + }, function(e) { + // failed + var error = Em.String.i18n('admin.user.send_activation_email_failed', { error: "http: " + e.status + " - " + e.body }); + bootbox.alert(error); + }); + }, + deleteForbidden: function() { return (this.get('post_count') > 0); }.property('post_count'), diff --git a/app/assets/javascripts/admin/templates/user.js.handlebars b/app/assets/javascripts/admin/templates/user.js.handlebars index abd599bebe4..dd016ec6237 100644 --- a/app/assets/javascripts/admin/templates/user.js.handlebars +++ b/app/assets/javascripts/admin/templates/user.js.handlebars @@ -68,6 +68,37 @@ +
+
{{i18n admin.users.active}}
+
+ {{#if content.active}} + {{i18n yes_value}} + {{else}} + {{i18n no_value}} + {{/if}} +
+
+ {{#if content.active}} + {{#if content.can_deactivate}} + + {{/if}} + {{else}} + {{#if content.can_send_activation_email}} + + {{/if}} + {{#if content.can_activate}} + + {{/if}} + {{/if}} +
+
+
{{i18n admin.user.admin}}
{{content.admin}}
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 6d3df8a18ea..77ea2653a22 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -102,6 +102,20 @@ class Admin::UsersController < Admin::AdminController render nothing: true end + def activate + @user = User.where(id: params[:user_id]).first + guardian.ensure_can_activate!(@user) + @user.activate + render nothing: true + end + + def deactivate + @user = User.where(id: params[:user_id]).first + guardian.ensure_can_deactivate!(@user) + @user.deactivate + render nothing: true + end + def destroy user = User.where(id: params[:id]).first guardian.ensure_can_delete_user!(user) diff --git a/app/models/user.rb b/app/models/user.rb index fb9f8916dc9..93b7d21a8bb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -491,6 +491,21 @@ class User < ActiveRecord::Base email_tokens.where(email: email, confirmed: true).present? || email_tokens.empty? end + def activate + email_token = self.email_tokens.active.first + if email_token + EmailToken.confirm(email_token.token) + else + self.active = true + save + end + end + + def deactivate + self.active = false + save + end + def treat_as_new_topic_start_date duration = new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes case duration diff --git a/app/serializers/admin_user_serializer.rb b/app/serializers/admin_user_serializer.rb index dc13d502081..b9d23a474c6 100644 --- a/app/serializers/admin_user_serializer.rb +++ b/app/serializers/admin_user_serializer.rb @@ -21,7 +21,10 @@ class AdminUserSerializer < BasicUserSerializer :banned_at, :banned_till, :is_banned, - :ip_address + :ip_address, + :can_send_activation_email, + :can_activate, + :can_deactivate def is_banned object.is_banned? @@ -62,4 +65,16 @@ class AdminUserSerializer < BasicUserSerializer SiteSetting.must_approve_users end + def can_send_activation_email + scope.can_send_activation_email?(object) + end + + def can_activate + scope.can_activate?(object) + end + + def can_deactivate + scope.can_deactivate?(object) + end + end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index f7ba5a66e78..32023e52283 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -995,6 +995,13 @@ en: delete_confirm: "Are you SURE you want to permanently delete this user from the site? This action is permanent!" deleted: "The user was deleted." delete_failed: "There was an error deleting that user. Make sure all posts are deleted before trying to delete the user." + send_activation_email: "Send Activation Email" + activation_email_sent: "An activation email has been sent." + send_activation_email_failed: "There was a problem sending another activation email." + activate: "Activate Account" + activate_failed: "There was a problem activating the user." + deactivate_account: "Deactivate Account" + deactivate_failed: "There was a problem deactivating the user." site_content: none: "Choose a type of content to begin editing." diff --git a/config/routes.rb b/config/routes.rb index ac02614b0bd..f70bb870cf1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,8 @@ Discourse::Application.routes.draw do put 'grant_moderation', constraints: AdminConstraint.new put 'approve' post 'refresh_browsers', constraints: AdminConstraint.new + put 'activate' + put 'deactivate' end resources :impersonate, constraints: AdminConstraint.new diff --git a/lib/guardian.rb b/lib/guardian.rb index 936f4ba3473..bee794c109d 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -58,6 +58,7 @@ class Guardian end alias :can_move_posts? :can_moderate? alias :can_see_flags? :can_moderate? + alias :can_send_activation_email? :can_moderate? # Can the user create a topic in the forum def can_create?(klass, parent=nil) @@ -105,6 +106,7 @@ class Guardian return false if target.approved? @user.staff? end + alias :can_activate? :can_approve? def can_ban?(user) return false if user.blank? @@ -112,6 +114,7 @@ class Guardian return false if user.admin? true end + alias :can_deactivate? :can_ban? def can_clear_flags?(post) return false if @user.blank?