From 7e7c4efcc03a5feccb9bf4647a48d9628c1788b7 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 24 Mar 2014 18:03:39 +1100 Subject: [PATCH] FEATURE: on initial boot hint users on how to get admin --- app/models/user.rb | 8 ++++++++ config/environments/development.rb | 4 ++++ config/initializers/06-ensure_login_hint.rb | 20 ++++++++++++++++++++ config/site_settings.yml | 3 +++ lib/auth/default_current_user_provider.rb | 3 ++- 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 config/initializers/06-ensure_login_hint.rb diff --git a/app/models/user.rb b/app/models/user.rb index 233539359e9..08c561b510d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -67,6 +67,7 @@ class User < ActiveRecord::Base after_initialize :set_default_external_links_in_new_tab after_save :update_tracked_topics + after_save :clear_global_notice_if_needed after_create :create_email_token after_create :create_user_stat @@ -586,6 +587,13 @@ class User < ActiveRecord::Base TrackedTopicsUpdater.new(id, auto_track_topics_after_msecs).call end + def clear_global_notice_if_needed + if admin && SiteSetting.has_login_hint + SiteSetting.has_login_hint = false + SiteSetting.global_notice = "" + end + end + def create_user_stat stat = UserStat.new(new_since: Time.now) stat.user_id = id diff --git a/config/environments/development.rb b/config/environments/development.rb index ea6e58aa5b0..1c33aa290d5 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -45,5 +45,9 @@ Discourse::Application.configure do config.enable_anon_caching = false require 'rbtrace' + + if emails = GlobalSetting.developer_emails + config.developer_emails = emails.split(",") + end end diff --git a/config/initializers/06-ensure_login_hint.rb b/config/initializers/06-ensure_login_hint.rb new file mode 100644 index 00000000000..444c30cbc36 --- /dev/null +++ b/config/initializers/06-ensure_login_hint.rb @@ -0,0 +1,20 @@ +# Some sanity checking so we don't count on an unindexed column on boot +if User.limit(20).count < 20 && User.where(admin: true).count == 1 + notice = + if GlobalSetting.developer_emails.blank? + "No developer email addresses defined, logging in will be tricky." + else + emails = GlobalSetting.developer_emails.split(",") + if emails.length > 1 + emails = emails[0..-2].join(' , ') << " or #{emails[-1]} " + end + "Please create an account or login with #{emails}" + end + + if notice != SiteSetting.global_notice + SiteSetting.global_notice = notice + SiteSetting.has_login_hint = true + end + +# we may be booting with no User table eg: first migration, just skip +end rescue nil diff --git a/config/site_settings.yml b/config/site_settings.yml index d9f7a882627..7fc68cba420 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -479,6 +479,9 @@ uncategorized: global_notice: default: "" client: true + has_login_hint: + default: false + hidden: true # Category IDs lounge_category_id: diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb index 835c9586c39..4523e5d14b1 100644 --- a/lib/auth/default_current_user_provider.rb +++ b/lib/auth/default_current_user_provider.rb @@ -78,7 +78,8 @@ class Auth::DefaultCurrentUserProvider !user.admin && Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(user.email) - user.update_column(:admin, true) + user.admin = true + user.save end end