From b09ad870980b4ec29a0f3497680eda7778c4a11e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 3 Nov 2014 12:46:08 +0100 Subject: [PATCH] FIX: add 'show emails' button from moderators in user admin section --- .../admin/controllers/admin-users-list.js.es6 | 8 +++++-- .../admin/templates/users_list.hbs | 14 +++++++++---- .../stylesheets/common/admin/admin_base.scss | 4 ++++ app/controllers/admin/users_controller.rb | 10 +++++++-- app/serializers/admin_user_serializer.rb | 2 +- app/services/staff_action_logger.rb | 16 +++++++++++++- config/locales/client.en.yml | 1 + lib/admin_user_index_query.rb | 1 + lib/guardian.rb | 6 ++++++ .../admin/users_controller_spec.rb | 21 +++++++++++++++++++ 10 files changed, 73 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-users-list.js.es6 b/app/assets/javascripts/admin/controllers/admin-users-list.js.es6 index 00940ddfb94..36209fc2127 100644 --- a/app/assets/javascripts/admin/controllers/admin-users-list.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-users-list.js.es6 @@ -90,11 +90,11 @@ export default Ember.ArrayController.extend(Discourse.Presence, { @method refreshUsers **/ - refreshUsers: function() { + refreshUsers: function(showEmails) { var adminUsersListController = this; adminUsersListController.set('loading', true); - Discourse.AdminUser.findAll(this.get('query'), { filter: this.get('username') }).then(function (result) { + Discourse.AdminUser.findAll(this.get('query'), { filter: this.get('username'), show_emails: showEmails }).then(function (result) { adminUsersListController.set('content', result); adminUsersListController.set('loading', false); }); @@ -140,6 +140,10 @@ export default Ember.ArrayController.extend(Discourse.Presence, { bootbox.alert(message); controller.refreshUsers(); }); + }, + + showEmails: function() { + this.refreshUsers(true); } }); diff --git a/app/assets/javascripts/admin/templates/users_list.hbs b/app/assets/javascripts/admin/templates/users_list.hbs index 285182aebf4..c9d760bcdb5 100644 --- a/app/assets/javascripts/admin/templates/users_list.hbs +++ b/app/assets/javascripts/admin/templates/users_list.hbs @@ -28,9 +28,14 @@ {{/if}} -

{{title}}

- -
+
+
+

{{title}}

+
+
+ +
+
{{#if loading}}
@@ -43,6 +48,7 @@ {{/if}}   {{i18n username}} + {{i18n email}} {{i18n admin.users.last_emailed}} {{i18n last_seen}} {{i18n admin.user.topics_entered}} @@ -53,7 +59,6 @@ {{i18n admin.users.approved}} {{/if}}   - {{#each model}} @@ -67,6 +72,7 @@ {{/if}} {{#link-to 'adminUser' this}}{{avatar this imageSize="small"}}{{/link-to}} {{#link-to 'adminUser' this}}{{unbound username}}{{/link-to}} + {{{unbound email}}} {{{unbound last_emailed_age}}} {{{unbound last_seen_age}}} {{{unbound topics_entered}}} diff --git a/app/assets/stylesheets/common/admin/admin_base.scss b/app/assets/stylesheets/common/admin/admin_base.scss index ce887d58f5e..79335b2fd01 100644 --- a/app/assets/stylesheets/common/admin/admin_base.scss +++ b/app/assets/stylesheets/common/admin/admin_base.scss @@ -89,6 +89,10 @@ td.flaggers td { margin-top: 20px; } +.admin-title { + height: 45px; +} + .admin-controls { background-color: dark-light-diff($primary, $secondary, 90%, -75%); padding: 10px 10px 3px 0; diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e6750c2b193..ed69ad463fa 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -25,8 +25,14 @@ class Admin::UsersController < Admin::AdminController :revoke_api_key] def index - query = ::AdminUserIndexQuery.new(params) - render_serialized(query.find_users, AdminUserSerializer) + users = ::AdminUserIndexQuery.new(params).find_users + + if params[:show_emails] == "true" + guardian.can_see_emails = true + StaffActionLogger.new(current_user).log_show_emails(users) + end + + render_serialized(users, AdminUserSerializer) end def show diff --git a/app/serializers/admin_user_serializer.rb b/app/serializers/admin_user_serializer.rb index 461bf67d20c..4c9f309ce92 100644 --- a/app/serializers/admin_user_serializer.rb +++ b/app/serializers/admin_user_serializer.rb @@ -39,7 +39,7 @@ class AdminUserSerializer < BasicUserSerializer def include_email? # staff members can always see their email - scope.is_staff? && object.id == scope.user.id + (scope.is_staff? && object.id == scope.user.id) || scope.can_see_emails? end alias_method :include_associated_accounts?, :include_email? diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index bc99302b9b1..509328f3333 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -142,10 +142,24 @@ class StaffActionLogger })) end + def log_show_emails(users) + values = [] + + users.each do |user| + values << "(#{@admin.id}, #{UserHistory.actions[:check_email]}, #{user.id}, current_timestamp, current_timestamp)" + end + + # bulk insert + UserHistory.exec_sql <<-SQL + INSERT INTO user_histories (acting_user_id, action, target_user_id, created_at, updated_at) + VALUES #{values.join(",")} + SQL + end + private def params(opts) - {acting_user_id: @admin.id, context: opts[:context]} + { acting_user_id: @admin.id, context: opts[:context] } end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 2a74b7e102d..376f1a4d1e5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1861,6 +1861,7 @@ en: last_emailed: "Last Emailed" not_found: "Sorry, that username doesn't exist in our system." active: "Active" + show_emails: "Show Emails" nav: new: "New" active: "Active" diff --git a/lib/admin_user_index_query.rb b/lib/admin_user_index_query.rb index 6376640e0ad..cf20f2103cb 100644 --- a/lib/admin_user_index_query.rb +++ b/lib/admin_user_index_query.rb @@ -78,6 +78,7 @@ class AdminUserIndexQuery .includes(:github_user_info) .includes(:google_user_info) .includes(:oauth2_user_info) + .includes(:user_open_ids) .take(100) end end diff --git a/lib/guardian.rb b/lib/guardian.rb index 5fab43dfd48..f0998e05770 100644 --- a/lib/guardian.rb +++ b/lib/guardian.rb @@ -26,6 +26,8 @@ class Guardian def email; nil; end end + attr_accessor :can_see_emails + def initialize(user=nil) @user = user.presence || AnonymousUser.new end @@ -243,6 +245,10 @@ class Guardian (is_staff? || target.is_a?(Group) || !target.suspended?) end + def can_see_emails? + @can_see_emails + end + private def is_my_own?(obj) diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index 3482d02201f..767dc021f12 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -22,6 +22,27 @@ describe Admin::UsersController do xhr :get, :index ::JSON.parse(response.body).should be_present end + + context 'when showing emails' do + + it "returns email for all the users" do + xhr :get, :index, show_emails: "true" + data = ::JSON.parse(response.body) + data.each do |user| + user["email"].should be_present + end + end + + it "logs an enty for all email shown" do + UserHistory.where(action: UserHistory.actions[:check_email], acting_user_id: @user.id).count.should == 0 + + xhr :get, :index, show_emails: "true" + data = ::JSON.parse(response.body) + + UserHistory.where(action: UserHistory.actions[:check_email], acting_user_id: @user.id).count.should == data.length + end + + end end describe '.show' do