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',
18 validate :public_key_must_be_unique
20 api_accessible :user, extend: :common do |t|
23 t.add :authorized_user_uuid
28 def permission_to_set_authorized_user_uuid
29 # Anonymous users cannot do anything here
30 return false if !current_user
32 # Administrators can attach a key to any user account
33 return true if current_user.is_admin
35 # All users can attach keys to their own accounts
36 return true if current_user.uuid == authorized_user_uuid
42 def public_key_must_be_unique
44 # Valid if no other rows have this public key
45 if self.class.where('uuid != ? and public_key like ?',
46 uuid || '', "%#{self.public_key}%").any?
47 errors.add(:public_key, "already exists in the database, use a different key.")