rename foreign uuid attributes
[arvados.git] / services / api / app / models / api_client_authorization.rb
1 class ApiClientAuthorization < ArvadosModel
2   include KindAndEtag
3   include CommonApiTemplate
4
5   belongs_to :api_client
6   belongs_to :user
7   after_initialize :assign_random_api_token
8
9   api_accessible :superuser, :extend => :common do |t|
10     t.add :owner_uuid
11     t.add :user_id
12     t.add :api_client_id
13     t.add :api_token
14     t.add :created_by_ip_address
15     t.add :default_owner_uuid
16     t.add :expires_at
17     t.add :last_used_at
18     t.add :last_used_by_ip_address
19   end
20
21   def assign_random_api_token
22     self.api_token ||= rand(2**256).to_s(36)
23   end
24
25   def owner_uuid
26     self.user.andand.uuid
27   end
28   def owner_uuid_was
29     self.user_id_changed? ? User.find(self.user_id_was).andand.uuid : self.user.andand.uuid
30   end
31   def owner_uuid_changed?
32     self.user_id_changed?
33   end
34
35   def uuid
36     self.api_token
37   end
38   def uuid=(x) end
39   def uuid_was
40     self.api_token_was
41   end
42   def uuid_changed?
43     self.api_token_changed?
44   end
45
46   def modified_by_client_uuid
47     nil
48   end
49   def modified_by_client_uuid=(x) end
50
51   def modified_by_user_uuid
52     nil
53   end
54   def modified_by_user_uuid=(x) end
55
56   def modified_at
57     nil
58   end
59   def modified_at=(x) end
60
61   protected
62
63   def permission_to_create
64     current_user.andand.is_admin or (current_user.andand.id == self.user_id)
65   end
66
67   def permission_to_update
68     (permission_to_create and
69      not self.user_id_changed? and
70      not self.owner_uuid_changed?)
71   end
72 end