Class: Key

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, Expirable, FromUnion, ShaAttribute, Sortable
Defined in:
app/models/key.rb

Direct Known Subclasses

DeployKey, GroupDeployKey

Constant Summary

Constants included from Expirable

Expirable::DAYS_TO_EXPIRE

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Expirable

#expired?, #expires?, #expires_soon?

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.regular_keysObject


52
53
54
# File 'app/models/key.rb', line 52

def self.regular_keys
  where(type: ['Key', nil])
end

Instance Method Details

#add_to_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass


88
89
90
91
92
# File 'app/models/key.rb', line 88

def add_to_authorized_keys
  return unless Gitlab::CurrentSettings.authorized_keys_enabled?

  AuthorizedKeysWorker.perform_async(:add_key, shell_id, key)
end

#can_delete?Boolean

EE overrides this

Returns:

  • (Boolean)

78
79
80
# File 'app/models/key.rb', line 78

def can_delete?
  true
end

#key=(value) ⇒ Object


56
57
58
59
60
# File 'app/models/key.rb', line 56

def key=(value)
  write_attribute(:key, value.present? ? Gitlab::SSHPublicKey.sanitize(value) : nil)

  @public_key = nil
end

#post_create_hookObject

rubocop: disable CodeReuse/ServiceClass


95
96
97
# File 'app/models/key.rb', line 95

def post_create_hook
  SystemHooksService.new.execute_hooks_for(self, :create)
end

#post_destroy_hookObject

rubocop: disable CodeReuse/ServiceClass


115
116
117
# File 'app/models/key.rb', line 115

def post_destroy_hook
  SystemHooksService.new.execute_hooks_for(self, :destroy)
end

#projectsObject

projects that has this key


69
70
71
# File 'app/models/key.rb', line 69

def projects
  user.authorized_projects
end

#public_keyObject

rubocop: enable CodeReuse/ServiceClass


120
121
122
# File 'app/models/key.rb', line 120

def public_key
  @public_key ||= Gitlab::SSHPublicKey.new(key)
end

#publishable_keyObject


62
63
64
65
66
# File 'app/models/key.rb', line 62

def publishable_key
  # Strip out the keys comment so we don't leak email addresses
  # Replace with simple ident of user_name (hostname)
  self.key.split[0..1].push("#{self.user_name} (#{Gitlab.config.gitlab.host})").join(' ')
end

#refresh_user_cacheObject

rubocop: disable CodeReuse/ServiceClass


107
108
109
110
111
# File 'app/models/key.rb', line 107

def refresh_user_cache
  return unless user

  Users::KeysCountService.new(user).refresh_cache
end

#remove_from_authorized_keysObject

rubocop: enable CodeReuse/ServiceClass


100
101
102
103
104
# File 'app/models/key.rb', line 100

def remove_from_authorized_keys
  return unless Gitlab::CurrentSettings.authorized_keys_enabled?

  AuthorizedKeysWorker.perform_async(:remove_key, shell_id)
end

#shell_idObject


73
74
75
# File 'app/models/key.rb', line 73

def shell_id
  "key-#{id}"
end

#update_last_used_atObject

rubocop: disable CodeReuse/ServiceClass


83
84
85
# File 'app/models/key.rb', line 83

def update_last_used_at
  Keys::LastUsedService.new(self).execute
end