Merge remote-tracking branch 'origin/master' into 1971-show-image-thumbnails
[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   def assign_random_api_token
24     self.api_token ||= rand(2**256).to_s(36)
25   end
26
27   def owner_uuid
28     self.user.andand.uuid
29   end
30   def owner_uuid_was
31     self.user_id_changed? ? User.find(self.user_id_was).andand.uuid : self.user.andand.uuid
32   end
33   def owner_uuid_changed?
34     self.user_id_changed?
35   end
36
37   def uuid
38     self.api_token
39   end
40   def uuid=(x) end
41   def uuid_was
42     self.api_token_was
43   end
44   def uuid_changed?
45     self.api_token_changed?
46   end
47
48   def modified_by_client_uuid
49     nil
50   end
51   def modified_by_client_uuid=(x) end
52
53   def modified_by_user_uuid
54     nil
55   end
56   def modified_by_user_uuid=(x) end
57
58   def modified_at
59     nil
60   end
61   def modified_at=(x) end
62
63   protected
64
65   def permission_to_create
66     current_user.andand.is_admin or (current_user.andand.id == self.user_id)
67   end
68
69   def permission_to_update
70     (permission_to_create and
71      not self.user_id_changed? and
72      not self.owner_uuid_changed?)
73   end
74 end