Module: Gitlab::Identifier

Included in:
API::Support::GitAccessActor, GitPostReceive
Defined in:
lib/gitlab/identifier.rb

Instance Method Summary collapse

Instance Method Details

#identification_cacheObject



46
47
48
49
50
51
52
# File 'lib/gitlab/identifier.rb', line 46

def identification_cache
  @identification_cache ||= {
    email: {},
    user: {},
    ssh_key: {}
  }
end

#identify(identifier) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/gitlab/identifier.rb', line 7

def identify(identifier)
  case identifier
  when /\Auser-\d+\Z/
    # git push over http
    identify_using_user(identifier)
  when /\Akey-\d+\Z/
    # git push over ssh
    identify_using_ssh_key(identifier)
  end
end

#identify_using_ssh_key(identifier) ⇒ Object

Tries to identify a user based on an SSH key identifier (e.g. “key-123”).



30
31
32
33
34
35
36
# File 'lib/gitlab/identifier.rb', line 30

def identify_using_ssh_key(identifier)
  key_id = identifier.gsub("key-", "")

  identify_with_cache(:ssh_key, key_id) do
    User.find_by_ssh_key_id(key_id)
  end
end

#identify_using_user(identifier) ⇒ Object

Tries to identify a user based on a user identifier (e.g. “user-123”). rubocop: disable CodeReuse/ActiveRecord



20
21
22
23
24
25
26
# File 'lib/gitlab/identifier.rb', line 20

def identify_using_user(identifier)
  user_id = identifier.gsub("user-", "")

  identify_with_cache(:user, user_id) do
    User.find_by(id: user_id)
  end
end

#identify_with_cache(category, key) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/gitlab/identifier.rb', line 38

def identify_with_cache(category, key)
  if identification_cache[category].key?(key)
    identification_cache[category][key]
  else
    identification_cache[category][key] = yield
  end
end