api: Don't log common changes to API tokens.
[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   serialize :scopes, Array
9
10   api_accessible :user, extend: :common do |t|
11     t.add :owner_uuid
12     t.add :user_id
13     t.add :api_client_id
14     t.add :api_token
15     t.add :created_by_ip_address
16     t.add :default_owner_uuid
17     t.add :expires_at
18     t.add :last_used_at
19     t.add :last_used_by_ip_address
20     t.add :scopes
21   end
22
23   UNLOGGED_ATTRIBUTES = ['last_used_at', 'last_used_by_ip_address',
24                          'updated_at']
25
26   def assign_random_api_token
27     self.api_token ||= rand(2**256).to_s(36)
28   end
29
30   def owner_uuid
31     self.user.andand.uuid
32   end
33   def owner_uuid_was
34     self.user_id_changed? ? User.find(self.user_id_was).andand.uuid : self.user.andand.uuid
35   end
36   def owner_uuid_changed?
37     self.user_id_changed?
38   end
39
40   def uuid
41     self.api_token
42   end
43   def uuid=(x) end
44   def uuid_was
45     self.api_token_was
46   end
47   def uuid_changed?
48     self.api_token_changed?
49   end
50
51   def modified_by_client_uuid
52     nil
53   end
54   def modified_by_client_uuid=(x) end
55
56   def modified_by_user_uuid
57     nil
58   end
59   def modified_by_user_uuid=(x) end
60
61   def modified_at
62     nil
63   end
64   def modified_at=(x) end
65
66   protected
67
68   def permission_to_create
69     current_user.andand.is_admin or (current_user.andand.id == self.user_id)
70   end
71
72   def permission_to_update
73     (permission_to_create and
74      not self.user_id_changed? and
75      not self.owner_uuid_changed?)
76   end
77
78   def log_update
79     super unless (changed - UNLOGGED_ATTRIBUTES).empty?
80   end
81 end