2013-09-03 17:04:27 -04:00
|
|
|
# OAuth extension for User model
|
|
|
|
#
|
|
|
|
# * Find GitLab user based on omniauth uid and provider
|
|
|
|
# * Create new user from omniauth data
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module OAuth
|
|
|
|
class User
|
|
|
|
class << self
|
2014-09-04 06:55:10 -04:00
|
|
|
attr_reader :auth_hash
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def find(auth_hash)
|
|
|
|
self.auth_hash = auth_hash
|
2013-09-03 17:04:27 -04:00
|
|
|
find_by_uid_and_provider
|
|
|
|
end
|
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def create(auth_hash)
|
|
|
|
user = new(auth_hash)
|
2014-09-03 11:33:03 -04:00
|
|
|
user.save_and_trigger_callbacks
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
|
|
|
|
2014-09-03 11:33:03 -04:00
|
|
|
def model
|
|
|
|
::User
|
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def auth_hash=(auth_hash)
|
|
|
|
@auth_hash = AuthHash.new(auth_hash)
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
protected
|
|
|
|
def find_by_uid_and_provider
|
|
|
|
model.where(provider: auth_hash.provider, extern_uid: auth_hash.uid).last
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
2014-09-03 09:59:50 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
# Instance methods
|
|
|
|
attr_accessor :auth_hash, :user
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def initialize(auth_hash)
|
|
|
|
self.auth_hash = auth_hash
|
2014-09-03 11:33:03 -04:00
|
|
|
self.user = self.class.model.new(user_attributes)
|
2014-09-03 09:59:50 -04:00
|
|
|
user.skip_confirmation!
|
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def auth_hash=(auth_hash)
|
|
|
|
@auth_hash = AuthHash.new(auth_hash)
|
|
|
|
end
|
|
|
|
|
2014-09-03 11:33:03 -04:00
|
|
|
def save_and_trigger_callbacks
|
|
|
|
user.save!
|
2014-09-04 06:55:10 -04:00
|
|
|
log.info "(OAuth) Creating user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
|
2014-09-03 11:33:03 -04:00
|
|
|
user.block if needs_blocking?
|
|
|
|
|
|
|
|
user
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
log.info "(OAuth) Email #{e.record.errors[:email]}. Username #{e.record.errors[:username]}"
|
|
|
|
return nil, e.record.errors
|
|
|
|
end
|
|
|
|
|
2014-09-03 09:59:50 -04:00
|
|
|
def user_attributes
|
|
|
|
{
|
2014-09-04 06:55:10 -04:00
|
|
|
extern_uid: auth_hash.uid,
|
|
|
|
provider: auth_hash.provider,
|
|
|
|
name: auth_hash.name,
|
|
|
|
username: auth_hash.username,
|
|
|
|
email: auth_hash.email,
|
|
|
|
password: auth_hash.password,
|
|
|
|
password_confirmation: auth_hash.password,
|
2014-09-03 09:59:50 -04:00
|
|
|
}
|
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-09-03 09:59:50 -04:00
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_error(message)
|
|
|
|
raise OmniAuth::Error, "(OAuth) " + message
|
|
|
|
end
|
|
|
|
|
2014-09-03 11:33:03 -04:00
|
|
|
def needs_blocking?
|
|
|
|
Gitlab.config.omniauth['block_auto_created_users']
|
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|