Class: Gitlab::Auth::OAuth::User

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/auth/o_auth/user.rb

Direct Known Subclasses

Atlassian::User, Ldap::User, Saml::User

Constant Summary collapse

SignupDisabledError =
Class.new(StandardError)
SigninDisabledForProviderError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_hash) ⇒ User

Returns a new instance of User.


17
18
19
20
21
# File 'lib/gitlab/auth/o_auth/user.rb', line 17

def initialize(auth_hash)
  self.auth_hash = auth_hash
  update_profile
  add_or_update_user_identities
end

Instance Attribute Details

#auth_hashObject

Returns the value of attribute auth_hash


15
16
17
# File 'lib/gitlab/auth/o_auth/user.rb', line 15

def auth_hash
  @auth_hash
end

Instance Method Details

#bypass_two_factor?Boolean

Returns:

  • (Boolean)

80
81
82
83
84
85
86
87
# File 'lib/gitlab/auth/o_auth/user.rb', line 80

def bypass_two_factor?
  providers = Gitlab.config.omniauth.allow_bypass_two_factor
  if providers.is_a?(Array)
    providers.include?(auth_hash.provider)
  else
    providers
  end
end

#find_and_update!Object


74
75
76
77
78
# File 'lib/gitlab/auth/o_auth/user.rb', line 74

def find_and_update!
  save if should_save?

  gl_user
end

#find_userObject


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gitlab/auth/o_auth/user.rb', line 62

def find_user
  user = find_by_uid_and_provider

  user ||= find_by_email if auto_link_user?
  user ||= find_or_build_ldap_user if auto_link_ldap_user?
  user ||= build_new_user if 

  user.external = true if external_provider? && user&.new_record?

  user
end

#gl_userObject


56
57
58
59
60
# File 'lib/gitlab/auth/o_auth/user.rb', line 56

def gl_user
  return @gl_user if defined?(@gl_user)

  @gl_user = find_user
end

#new?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/gitlab/auth/o_auth/user.rb', line 27

def new?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/gitlab/auth/o_auth/user.rb', line 23

def persisted?
  gl_user.try(:persisted?)
end

#save(provider = 'OAuth') ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitlab/auth/o_auth/user.rb', line 39

def save(provider = 'OAuth')
  raise SigninDisabledForProviderError if oauth_provider_disabled?
  raise SignupDisabledError unless gl_user

  block_after_save = needs_blocking?

  Users::UpdateService.new(gl_user, user: gl_user).execute!

  gl_user.block if block_after_save

  log.info "(#{provider}) saving user #{auth_hash.email} from login with admin => #{gl_user.admin}, extern_uid => #{auth_hash.uid}"
  gl_user
rescue ActiveRecord::RecordInvalid => e
  log.info "(#{provider}) Error saving user #{auth_hash.uid} (#{auth_hash.email}): #{gl_user.errors.full_messages}"
  [self, e.record.errors]
end

#valid?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/gitlab/auth/o_auth/user.rb', line 31

def valid?
  gl_user.try(:valid?)
end

#valid_sign_in?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/gitlab/auth/o_auth/user.rb', line 35

def valid_sign_in?
  valid? && persisted?
end