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
|
|
|
|
attr_reader :auth
|
|
|
|
|
|
|
|
def find(auth)
|
|
|
|
@auth = auth
|
|
|
|
find_by_uid_and_provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(auth)
|
|
|
|
@auth = auth
|
|
|
|
password = Devise.friendly_token[0, 8].downcase
|
|
|
|
opts = {
|
|
|
|
extern_uid: uid,
|
|
|
|
provider: provider,
|
|
|
|
name: name,
|
|
|
|
username: username,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
}
|
|
|
|
|
2013-09-11 14:03:26 -04:00
|
|
|
user = model.build_user(opts, as: :admin)
|
2013-12-09 05:06:12 -05:00
|
|
|
user.skip_confirmation!
|
2014-04-07 07:09:29 -04:00
|
|
|
|
2014-04-07 09:36:33 -04:00
|
|
|
# Services like twitter and github does not return email via oauth
|
|
|
|
# In this case we generate temporary email and force user to fill it later
|
2014-04-07 07:09:29 -04:00
|
|
|
if user.email.blank?
|
|
|
|
user.generate_tmp_oauth_email
|
2014-04-07 09:36:33 -04:00
|
|
|
user.save!(validate: false)
|
|
|
|
else
|
|
|
|
# Google oauth returns email but dont return nickname
|
|
|
|
# So we use part of email as username for new user
|
|
|
|
user.username = email.match(/^[^@]*/)[0]
|
|
|
|
user.save
|
2014-04-07 07:09:29 -04:00
|
|
|
end
|
|
|
|
|
2013-09-03 17:04:27 -04:00
|
|
|
log.info "(OAuth) Creating user #{email} from login with extern_uid => #{uid}"
|
|
|
|
|
|
|
|
if Gitlab.config.omniauth['block_auto_created_users'] && !ldap?
|
|
|
|
user.block
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_by_uid_and_provider
|
|
|
|
model.where(provider: provider, extern_uid: uid).last
|
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
|
|
|
auth.info.uid || auth.uid
|
|
|
|
end
|
|
|
|
|
|
|
|
def email
|
|
|
|
auth.info.email.downcase unless auth.info.email.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
auth.info.name.to_s.force_encoding("utf-8")
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2014-04-07 07:09:29 -04:00
|
|
|
auth.info.nickname.to_s.force_encoding("utf-8")
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
auth.provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
|
|
|
def model
|
|
|
|
::User
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_error(message)
|
|
|
|
raise OmniAuth::Error, "(OAuth) " + message
|
|
|
|
end
|
|
|
|
|
|
|
|
def ldap?
|
|
|
|
provider == 'ldap'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|