• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hiroaki / Annabelle / 16597649163

29 Jul 2025 01:33PM UTC coverage: 99.871% (-0.1%) from 100.0%
16597649163

Pull #41

github

web-flow
Merge 5ab866f24 into 52b06922b
Pull Request #41: Basic認証 の実装

8 of 9 new or added lines in 1 file covered. (88.89%)

777 of 778 relevant lines covered (99.87%)

86.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/app/controllers/application_controller.rb
1
class ApplicationController < ActionController::Base
1✔
2
  before_action :http_basic_authenticate
1✔
3
  before_action :set_locale
1✔
4
  before_action :configure_permitted_parameters, if: :devise_controller?
1✔
5
  before_action :conditional_auto_login
1✔
6

7
  rescue_from I18n::InvalidLocale, with: :handle_invalid_locale
1✔
8

9
  # URLヘルパーに自動的にロケールパラメータを追加
10
  def default_url_options
1✔
11
    { locale: I18n.locale }
581✔
12
  end
13

14
  # ログイン後のリダイレクト先を決定
15
  def after_sign_in_path_for(resource)
1✔
16
    stored_location = stored_location_for(resource)
56✔
17
    if stored_location.present?
56✔
18
      uri = URI.parse(stored_location)
2✔
19
      # 既存のロケール部分を現在のI18n.localeで置換
20
      uri.path = uri.path.sub(/^\/[a-z]{2}/, "/#{I18n.locale}")
2✔
21
      uri.to_s
2✔
22
    else
23
      root_path(locale: I18n.locale)
54✔
24
    end
25
  end
26

27
  protected
1✔
28

29
  def set_locale
1✔
30
    I18n.locale = LocaleUtils.determine_locale(params, request, current_user)
607✔
31
  end
32

33
  private
1✔
34

35
  # 環境変数 BASIC_AUTH_USER と BASIC_AUTH_PASSWORD が設定されている場合にかぎり、
36
  # Basic 認証を有効にします。
37
  def http_basic_authenticate
1✔
38
    if valid_user && valid_pswd
619✔
39
      authenticate_or_request_with_http_basic do |username, password|
3✔
40
        username == valid_user && password == valid_pswd
2✔
41
      end
42
    end
43
  end
44

45
  def valid_user
1✔
46
    ENV['BASIC_AUTH_USER'].presence
613✔
47
  end
48

49
  def valid_pswd
1✔
NEW
50
    ENV['BASIC_AUTH_PASSWORD'].presence
×
51
  end
52

53
  def handle_invalid_locale(exception)
1✔
54
    Rails.logger.error("[Locale] Invalid locale error: #{exception.message} (params: #{params.inspect})")
1✔
55
  end
56

57
  def configure_permitted_parameters
1✔
58
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
300✔
59
    devise_parameter_sanitizer.permit(:sign_in, keys: [:otp_attempt])
300✔
60
    devise_parameter_sanitizer.permit(:account_update, keys: [:username, :preferred_language])
300✔
61
  end
62

63
  def conditional_auto_login
1✔
64
    return unless Rails.configuration.x.auto_login.enabled
616✔
65
    return if user_signed_in?
5✔
66
    email = Rails.configuration.x.auto_login.email.presence
3✔
67
    user = email ? User.find_by!(email: email) : User.admin_user
3✔
68
    if !user.present? || !user.active_for_authentication?
3✔
69
      raise "conditional_auto_login failed"
1✔
70
    end
71
    sign_in(:user, user)
2✔
72
  end
73
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc