From e151bbbb285c8eafe4fe8aff06d225fef71e520e Mon Sep 17 00:00:00 2001 From: Jan-Willem van der Meer Date: Tue, 7 Oct 2014 12:22:16 +0200 Subject: [PATCH] Extract LDAP config to seperate class --- lib/gitlab/ldap/config.rb | 81 +++++++++++++++++++++++++++++ spec/lib/gitlab/ldap/config_spec.rb | 16 ++++++ 2 files changed, 97 insertions(+) create mode 100644 lib/gitlab/ldap/config.rb create mode 100644 spec/lib/gitlab/ldap/config_spec.rb diff --git a/lib/gitlab/ldap/config.rb b/lib/gitlab/ldap/config.rb new file mode 100644 index 0000000000..37316f41a9 --- /dev/null +++ b/lib/gitlab/ldap/config.rb @@ -0,0 +1,81 @@ +# Load a specific server configuration +module Gitlab + module LDAP + class Config + attr_accessor :provider, :options + + def initialize(provider) + @provider = provider + @options = config_for(provider) + end + + def enabled? + base_config.enabled + end + + def adapter_options + { + host: options['host'], + port: options['port'], + encryption: encryption + }.tap do |options| + options.merge!(auth_options) if has_auth? + end + end + + def sync_ssh_keys? + ssh_sync_key.present? + end + + def ssh_sync_key + options['sync_ssh_keys'] + end + + def user_filter + options['user_filter'] + end + + def group_base + options['group_base'] + end + + def admin_group + options['admin_group'] + end + + protected + def base_config + Gitlab.config.ldap + end + + def config_for(provider) + base_config.servers.find { |server| server.provider_name == provider } + end + + def encryption + case options['method'].to_s + when 'ssl' + :simple_tls + when 'tls' + :start_tls + else + nil + end + end + + def auth_options + { + auth: { + method: :simple, + username: options['bind_dn'], + password: options['password'] + } + } + end + + def has_auth? + options['password'] || options['bind_dn'] + end + end + end +end diff --git a/spec/lib/gitlab/ldap/config_spec.rb b/spec/lib/gitlab/ldap/config_spec.rb new file mode 100644 index 0000000000..a01166b264 --- /dev/null +++ b/spec/lib/gitlab/ldap/config_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Gitlab::LDAP::Config do + let(:config) { Gitlab::LDAP::Config.new provider } + let(:provider) { 'ldapmain' } + + describe :initalize do + it 'requires a provider' do + expect{ Gitlab::LDAP::Config.new }.to raise_error ArgumentError + end + + it "works" do + expect(config).to be_a described_class + end + end +end \ No newline at end of file