debian-mirror-gitlab/app/models/key.rb

94 lines
2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# == Schema Information
#
# Table name: keys
#
# id :integer not null, primary key
# user_id :integer
# created_at :datetime
# updated_at :datetime
# key :text
# title :string(255)
# type :string(255)
# fingerprint :string(255)
2015-09-11 14:41:01 +05:30
# public :boolean default(FALSE), not null
2014-09-02 18:07:02 +05:30
#
require 'digest/md5'
class Key < ActiveRecord::Base
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
NotificationService.new.new_key(self)
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