From 30f64416237464fc66e4c20ed96198a8123e287c Mon Sep 17 00:00:00 2001 From: Tyler Whitney Date: Mon, 19 Dec 2016 22:47:20 -0500 Subject: [PATCH] Add capability to add optional suffix to username Add capability to add optional suffix to username, e.g. @domain.com in the case that you want users to be able to login with just a username and not full email address. While you can customize which field in LDAP contains the username, sometimes you might want to be able to append a predefined suffix. --- lib/casino/ldap_authenticator.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/casino/ldap_authenticator.rb b/lib/casino/ldap_authenticator.rb index 5af2ed8..9319126 100644 --- a/lib/casino/ldap_authenticator.rb +++ b/lib/casino/ldap_authenticator.rb @@ -3,6 +3,7 @@ class CASino::LDAPAuthenticator DEFAULT_USERNAME_ATTRIBUTE = 'uid' + DEFAULT_USERNAME_SUFFIX = '' # @param [Hash] options def initialize(options) @@ -59,7 +60,7 @@ def load_user_data_with_connection(username, ldap) def user_data(user) { - username: user[username_attribute].first, + username: user[username_attribute].first.chomp(username_suffix), extra_attributes: extra_attributes(user) } end @@ -68,8 +69,12 @@ def username_attribute @options[:username_attribute] || DEFAULT_USERNAME_ATTRIBUTE end + def username_suffix + @options[:username_suffix] || DEFAULT_USERNAME_SUFFIX + end + def user_filter(username) - filter = Net::LDAP::Filter.eq(username_attribute, username) + filter = Net::LDAP::Filter.eq(username_attribute, username + username_suffix) unless @options[:filter].nil? filter &= Net::LDAP::Filter.construct(@options[:filter]) end