From 11677b14aef8a76aa3579d48c8e3b6adc68a52f7 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 15 Jan 2025 21:52:59 +0100 Subject: [PATCH] Introduce self-hosted mode --- app/controllers/application_controller.rb | 6 ++++ app/controllers/settings/users_controller.rb | 2 +- app/views/settings/_navigation.html.erb | 2 +- config/initializers/01_constants.rb | 4 ++- config/initializers/03_dawarich_settings.rb | 4 +++ config/routes.rb | 13 ++++--- spec/requests/settings_spec.rb | 37 +++++++++++++++++++- spec/requests/users_spec.rb | 35 ++++++++++++++++++ 8 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 spec/requests/users_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6d104eab..f1a5e617 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,4 +18,10 @@ class ApplicationController < ActionController::Base redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :see_other end + + def authenticate_self_hosted! + return if DawarichSettings.self_hosted? + + redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :see_other + end end diff --git a/app/controllers/settings/users_controller.rb b/app/controllers/settings/users_controller.rb index 529785db..046d34ac 100644 --- a/app/controllers/settings/users_controller.rb +++ b/app/controllers/settings/users_controller.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true class Settings::UsersController < ApplicationController - before_action :authenticate_user! before_action :authenticate_admin! + before_action :authenticate_self_hosted! def index @users = User.order(created_at: :desc) diff --git a/app/views/settings/_navigation.html.erb b/app/views/settings/_navigation.html.erb index b0b20437..8ce09ba9 100644 --- a/app/views/settings/_navigation.html.erb +++ b/app/views/settings/_navigation.html.erb @@ -1,6 +1,6 @@
<%= link_to 'Integrations', settings_path, role: 'tab', class: "tab #{active_tab?(settings_path)}" %> - <% if current_user.admin? %> + <% if DawarichSettings.self_hosted? && current_user.admin? %> <%= link_to 'Users', settings_users_path, role: 'tab', class: "tab #{active_tab?(settings_users_path)}" %> <%= link_to 'Background Jobs', settings_background_jobs_path, role: 'tab', class: "tab #{active_tab?(settings_background_jobs_path)}" %> <% end %> diff --git a/config/initializers/01_constants.rb b/config/initializers/01_constants.rb index ce760238..37a318ed 100644 --- a/config/initializers/01_constants.rb +++ b/config/initializers/01_constants.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +SELF_HOSTED = ENV.fetch('SELF_HOSTED', 'true') == 'true' + MIN_MINUTES_SPENT_IN_CITY = ENV.fetch('MIN_MINUTES_SPENT_IN_CITY', 60).to_i DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym @@ -11,7 +13,7 @@ TELEMETRY_URL = 'https://influxdb2.frey.today/api/v2/write' # Reverse geocoding settings PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) PHOTON_API_KEY = ENV.fetch('PHOTON_API_KEY', nil) -PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true' +PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'false') == 'true' GEOAPIFY_API_KEY = ENV.fetch('GEOAPIFY_API_KEY', nil) # /Reverse geocoding settings diff --git a/config/initializers/03_dawarich_settings.rb b/config/initializers/03_dawarich_settings.rb index 87cf4817..aa80b763 100644 --- a/config/initializers/03_dawarich_settings.rb +++ b/config/initializers/03_dawarich_settings.rb @@ -17,5 +17,9 @@ class DawarichSettings def geoapify_enabled? @geoapify_enabled ||= GEOAPIFY_API_KEY.present? end + + def self_hosted? + @self_hosted ||= SELF_HOSTED + end end end diff --git a/config/routes.rb b/config/routes.rb index 8d28efde..5b370c5d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,10 +51,15 @@ Rails.application.routes.draw do constraints: { year: /\d{4}/, month: /\d{1,2}|all/ } root to: 'home#index' - devise_for :users, skip: [:registrations] - as :user do - get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration' - put 'users' => 'devise/registrations#update', :as => 'user_registration' + + if SELF_HOSTED + devise_for :users, skip: [:registrations] + as :user do + get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration' + put 'users' => 'devise/registrations#update', :as => 'user_registration' + end + else + devise_for :users end get 'map', to: 'map#index' diff --git a/spec/requests/settings_spec.rb b/spec/requests/settings_spec.rb index e214b1c4..0ced085e 100644 --- a/spec/requests/settings_spec.rb +++ b/spec/requests/settings_spec.rb @@ -58,7 +58,7 @@ RSpec.describe 'Settings', type: :request do end it 'generates an API key for the user' do - expect { post '/settings/generate_api_key' }.to change { user.reload.api_key } + expect { post '/settings/generate_api_key' }.to(change { user.reload.api_key }) end it 'redirects back' do @@ -83,4 +83,39 @@ RSpec.describe 'Settings', type: :request do expect(user.reload.settings).to eq(params[:settings]) end end + + describe 'GET /settings/users' do + let!(:user) { create(:user, admin: true) } + + before do + stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags') + .to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {}) + + sign_in user + end + + context 'when self-hosted' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(true) + end + + it 'returns http success' do + get '/settings/users' + + expect(response).to have_http_status(:success) + end + end + + context 'when not self-hosted' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(false) + end + + it 'redirects to root path' do + get '/settings/users' + + expect(response).to redirect_to(root_path) + end + end + end end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb new file mode 100644 index 00000000..8c0bcdf5 --- /dev/null +++ b/spec/requests/users_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Users', type: :request do + before do + stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags') + .to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {}) + end + + describe 'GET /users/sign_up' do + context 'when self-hosted' do + before do + stub_const('SELF_HOSTED', true) + end + + it 'returns http success' do + get '/users/sign_up' + expect(response).to have_http_status(:not_found) + end + end + + context 'when not self-hosted' do + before do + stub_const('SELF_HOSTED', false) + Rails.application.reload_routes! + end + + it 'returns http success' do + get '/users/sign_up' + expect(response).to have_http_status(:success) + end + end + end +end