2013-09-03 21:04:27 +00: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-03 15:33:03 +00:00
|
|
|
attr_accessor :auth
|
2013-09-03 21:04:27 +00:00
|
|
|
|
|
|
|
def find(auth)
|
2014-09-03 15:33:03 +00:00
|
|
|
self.auth = auth
|
2013-09-03 21:04:27 +00:00
|
|
|
find_by_uid_and_provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(auth)
|
2014-09-03 15:33:03 +00:00
|
|
|
user = new(auth)
|
|
|
|
user.save_and_trigger_callbacks
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
|
|
|
|
2014-09-03 15:33:03 +00:00
|
|
|
def model
|
|
|
|
::User
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 15:33:03 +00:00
|
|
|
protected
|
2013-09-03 21:04:27 +00:00
|
|
|
def find_by_uid_and_provider
|
2014-09-03 15:33:03 +00:00
|
|
|
model.where(provider: provider, extern_uid: uid).last
|
2014-09-03 13:59:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
auth.provider
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
2014-09-01 14:31:27 +00:00
|
|
|
auth.uid.to_s
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
2014-09-03 13:59:50 +00:00
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
attr_accessor :auth, :user
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def initialize(auth)
|
|
|
|
self.auth = auth
|
2014-09-03 15:33:03 +00:00
|
|
|
self.user = self.class.model.new(user_attributes)
|
2014-09-03 13:59:50 +00:00
|
|
|
user.skip_confirmation!
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 15:33:03 +00:00
|
|
|
def save_and_trigger_callbacks
|
|
|
|
user.save!
|
|
|
|
log.info "(OAuth) Creating user #{email} from login with extern_uid => #{uid}"
|
|
|
|
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 13:59:50 +00:00
|
|
|
def user_attributes
|
|
|
|
{
|
|
|
|
extern_uid: uid,
|
|
|
|
provider: provider,
|
|
|
|
name: name,
|
|
|
|
username: username,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
}
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def uid
|
|
|
|
auth.uid.to_s
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def provider
|
|
|
|
auth.provider
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def info
|
|
|
|
auth.info
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def name
|
|
|
|
(info.name || full_name).to_s.force_encoding('utf-8')
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
"#{info.first_name} #{info.last_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
(info.try(:nickname) || generate_username).to_s.force_encoding('utf-8')
|
|
|
|
end
|
|
|
|
|
|
|
|
def email
|
|
|
|
(info.try(:email) || generate_temporarily_email).downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def password
|
|
|
|
@password ||= Devise.friendly_token[0, 8].downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_error(message)
|
|
|
|
raise OmniAuth::Error, "(OAuth) " + message
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the first part of the email address (before @)
|
|
|
|
# In addtion in removes illegal characters
|
|
|
|
def generate_username
|
|
|
|
email.match(/^[^@]*/)[0].parameterize
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_temporarily_email
|
|
|
|
"temp-email-for-oauth-#{username}@gitlab.localhost"
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
2014-09-03 15:33:03 +00:00
|
|
|
|
|
|
|
def needs_blocking?
|
|
|
|
Gitlab.config.omniauth['block_auto_created_users']
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|