1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class AuthorizedKey < ArvadosModel
8 include CommonApiTemplate
9 before_create :permission_to_set_authorized_user_uuid
10 before_update :permission_to_set_authorized_user_uuid
12 belongs_to :authorized_user, :foreign_key => :authorized_user_uuid, :class_name => 'User', :primary_key => :uuid
14 validate :public_key_must_be_unique
16 api_accessible :user, extend: :common do |t|
19 t.add :authorized_user_uuid
24 def permission_to_set_authorized_user_uuid
25 # Anonymous users cannot do anything here
26 return false if !current_user
28 # Administrators can attach a key to any user account
29 return true if current_user.is_admin
31 # All users can attach keys to their own accounts
32 return true if current_user.uuid == authorized_user_uuid
38 def public_key_must_be_unique
40 valid_key = SSHKey.valid_ssh_public_key? self.public_key
43 errors.add(:public_key, "does not appear to be a valid ssh-rsa or dsa public key")
45 # Valid if no other rows have this public key
46 if self.class.where('uuid != ? and public_key like ?',
47 uuid || '', "%#{self.public_key}%").any?
48 errors.add(:public_key, "already exists in the database, use a different key.")