October 14

0 comments

Rails – Adding the user’s active directory email to the User model when using Adauth

By Christopher Mendla

October 14, 2016


Last Updated on November 30, 2019 by Christopher G Mendla

I use adauth to authenticate users in my Rails apps using their Active Directory information. A model and table called User(s) gets created.

However, the standard setup for Adauth does not grab the user’s email address as it appears in Active Directory. You can grab and store the email using the following method.

If you want adauth to automatically populate the email field of your user model, you need to 
  1. Add a field to the user model called email – varchar
  2. Add the following to your user model 
    1. Adauth.add_field(Adauth::AdObjects::User:mail:mail
    2.  :email => :mail

This will then cause adauth to grab the user email from active directory and populate the user model when the user logs on. 

Your user model will need the following


 include Adauth::Rails::ModelBridge
 Adauth.add_field(Adauth::AdObjects::User:mail:mail)
 AdauthMappings = {
     :login => :login,                #was login  login     :group_strings => :cn_groups,     :ou_strings => :dn_ous,     :email => :mail,     :name => :name }


 AdauthSearchField = [:login:login] def groups
   group_strings.split(“, “)
 end
end
end

Christopher Mendla

About the author

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}