Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add bootstrap first admin job (#39851)

Gabriel Grubba75db3d669db2
diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb
index f01d584bfd5..e1a79c2a9f5 100644
--- a/lib/auth/default_current_user_provider.rb
+++ b/lib/auth/default_current_user_provider.rb
@@ -275,6 +275,7 @@ class Auth::DefaultCurrentUserProvider
user.unstage!
make_developer_admin(user)
+ bootstrap_first_admin(user)
UserAuthToken.enforce_session_count_limit!(user.id)
@@ -335,6 +336,13 @@ class Auth::DefaultCurrentUserProvider
end
end
+ def bootstrap_first_admin(user)
+ return if !user.admin || user.moderator || !user.last_seen_at.nil? || !user.is_singular_admin?
+
+ user.grant_moderation!
+ StaffActionLogger.new(Discourse.system_user).log_grant_moderation(user)
+ end
+
def log_off_user(session, cookie_jar, push_subscription: nil)
user = current_user
diff --git a/plugins/discourse-assign/spec/system/assign_post_spec.rb b/plugins/discourse-assign/spec/system/assign_post_spec.rb
index 0f1e1c3bbc4..1abdf74249d 100644
--- a/plugins/discourse-assign/spec/system/assign_post_spec.rb
+++ b/plugins/discourse-assign/spec/system/assign_post_spec.rb
@@ -14,6 +14,7 @@ describe "Assign | Assigning posts" do
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
SiteSetting.prioritize_full_name_in_ux = false
+ admin.update!(last_seen_at: 1.day.ago)
sign_in(admin)
end
diff --git a/plugins/discourse-assign/spec/system/bulk_assign_spec.rb b/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
index 9b949680836..179e0c5d390 100644
--- a/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
+++ b/plugins/discourse-assign/spec/system/bulk_assign_spec.rb
@@ -12,6 +12,7 @@ describe "Assign | Bulk Assign" do
before do
SiteSetting.assign_enabled = true
+ admin.update!(last_seen_at: 1.day.ago)
sign_in(admin)
end
diff --git a/spec/lib/auth/default_current_user_provider_spec.rb b/spec/lib/auth/default_current_user_provider_spec.rb
index 0289d0d599f..8d2a3247f4d 100644
--- a/spec/lib/auth/default_current_user_provider_spec.rb
+++ b/spec/lib/auth/default_current_user_provider_spec.rb
@@ -833,4 +833,55 @@ RSpec.describe Auth::DefaultCurrentUserProvider do
expect(user.in_any_groups?([Group::AUTO_GROUPS[:admins]])).to eq(true)
end
end
+
+ describe "bootstrap first admin" do
+ let(:admin) { Fabricate(:admin, last_seen_at: nil) }
+
+ it "grants moderation and logs the staff action on the singular admin's first login" do
+ @provider = provider("/")
+ @provider.log_on_user(admin, {}, @provider.cookie_jar)
+
+ expect(admin.reload.moderator).to eq(true)
+ log = UserHistory.where(action: UserHistory.actions[:grant_moderation]).last
+ expect(log.target_user_id).to eq(admin.id)
+ expect(log.acting_user_id).to eq(Discourse.system_user.id)
+ end
+
+ it "is idempotent: a second login does not re-log grant_moderation" do
+ @provider = provider("/")
+ @provider.log_on_user(admin, {}, @provider.cookie_jar)
+ admin.update!(last_seen_at: nil)
+
+ expect {
+ @provider = provider("/")
+ @provider.log_on_user(admin.reload, {}, @provider.cookie_jar)
+ }.to_not change { UserHistory.where(action: UserHistory.actions[:grant_moderation]).count }
+ end
+
+ it "does not grant moderation when another admin already exists" do
+ Fabricate(:admin)
+
+ @provider = provider("/")
+ @provider.log_on_user(admin, {}, @provider.cookie_jar)
+
+ expect(admin.reload.moderator).to eq(false)
+ expect(UserHistory.where(action: UserHistory.actions[:grant_moderation]).count).to eq(0)
+ end
+
+ it "does not grant moderation when the admin has logged in before" do
+ admin.update!(last_seen_at: 1.day.ago)
+
+ @provider = provider("/")
+ @provider.log_on_user(admin, {}, @provider.cookie_jar)
+
+ expect(admin.reload.moderator).to eq(false)
+ end
+
+ it "does not grant moderation to non-admin users" do
+ @provider = provider("/")
+ @provider.log_on_user(user, {}, @provider.cookie_jar)
+
+ expect(user.reload.moderator).to eq(false)
+ end
+ end
end
diff --git a/spec/requests/admin/staff_action_logs_controller_spec.rb b/spec/requests/admin/staff_action_logs_controller_spec.rb
index cdffd14a368..c55a09977b9 100644
--- a/spec/requests/admin/staff_action_logs_controller_spec.rb
+++ b/spec/requests/admin/staff_action_logs_controller_spec.rb
@@ -87,7 +87,10 @@ RSpec.describe Admin::StaffActionLogsController do
end
context "when logged in as an admin" do
- before { sign_in(admin) }
+ before do
+ admin.update!(last_seen_at: 1.day.ago)
+ sign_in(admin)
+ end
include_examples "staff action logs accessible"
@@ -408,7 +411,10 @@ RSpec.describe Admin::StaffActionLogsController do
end
context "when logged in as an admin" do
- before { sign_in(admin) }
+ before do
+ admin.update!(last_seen_at: 1.day.ago)
+ sign_in(admin)
+ end
include_examples "theme diffs accessible"
include_examples "tag_group diffs accessible"
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index b68e73a7f8e..82d822019b1 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -568,7 +568,7 @@ RSpec.describe CategoriesController do
expect(category.category_groups.map { |g| [g.group_id, g.permission_type] }.sort).to eq(
[[Group[:everyone].id, readonly], [Group[:staff].id, create_post]],
)
- expect(UserHistory.count).to eq(1)
+ expect(UserHistory.count).to eq(2) # 1 + 1 (bootstrap first admin)
end
it "creates a category with posting review mode" do
@@ -780,7 +780,7 @@ RSpec.describe CategoriesController do
expect do delete "/categories/#{category.slug}.json" end.to change(Category, :count).by(-1)
expect(response.status).to eq(200)
- expect(UserHistory.count).to eq(1)
+ expect(UserHistory.count).to eq(2) # 1 + 1 (bootstrap first admin)
expect(TopicTimer.where(id: id).exists?).to eq(false)
end
end
@@ -1011,7 +1011,7 @@ RSpec.describe CategoriesController do
},
}
expect(response.status).to eq(200)
- expect(UserHistory.count).to eq(2)
+ expect(UserHistory.count).to eq(3) # 2 + 1 (bootstrap first admin)
end
it "does not log false permission changes when everyone group name is localized" do
diff --git a/spec/system/csv_exports_spec.rb b/spec/system/csv_exports_spec.rb
index 37585037cb4..b242043833c 100644
--- a/spec/system/csv_exports_spec.rb
+++ b/spec/system/csv_exports_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe "CSV Exports" do
before do
Jobs.run_immediately!
+ admin.update!(last_seen_at: 1.day.ago)
sign_in(admin)
end