2014-09-02 18:07:02 +05:30
|
|
|
require 'digest/md5'
|
|
|
|
|
|
|
|
class Key < ActiveRecord::Base
|
2016-06-02 11:05:42 +05:30
|
|
|
include AfterCommitQueue
|
2015-04-26 12:48:37 +05:30
|
|
|
include Sortable
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
before_validation :strip_white_space, :generate_fingerprint
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
validates :title, presence: true, length: { within: 0..255 }
|
|
|
|
validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }, uniqueness: true
|
2015-09-11 14:41:01 +05:30
|
|
|
validates :key, format: { without: /\n|\r/, message: 'should be a single line' }
|
2014-09-02 18:07:02 +05:30
|
|
|
validates :fingerprint, uniqueness: true, presence: { message: 'cannot be generated' }
|
|
|
|
|
|
|
|
delegate :name, :email, to: :user, prefix: true
|
|
|
|
|
|
|
|
after_create :add_to_shell
|
|
|
|
after_create :notify_user
|
2015-04-26 12:48:37 +05:30
|
|
|
after_create :post_create_hook
|
2014-09-02 18:07:02 +05:30
|
|
|
after_destroy :remove_from_shell
|
2015-04-26 12:48:37 +05:30
|
|
|
after_destroy :post_destroy_hook
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def strip_white_space
|
|
|
|
self.key = key.strip unless key.blank?
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def publishable_key
|
|
|
|
#Removes anything beyond the keytype and key itself
|
|
|
|
self.key.split[0..1].join(' ')
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# projects that has this key
|
|
|
|
def projects
|
|
|
|
user.authorized_projects
|
|
|
|
end
|
|
|
|
|
|
|
|
def shell_id
|
|
|
|
"key-#{id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:add_key,
|
|
|
|
shell_id,
|
|
|
|
key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_user
|
2016-06-02 11:05:42 +05:30
|
|
|
run_after_commit { NotificationService.new.new_key(self) }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def post_create_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def remove_from_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:remove_key,
|
|
|
|
shell_id,
|
|
|
|
key,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def post_destroy_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
private
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def generate_fingerprint
|
2014-09-02 18:07:02 +05:30
|
|
|
self.fingerprint = nil
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
return unless self.key.present?
|
|
|
|
|
|
|
|
self.fingerprint = Gitlab::KeyFingerprint.new(self.key).fingerprint
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|