@@ -26,10 +26,8 @@ class User < ActiveRecord::Base
2626 # Include default devise modules. Others available are:
2727 # :confirmable, :lockable, :timeoutable
2828 # Registration is controlled via settings.yml
29- devise_list = [ :database_authenticatable , :invitable , :omniauthable ,
30- :recoverable , :rememberable , :trackable , :validatable ]
31- devise_list << :registerable if Settings . auth . registerable
32- devise_list << { authentication_keys : [ :login ] }
29+ devise_list = [ :omniauthable , :rememberable , :trackable , omniauth_providers : [ :shibboleth ] , authentication_keys : [ :login ] ]
30+ devise_list . prepend ( :database_authenticatable ) if AuthConfig . use_database_auth?
3331
3432 devise ( *devise_list )
3533
@@ -158,6 +156,32 @@ def self.walk_ldap_groups(groups, seen)
158156 end
159157 seen
160158 end
159+
160+ # When a user authenticates via shibboleth, find their User object or make
161+ # a new one. Populate it with data we get from shibboleth.
162+ # @param [OmniAuth::AuthHash] auth
163+ def self . from_omniauth ( auth )
164+ begin
165+ user = find_by! ( provider : auth . provider , username : auth . uid . downcase )
166+ rescue ActiveRecord ::RecordNotFound
167+ log_omniauth_error ( auth )
168+ return User . new
169+ end
170+ user . assign_attributes ( display_name : auth . info . display_name , ppid : auth . uid , uid : auth . info . uid )
171+ # [email protected] isn't a real email address 172+ user . email = auth . info . uid + '@emory.edu' unless auth . info . uid == 'tezprox'
173+ user . save
174+ user
175+ end
176+
177+ def self . log_omniauth_error ( auth )
178+ if auth . info . uid . empty?
179+ Rails . logger . error "Nil user detected: Shibboleth didn't pass a uid for #{ auth . inspect } "
180+ else
181+ # Log unauthorized logins to error.
182+ Rails . logger . error "Unauthorized user attemped login: #{ auth . inspect } "
183+ end
184+ end
161185end
162186
163187class Avalon ::MissingUserId < StandardError ; end
0 commit comments