2012-02-07 21:56:53 +00:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2011-10-08 21:36:38 +00:00
|
|
|
class Key < ActiveRecord::Base
|
2016-03-10 16:15:14 +00:00
|
|
|
include AfterCommitQueue
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
2013-06-24 17:07:21 +00:00
|
|
|
|
2011-10-08 21:36:38 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2015-01-18 15:29:37 +00:00
|
|
|
before_validation :strip_white_space, :generate_fingerprint
|
2012-10-09 00:10:04 +00:00
|
|
|
|
2012-09-27 06:20:36 +00:00
|
|
|
validates :title, presence: true, length: { within: 0..255 }
|
2013-07-10 22:19:52 +00:00
|
|
|
validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }, uniqueness: true
|
2015-07-11 18:18:56 +00:00
|
|
|
validates :key, format: { without: /\n|\r/, message: 'should be a single line' }
|
2013-07-17 13:16:34 +00:00
|
|
|
validates :fingerprint, uniqueness: true, presence: { message: 'cannot be generated' }
|
2011-10-08 21:36:38 +00:00
|
|
|
|
2012-08-10 22:07:50 +00:00
|
|
|
delegate :name, :email, to: :user, prefix: true
|
2012-02-07 21:56:53 +00:00
|
|
|
|
2014-04-02 18:13:05 +00:00
|
|
|
after_create :add_to_shell
|
|
|
|
after_create :notify_user
|
2014-08-18 14:46:46 +00:00
|
|
|
after_create :post_create_hook
|
2014-04-02 18:13:05 +00:00
|
|
|
after_destroy :remove_from_shell
|
2014-08-18 14:46:46 +00:00
|
|
|
after_destroy :post_destroy_hook
|
2014-04-02 18:13:05 +00:00
|
|
|
|
2012-02-07 22:32:20 +00:00
|
|
|
def strip_white_space
|
2013-05-06 09:26:36 +00:00
|
|
|
self.key = key.strip unless key.blank?
|
2012-02-07 22:32:20 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 17:17:34 +00:00
|
|
|
def publishable_key
|
|
|
|
#Removes anything beyond the keytype and key itself
|
|
|
|
self.key.split[0..1].join(' ')
|
|
|
|
end
|
|
|
|
|
2012-06-07 12:44:57 +00:00
|
|
|
# projects that has this key
|
2011-10-08 21:36:38 +00:00
|
|
|
def projects
|
2013-05-06 09:26:36 +00:00
|
|
|
user.authorized_projects
|
2011-10-08 21:36:38 +00:00
|
|
|
end
|
2012-08-28 21:04:06 +00:00
|
|
|
|
2013-02-05 09:12:15 +00:00
|
|
|
def shell_id
|
2013-05-06 09:26:36 +00:00
|
|
|
"key-#{id}"
|
2013-02-04 13:07:56 +00:00
|
|
|
end
|
2013-06-24 17:07:21 +00:00
|
|
|
|
2014-04-02 18:13:05 +00:00
|
|
|
def add_to_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:add_key,
|
|
|
|
shell_id,
|
|
|
|
key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_user
|
2016-03-10 16:15:14 +00:00
|
|
|
run_after_commit { NotificationService.new.new_key(self) }
|
2014-04-02 18:13:05 +00:00
|
|
|
end
|
|
|
|
|
2014-08-18 14:46:46 +00:00
|
|
|
def post_create_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
2014-04-02 18:13:05 +00:00
|
|
|
def remove_from_shell
|
|
|
|
GitlabShellWorker.perform_async(
|
|
|
|
:remove_key,
|
|
|
|
shell_id,
|
|
|
|
key,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-08-18 14:46:46 +00:00
|
|
|
def post_destroy_hook
|
|
|
|
SystemHooksService.new.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
2013-06-24 17:07:21 +00:00
|
|
|
private
|
|
|
|
|
2015-01-18 15:29:37 +00:00
|
|
|
def generate_fingerprint
|
2013-07-17 13:16:34 +00:00
|
|
|
self.fingerprint = nil
|
2015-04-14 10:00:43 +00:00
|
|
|
|
|
|
|
return unless self.key.present?
|
|
|
|
|
|
|
|
self.fingerprint = Gitlab::KeyFingerprint.new(self.key).fingerprint
|
2013-06-24 17:07:21 +00:00
|
|
|
end
|
2011-10-08 21:36:38 +00:00
|
|
|
end
|