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, {
13 foreign_key: 'authorized_user_uuid',
19 validate :public_key_must_be_unique
21 api_accessible :user, extend: :common do |t|
24 t.add :authorized_user_uuid
29 def permission_to_set_authorized_user_uuid
30 # Anonymous users cannot do anything here
31 return false if !current_user
33 # Administrators can attach a key to any user account
34 return true if current_user.is_admin
36 # All users can attach keys to their own accounts
37 return true if current_user.uuid == authorized_user_uuid
43 def public_key_must_be_unique
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.")