BigW Consortium Gitlab

auth_helper.rb 1.71 KB
Newer Older
1
module AuthHelper
2 3
  include Gitlab::CurrentSettings

4
  PROVIDERS_WITH_ICONS = %w(twitter github gitlab bitbucket google_oauth2 facebook azure_oauth2 authentiq).freeze
5
  FORM_BASED_PROVIDERS = [/\Aldap/, 'crowd'].freeze
6 7

  def ldap_enabled?
8
    Gitlab::LDAP::Config.enabled?
9 10
  end

11 12 13 14
  def omniauth_enabled?
    Gitlab.config.omniauth.enabled
  end

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  def provider_has_icon?(name)
    PROVIDERS_WITH_ICONS.include?(name.to_s)
  end

  def auth_providers
    Gitlab::OAuth::Provider.providers
  end

  def label_for_provider(name)
    Gitlab::OAuth::Provider.label_for(name)
  end

  def form_based_provider?(name)
    FORM_BASED_PROVIDERS.any? { |pattern| pattern === name.to_s }
  end

  def form_based_providers
    auth_providers.select { |provider| form_based_provider?(provider) }
  end

Valery Sizov committed
35 36 37 38
  def crowd_enabled?
    auth_providers.include? :crowd
  end

39 40 41 42
  def button_based_providers
    auth_providers.reject { |provider| form_based_provider?(provider) }
  end

43 44 45 46 47 48
  def enabled_button_based_providers
    disabled_providers = current_application_settings.disabled_oauth_sign_in_sources || []

    button_based_providers.map(&:to_s) - disabled_providers
  end

49
  def button_based_providers_enabled?
50
    enabled_button_based_providers.any?
51 52
  end

53 54 55 56 57 58
  def provider_image_tag(provider, size = 64)
    label = label_for_provider(provider)

    if provider_has_icon?(provider)
      file_name = "#{provider.to_s.split('_').first}_#{size}.png"

59
      image_tag("auth_buttons/#{file_name}", alt: label, title: "Sign in with #{label}")
60 61 62 63 64 65 66 67 68
    else
      label
    end
  end

  def auth_active?(provider)
    current_user.identities.exists?(provider: provider.to_s)
  end

69 70 71 72
  def unlink_allowed?(provider)
    %w(saml cas3).exclude?(provider.to_s)
  end

73 74
  extend self
end