From 1c8477eab214629d26d9526b386bf75729917060 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcin=20Cie=C5=9Blak?= <saper@saper.info>
Date: Sat, 15 Apr 2017 16:46:27 +0200
Subject: [PATCH] Give SINGLE_USER a chance to register (#1820)

An attempt to open a brand new Mastodon instance configured
as SINGLE_USER_MODE=true will cause an exception.

Enable temporary registration if we have no users in the database

Fixes #1817
---
 app/controllers/application_controller.rb        | 6 +++++-
 app/controllers/auth/registrations_controller.rb | 2 +-
 app/controllers/home_controller.rb               | 2 +-
 app/views/accounts/show.html.haml                | 2 +-
 app/views/stream_entries/show.html.haml          | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 61ca71123f..0c320177dd 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base
   force_ssl if: "Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'"
 
   include Localized
-  helper_method :current_account
+  helper_method :current_account, :single_user_mode?
 
   rescue_from ActionController::RoutingError, with: :not_found
   rescue_from ActiveRecord::RecordNotFound, with: :not_found
@@ -69,6 +69,10 @@ class ApplicationController < ActionController::Base
     end
   end
 
+  def single_user_mode?
+    @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.first
+  end
+
   def current_account
     @current_account ||= current_user.try(:account)
   end
diff --git a/app/controllers/auth/registrations_controller.rb b/app/controllers/auth/registrations_controller.rb
index 4881c074aa..f8050afb5c 100644
--- a/app/controllers/auth/registrations_controller.rb
+++ b/app/controllers/auth/registrations_controller.rb
@@ -28,7 +28,7 @@ class Auth::RegistrationsController < Devise::RegistrationsController
   end
 
   def check_enabled_registrations
-    redirect_to root_path if Rails.configuration.x.single_user_mode || !Setting.open_registrations
+    redirect_to root_path if single_user_mode? || !Setting.open_registrations
   end
 
   private
diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb
index 65ac106928..2d1cf74f03 100644
--- a/app/controllers/home_controller.rb
+++ b/app/controllers/home_controller.rb
@@ -13,7 +13,7 @@ class HomeController < ApplicationController
   private
 
   def authenticate_user!
-    redirect_to(Rails.configuration.x.single_user_mode ? account_path(Account.first) : about_path) unless user_signed_in?
+    redirect_to(single_user_mode? ? account_path(Account.first) : about_path) unless user_signed_in?
   end
 
   def find_or_create_access_token
diff --git a/app/views/accounts/show.html.haml b/app/views/accounts/show.html.haml
index 3b0d69dcdf..9c4e32e0e9 100644
--- a/app/views/accounts/show.html.haml
+++ b/app/views/accounts/show.html.haml
@@ -14,7 +14,7 @@
   %meta{ property: 'og:image:height', content: '120' }/
   %meta{ property: 'twitter:card', content: 'summary' }/
 
-- if !user_signed_in? && !Rails.configuration.x.single_user_mode
+- if !user_signed_in? && !single_user_mode?
   = render partial: 'shared/landing_strip', locals: { account: @account }
 
 .h-feed
diff --git a/app/views/stream_entries/show.html.haml b/app/views/stream_entries/show.html.haml
index 8629467503..73a2365f06 100644
--- a/app/views/stream_entries/show.html.haml
+++ b/app/views/stream_entries/show.html.haml
@@ -20,7 +20,7 @@
 
   %meta{ property: 'twitter:card', content: 'summary' }/
 
-- if !user_signed_in? && !Rails.configuration.x.single_user_mode
+- if !user_signed_in? && !single_user_mode?
   = render partial: 'shared/landing_strip', locals: { account: @stream_entry.account }
 
 .activity-stream.activity-stream-headless.h-entry
-- 
GitLab