diff --git a/.env_sample b/.env_sample index cf3c5b010..6bcadb369 100644 --- a/.env_sample +++ b/.env_sample @@ -1,7 +1,9 @@ -# App domain + Protocol setting for development +# App core values export APP_DOMAIN="localhost:3000" export APP_PROTOCOL="http://" export APP_NAME="forem_local" +export FOREM_OWNER_SECRET="secret" + # Openresty domain + Protocol setting for development export OPENRESTY_DOMAIN="" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7ea6b0665..586f5fc2f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,7 +17,14 @@ class ApplicationController < ActionController::Base error_too_many_requests(exc) end - PUBLIC_CONTROLLERS = %w[shell async_info ga_events service_worker omniauth_callbacks registrations].freeze + PUBLIC_CONTROLLERS = %w[shell + async_info + ga_events + service_worker + omniauth_callbacks + registrations + confirmations + passwords].freeze private_constant :PUBLIC_CONTROLLERS def verify_private_forem diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 92c55fcff..b9bfa22f9 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -13,6 +13,8 @@ class RegistrationsController < Devise::RegistrationsController def create not_authorized unless SiteConfig.allow_email_password_registration || SiteConfig.waiting_on_first_user + not_authorized if SiteConfig.waiting_on_first_user && ENV["FOREM_OWNER_SECRET"].present? && + ENV["FOREM_OWNER_SECRET"] != params[:user][:forem_owner_secret] build_resource(sign_up_params) resource.saw_onboarding = false diff --git a/app/views/shared/authentication/_email_registration_form.html.erb b/app/views/shared/authentication/_email_registration_form.html.erb index 75fce7338..740915de8 100644 --- a/app/views/shared/authentication/_email_registration_form.html.erb +++ b/app/views/shared/authentication/_email_registration_form.html.erb @@ -48,6 +48,12 @@ <%= f.password_field :password_confirmation, autocomplete: "current-password", class: "crayons-textfield", required: true %> + <% if ENV["FOREM_OWNER_SECRET"].present? && SiteConfig.waiting_on_first_user %> +
+ <%= f.label :forem_owner_secret, "Few Forem Secret", class: "crayons-field__label" %> + <%= f.password_field :forem_owner_secret, autocomplete: "current-password", class: "crayons-textfield", required: true, placeholder: "As provided by your Forem host" %> +
+ <% end %>
<%= f.submit "Sign up", class: "crayons-btn" %>
diff --git a/spec/requests/registrations_spec.rb b/spec/requests/registrations_spec.rb index e57d359d6..454b2ccf5 100644 --- a/spec/requests/registrations_spec.rb +++ b/spec/requests/registrations_spec.rb @@ -108,13 +108,15 @@ RSpec.describe "Registrations", type: :request do context "when site is in waiting_on_first_user state" do before do SiteConfig.waiting_on_first_user = true + ENV["FOREM_OWNER_SECRET"] = nil end after do SiteConfig.waiting_on_first_user = false + ENV["FOREM_OWNER_SECRET"] = nil end - it "does not raise disallowed if community is set to allow email" do + it "does not raise disallowed" do expect { post "/users" }.not_to raise_error Pundit::NotAuthorizedError end @@ -138,6 +140,33 @@ RSpec.describe "Registrations", type: :request do expect(User.first.has_role?(:super_admin)).to be true expect(User.first.has_role?(:single_resource_admin, Config)).to be true end + + it "creates super admin with valid params in FOREM_OWNER_SECRET scenario" do + ENV["FOREM_OWNER_SECRET"] = "test" + post "/users", params: + { user: { name: "test #{rand(10)}", + username: "haha_#{rand(10)}", + email: "yoooo#{rand(100)}@yo.co", + password: "PaSSw0rd_yo000", + forem_owner_secret: "test", + password_confirmation: "PaSSw0rd_yo000" } } + expect(User.first.has_role?(:super_admin)).to be true + expect(User.first.has_role?(:single_resource_admin, Config)).to be true + end + + it "does not authorize request in FOREM_OWNER_SECRET scenario if not passed correct value" do + ENV["FOREM_OWNER_SECRET"] = "test" + expect do + post "/users", params: + { user: { name: "test #{rand(10)}", + username: "haha_#{rand(10)}", + email: "yoooo#{rand(100)}@yo.co", + password: "PaSSw0rd_yo000", + forem_owner_secret: "not_test", + password_confirmation: "PaSSw0rd_yo000" } } + expect(User.first).to be nil + end.to raise_error Pundit::NotAuthorizedError + end end end end