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
|
2014-10-10 10:03:32 +00:00
|
|
|
attr_accessor :auth_hash, :gl_user
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def initialize(auth_hash)
|
|
|
|
self.auth_hash = auth_hash
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def persisted?
|
2014-10-16 18:08:30 +00:00
|
|
|
gl_user.try(:persisted?)
|
2014-10-10 10:03:32 +00:00
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def new?
|
2014-10-17 11:08:02 +00:00
|
|
|
!persisted?
|
2014-10-10 10:03:32 +00:00
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def valid?
|
2014-10-16 18:08:30 +00:00
|
|
|
gl_user.try(:valid?)
|
2014-09-03 13:59:50 +00:00
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def save
|
2014-10-16 18:08:30 +00:00
|
|
|
unauthorized_to_create unless gl_user
|
|
|
|
|
2014-10-17 11:08:02 +00:00
|
|
|
if needs_blocking?
|
|
|
|
gl_user.save!
|
|
|
|
gl_user.block
|
|
|
|
else
|
|
|
|
gl_user.save!
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-17 11:08:02 +00:00
|
|
|
log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
|
2014-10-10 10:03:32 +00:00
|
|
|
gl_user
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}"
|
|
|
|
return self, e.record.errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def gl_user
|
2014-10-16 18:08:30 +00:00
|
|
|
@user ||= find_by_uid_and_provider
|
|
|
|
|
2014-10-17 11:08:02 +00:00
|
|
|
if signup_enabled?
|
2014-10-16 18:08:30 +00:00
|
|
|
@user ||= build_new_user
|
|
|
|
end
|
2014-10-17 11:08:02 +00:00
|
|
|
|
2014-10-16 18:08:30 +00:00
|
|
|
@user
|
2014-09-03 13:59:50 +00:00
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
protected
|
2014-10-17 11:08:02 +00:00
|
|
|
|
|
|
|
def needs_blocking?
|
|
|
|
new? && block_after_signup?
|
|
|
|
end
|
|
|
|
|
|
|
|
def signup_enabled?
|
|
|
|
Gitlab.config.omniauth.allow_single_sign_on
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_after_signup?
|
|
|
|
Gitlab.config.omniauth.block_auto_created_users
|
|
|
|
end
|
|
|
|
|
2014-09-04 10:55:10 +00:00
|
|
|
def auth_hash=(auth_hash)
|
|
|
|
@auth_hash = AuthHash.new(auth_hash)
|
|
|
|
end
|
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def find_by_uid_and_provider
|
|
|
|
model.where(provider: auth_hash.provider, extern_uid: auth_hash.uid).last
|
|
|
|
end
|
2014-09-03 15:33:03 +00:00
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def build_new_user
|
|
|
|
model.new(user_attributes).tap do |user|
|
|
|
|
user.skip_confirmation!
|
|
|
|
end
|
2014-09-03 15:33:03 +00:00
|
|
|
end
|
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def user_attributes
|
|
|
|
{
|
2014-09-04 10:55:10 +00: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 13:59:50 +00:00
|
|
|
}
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
|
2014-09-03 13:59:50 +00:00
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
2014-10-10 10:03:32 +00:00
|
|
|
def model
|
|
|
|
::User
|
|
|
|
end
|
2014-10-16 18:08:30 +00:00
|
|
|
|
|
|
|
def raise_unauthorized_to_create
|
|
|
|
raise StandardError.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}")
|
|
|
|
end
|
2013-09-03 21:04:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|