2013-02-12 21:47:22 +08:00
# -*- encoding : utf-8 -*-
require_dependency 'email'
2013-03-04 13:44:41 -05:00
require_dependency 'enum'
2013-06-06 16:40:10 +02:00
require_dependency 'user_name_suggester'
2013-03-04 13:44:41 -05:00
2013-02-12 21:47:22 +08:00
class Users :: OmniauthCallbacksController < ApplicationController
2013-08-23 16:20:43 +10:00
BUILTIN_AUTH = [
Auth :: FacebookAuthenticator . new ,
2013-08-26 11:04:16 +10:00
Auth :: OpenIdAuthenticator . new ( " google " , " https://www.google.com/accounts/o8/id " , trusted : true ) ,
Auth :: OpenIdAuthenticator . new ( " yahoo " , " https://me.yahoo.com " , trusted : true ) ,
2013-08-23 16:20:43 +10:00
Auth :: GithubAuthenticator . new ,
Auth :: TwitterAuthenticator . new ,
2013-08-28 14:32:51 +02:00
Auth :: PersonaAuthenticator . new ,
Auth :: CasAuthenticator . new
2013-08-23 16:20:43 +10:00
]
2013-06-05 10:30:51 +10:00
skip_before_filter :redirect_to_login_if_required
2013-02-12 21:47:22 +08:00
layout false
2013-03-04 13:44:41 -05:00
def self . types
2013-05-23 13:40:50 -07:00
@types || = Enum . new ( :facebook , :twitter , :google , :yahoo , :github , :persona , :cas )
2013-03-04 13:44:41 -05:00
end
2013-02-12 21:47:22 +08:00
# need to be able to call this
skip_before_filter :check_xhr
2013-07-29 15:13:13 +10:00
# this is the only spot where we allow CSRF, our openid / oauth redirect
# will not have a CSRF token, however the payload is all validated so its safe
2013-03-23 20:32:59 +05:30
skip_before_filter :verify_authenticity_token , only : :complete
2013-02-12 21:47:22 +08:00
def complete
2013-08-23 16:20:43 +10:00
auth = request . env [ " omniauth.auth " ]
2013-08-01 15:59:57 +10:00
2013-08-23 16:20:43 +10:00
authenticator = self . class . find_authenticator ( params [ :provider ] )
2013-03-04 13:44:41 -05:00
2013-08-23 16:20:43 +10:00
@data = authenticator . after_authenticate ( auth )
@data . authenticator_name = authenticator . name
2013-08-01 15:59:57 +10:00
2013-08-28 17:18:31 +10:00
if @data . user
user_found ( @data . user )
elsif SiteSetting . invite_only?
@data . requires_invite = true
else
session [ :authentication ] = @data . session_data
end
2013-06-05 11:11:02 -07:00
2013-03-01 13:22:54 -06:00
respond_to do | format |
format . html
2013-03-22 14:08:11 -04:00
format . json { render json : @data }
2013-03-01 13:22:54 -06:00
end
2013-02-12 21:47:22 +08:00
end
2013-02-14 11:11:13 -08:00
def failure
flash [ :error ] = I18n . t ( " login.omniauth_error " , strategy : params [ :strategy ] . titleize )
2013-03-22 14:08:11 -04:00
render layout : 'no_js'
2013-02-14 11:11:13 -08:00
end
2013-08-17 21:43:59 -07:00
2013-08-23 16:20:43 +10:00
def self . find_authenticator ( name )
BUILTIN_AUTH . each do | authenticator |
if authenticator . name == name
raise Discourse :: InvalidAccess . new ( " provider is not enabled " ) unless SiteSetting . send ( " enable_ #{ name } _logins? " )
2013-08-17 21:43:59 -07:00
2013-08-23 16:20:43 +10:00
return authenticator
2013-08-17 21:43:59 -07:00
end
end
2013-02-12 21:47:22 +08:00
2013-08-23 16:20:43 +10:00
Discourse . auth_providers . each do | provider |
if provider . name == name
2013-02-12 21:47:22 +08:00
2013-08-23 16:20:43 +10:00
return provider . authenticator
2013-02-12 21:47:22 +08:00
end
end
2013-03-01 09:23:21 -06:00
2013-08-23 16:20:43 +10:00
raise Discourse :: InvalidAccess . new ( " provider is not found " )
2013-02-12 21:47:22 +08:00
end
2013-08-23 16:20:43 +10:00
protected
2013-02-26 04:28:32 +00:00
2013-08-23 16:20:43 +10:00
def user_found ( user )
# automatically activate any account if a provider marked the email valid
if ! user . active && @data . email_valid
user . toggle ( :active ) . save
2013-08-02 12:03:53 +10:00
end
2013-08-23 16:20:43 +10:00
# log on any account that is active with forum access
if Guardian . new ( user ) . can_access_forum? && user . active
log_on_user ( user )
2013-08-27 15:56:12 +10:00
# don't carry around old auth info, perhaps move elsewhere
session [ :authentication ] = nil
2013-08-23 16:20:43 +10:00
@data . authenticated = true
2013-03-01 09:23:21 -06:00
else
2013-08-28 17:18:31 +10:00
if SiteSetting . must_approve_users? && ! user . approved?
2013-08-23 16:20:43 +10:00
@data . awaiting_approval = true
2013-07-11 16:02:18 +10:00
else
2013-08-23 16:20:43 +10:00
@data . awaiting_activation = true
2013-07-11 16:02:18 +10:00
end
end
end
2013-02-12 21:47:22 +08:00
end