intridea/omniauth-ldap — pull request #29 by sorccu (2013-02-25 04:05:16+00:00)
Title: Fix alternate LDAP fields (e.g. `email` instead of `mail`) never being used

I came across an issue where fallback attributes would not get mapped correctly. I had an "email" field in LDAP, which should obviously have mapped to the user's email field, as per self.map_user in lib/omniauth/strategies/ldap.rb. But it did not. While debugging this issue I came across the following post: http://comments.gmane.org/gmane.comp.version-control.gitlab/901

And indeed the issue was fixed for me too the same way. This is clearly a bug so I got interested. As it turns out, [Net::LDAP::Entry returns empty arrays for non-existing keys](https://github.com/ruby-ldap/ruby-net-ldap/blob/master/lib/net/ldap/entry.rb#L113), causing omniauth-ldap's user mapping to ignore alternate fields entirely.

In this pull request I first modified the spec so that it uses Net::LDAP::Entry instead of a simple hash. I also changed the `:mail` field to `:email` so that the fallbacks are used. This created a failing spec, matching my original problem. I then simply modified map_user to look for empty arrays instead of falsy values (which `[]` is not). [It can only contain arrays](https://github.com/ruby-ldap/ruby-net-ldap/blob/master/lib/net/ldap/entry.rb#L104), so this should be fine.