debian-mirror-gitlab/lib/gitlab/ldap/user.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'gitlab/o_auth/user'
2014-09-02 18:07:02 +05:30
# LDAP extension for User model
#
# * Find or create user from omniauth.auth data
# * Links LDAP account with existing user
# * Auth LDAP user with login and password
#
module Gitlab
module LDAP
class User < Gitlab::OAuth::User
class << self
2015-04-26 12:48:37 +05:30
def find_by_uid_and_provider(uid, provider)
# LDAP distinguished name is case-insensitive
identity = ::Identity.
where(provider: provider).
where('lower(extern_uid) = ?', uid.downcase).last
identity && identity.user
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def initialize(auth_hash)
super
update_user_attributes
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
# instance methods
def gl_user
@gl_user ||= find_by_uid_and_provider || find_by_email || build_new_user
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def find_by_uid_and_provider
self.class.find_by_uid_and_provider(
auth_hash.uid.downcase, auth_hash.provider)
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def find_by_email
::User.find_by(email: auth_hash.email)
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def update_user_attributes
return unless persisted?
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
gl_user.skip_reconfirmation!
gl_user.email = auth_hash.email
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
# Build new identity only if we dont have have same one
gl_user.identities.find_or_initialize_by(provider: auth_hash.provider,
extern_uid: auth_hash.uid)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
gl_user
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def changed?
gl_user.changed? || gl_user.identities.any?(&:changed?)
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def block_after_signup?
ldap_config.block_auto_created_users
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def allowed?
Gitlab::LDAP::Access.allowed?(gl_user)
end
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
def ldap_config
Gitlab::LDAP::Config.new(auth_hash.provider)
2014-09-02 18:07:02 +05:30
end
end
end
end