Skip to content

Commit

Permalink
[api] ldap: Simplify checking of Config values
Browse files Browse the repository at this point in the history
This shortens the code for checking if a value for a certain
configuration option matches what we inspect.
When a key value pair does not exist in the Config store, the return
value for that non-existing key is nil. Therefore we can skip checking
the existence of a key and just query andcompare it in one go.
  • Loading branch information
bgeuken committed Jul 27, 2017
1 parent ce0d958 commit 2ea5631
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/api/app/models/user_ldap_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def self.render_grouplist_ldap(grouplist, user = nil)

if user
# search user
if CONFIG.has_key?('ldap_user_filter')
if CONFIG['ldap_user_filter']
filter = "(&(#{CONFIG['ldap_search_attr']}=#{user})#{CONFIG['ldap_user_filter']})"
else
filter = "(#{CONFIG['ldap_search_attr']}=#{user})"
Expand All @@ -83,7 +83,7 @@ def self.render_grouplist_ldap(grouplist, user = nil)
user_memberof_attr = String.new
ldap_con.search(CONFIG['ldap_search_base'], LDAP::LDAP_SCOPE_SUBTREE, filter) do |entry|
user_dn = entry.dn
if CONFIG.has_key?('ldap_user_memberof_attr') && entry.attrs.include?(CONFIG['ldap_user_memberof_attr'])
if CONFIG['ldap_user_memberof_attr'].in?(entry.attrs)
user_memberof_attr = entry.vals(CONFIG['ldap_user_memberof_attr'])
end
end
Expand Down Expand Up @@ -121,7 +121,7 @@ def self.render_grouplist_ldap(grouplist, user = nil)
Rails.logger.debug("Search group: #{filter}")
ldap_con.search(CONFIG['ldap_group_search_base'], LDAP::LDAP_SCOPE_SUBTREE, filter) do |entry|
group_dn = entry.dn
if CONFIG.has_key?('ldap_group_member_attr') && entry.attrs.include?(CONFIG['ldap_group_member_attr'])
if CONFIG['ldap_group_member_attr'].in?(entry.attrs)
group_member_attr = entry.vals(CONFIG['ldap_group_member_attr'])
end
end
Expand Down Expand Up @@ -335,7 +335,7 @@ def self.initialize_ldap_con(user_name, password)
require 'ldap'
ldap_servers = CONFIG['ldap_servers'].split(":")

max_ldap_attempts = CONFIG.has_key?('ldap_max_attempts') ? CONFIG['ldap_max_attempts'] : 10
max_ldap_attempts = CONFIG['ldap_max_attempts'] || 10
# Do 10 attempts to connect to one of the configured LDAP servers. LDAP server
# to connect to is chosen randomly.
max_ldap_attempts.times do
Expand Down Expand Up @@ -370,7 +370,7 @@ def self.try_ldap_con(server, user_name, password)
conn = LDAP::Conn.new(server, port)
end
conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
if CONFIG.has_key?('ldap_referrals') && CONFIG['ldap_referrals'] == :off
if CONFIG['ldap_referrals'] == :off
conn.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_OFF)
end
conn.bind(user_name, password)
Expand Down

0 comments on commit 2ea5631

Please sign in to comment.