2013-09-03 17:06:13 -04:00
|
|
|
require 'gitlab/oauth/user'
|
|
|
|
|
2013-09-02 16:35:40 -04:00
|
|
|
# LDAP extension for User model
|
|
|
|
#
|
|
|
|
# * Find or create user from omniauth.auth data
|
|
|
|
# * Links LDAP account with existing user
|
2013-09-03 17:06:13 -04:00
|
|
|
# * Auth LDAP user with login and password
|
2013-09-02 16:35:40 -04:00
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module LDAP
|
2013-09-03 17:06:13 -04:00
|
|
|
class User < Gitlab::OAuth::User
|
2013-09-02 16:35:40 -04:00
|
|
|
class << self
|
2014-10-13 11:24:05 -04:00
|
|
|
def find_by_uid_and_provider(uid, provider)
|
2014-01-30 07:11:19 -05:00
|
|
|
# LDAP distinguished name is case-insensitive
|
2014-11-25 11:15:30 -05:00
|
|
|
identity = ::Identity.
|
2014-10-13 11:24:05 -04:00
|
|
|
where(provider: [provider, :ldap]).
|
|
|
|
where('lower(extern_uid) = ?', uid.downcase).last
|
2014-11-25 11:15:30 -05:00
|
|
|
identity && identity.user
|
2013-09-02 16:35:40 -04:00
|
|
|
end
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
2013-09-02 16:35:40 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def initialize(auth_hash)
|
|
|
|
super
|
|
|
|
update_user_attributes
|
|
|
|
end
|
2013-09-02 16:35:40 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
# instance methods
|
|
|
|
def gl_user
|
|
|
|
@gl_user ||= find_by_uid_and_provider || find_by_email || build_new_user
|
|
|
|
end
|
2014-09-08 08:53:59 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def find_by_uid_and_provider
|
2014-10-14 03:05:29 -04:00
|
|
|
self.class.find_by_uid_and_provider(
|
2014-10-14 03:40:35 -04:00
|
|
|
auth_hash.uid.downcase, auth_hash.provider)
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_email
|
2014-11-27 06:34:39 -05:00
|
|
|
::User.find_by(email: auth_hash.email)
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_user_attributes
|
2014-11-27 06:34:39 -05:00
|
|
|
gl_user.email = auth_hash.email
|
2015-01-29 16:28:41 -05:00
|
|
|
|
|
|
|
# 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-11-27 06:34:39 -05:00
|
|
|
gl_user
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def changed?
|
2015-01-29 16:28:41 -05:00
|
|
|
gl_user.changed? || gl_user.identities.any?(&:changed?)
|
2013-09-02 16:35:40 -04:00
|
|
|
end
|
2014-09-03 11:33:03 -04:00
|
|
|
|
|
|
|
def needs_blocking?
|
|
|
|
false
|
|
|
|
end
|
2014-10-13 11:24:05 -04:00
|
|
|
|
|
|
|
def allowed?
|
|
|
|
Gitlab::LDAP::Access.allowed?(gl_user)
|
|
|
|
end
|
2013-09-02 16:35:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|