key.rb 1.74 KB
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: keys
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6
#  id          :integer          not null, primary key
#  user_id     :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7 8
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9 10 11 12
#  key         :text
#  title       :string(255)
#  type        :string(255)
#  fingerprint :string(255)
13 14
#

miks's avatar
miks committed
15 16
require 'digest/md5'

gitlabhq's avatar
gitlabhq committed
17
class Key < ActiveRecord::Base
18
  include Sortable
19

gitlabhq's avatar
gitlabhq committed
20 21
  belongs_to :user

Steven Burgart's avatar
Steven Burgart committed
22
  before_validation :strip_white_space, :generate_fingerprint
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
23

Nihad Abbasov's avatar
Nihad Abbasov committed
24
  validates :title, presence: true, length: { within: 0..255 }
Akiva Levy's avatar
Akiva Levy committed
25
  validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }, uniqueness: true
26
  validates :fingerprint, uniqueness: true, presence: { message: 'cannot be generated' }
gitlabhq's avatar
gitlabhq committed
27

28
  delegate :name, :email, to: :user, prefix: true
miks's avatar
miks committed
29

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30 31
  after_create :add_to_shell
  after_create :notify_user
32
  after_create :post_create_hook
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33
  after_destroy :remove_from_shell
34
  after_destroy :post_destroy_hook
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
35

miks's avatar
miks committed
36
  def strip_white_space
37
    self.key = key.strip unless key.blank?
miks's avatar
miks committed
38 39
  end

40
  # projects that has this key
gitlabhq's avatar
gitlabhq committed
41
  def projects
42
    user.authorized_projects
gitlabhq's avatar
gitlabhq committed
43
  end
44

45
  def shell_id
46
    "key-#{id}"
47
  end
48

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
49 50 51 52 53 54 55 56 57 58 59 60
  def add_to_shell
    GitlabShellWorker.perform_async(
      :add_key,
      shell_id,
      key
    )
  end

  def notify_user
    NotificationService.new.new_key(self)
  end

61 62 63 64
  def post_create_hook
    SystemHooksService.new.execute_hooks_for(self, :create)
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65 66 67 68 69 70 71 72
  def remove_from_shell
    GitlabShellWorker.perform_async(
      :remove_key,
      shell_id,
      key,
    )
  end

73 74 75 76
  def post_destroy_hook
    SystemHooksService.new.execute_hooks_for(self, :destroy)
  end

77 78
  private

Steven Burgart's avatar
Steven Burgart committed
79
  def generate_fingerprint
80
    self.fingerprint = nil
81 82 83 84

    return unless self.key.present?

    self.fingerprint = Gitlab::KeyFingerprint.new(self.key).fingerprint
85
  end
gitlabhq's avatar
gitlabhq committed
86
end