Introduce self-hosted mode

This commit is contained in:
Eugene Burmakin 2025-01-15 21:52:59 +01:00
parent dfe3be5232
commit 11677b14ae
8 changed files with 95 additions and 8 deletions

View file

@ -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

View file

@ -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)

View file

@ -1,6 +1,6 @@
<div role="tablist" class="tabs tabs-lifted tabs-lg">
<%= 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 %>

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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