d079952beed19178f8bfd99704225e56d417d268
[arvados.git] / services / api / app / models / authorized_key.rb
1 class AuthorizedKey < ArvadosModel
2   include AssignUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   before_create :permission_to_set_authorized_user_uuid
6   before_update :permission_to_set_authorized_user_uuid
7
8   belongs_to :authorized_user, :foreign_key => :authorized_user_uuid, :class_name => 'User', :primary_key => :uuid
9
10   api_accessible :superuser, :extend => :common do |t|
11     t.add :name
12     t.add :key_type
13     t.add :authorized_user_uuid
14     t.add :public_key
15     t.add :expires_at
16   end
17
18   def permission_to_set_authorized_user_uuid
19     # Anonymous users cannot do anything here
20     return false if !current_user
21
22     # Administrators can attach a key to any user account
23     return true if current_user.is_admin
24
25     # All users can attach keys to their own accounts
26     return true if current_user.uuid == authorized_user_uuid
27
28     # Default = deny.
29     false
30   end
31 end