Merge remote-tracking branch 'origin/master' into 14645-fuse-operations-reporting
[arvados.git] / services / api / app / models / group.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'can_be_an_owner'
6 require 'trashable'
7
8 class Group < ArvadosModel
9   include HasUuid
10   include KindAndEtag
11   include CommonApiTemplate
12   include CanBeAnOwner
13   include Trashable
14
15   serialize :properties, Hash
16
17   after_create :invalidate_permissions_cache
18   after_update :maybe_invalidate_permissions_cache
19   before_create :assign_name
20
21   api_accessible :user, extend: :common do |t|
22     t.add :name
23     t.add :group_class
24     t.add :description
25     t.add :writable_by
26     t.add :delete_at
27     t.add :trash_at
28     t.add :is_trashed
29     t.add :properties
30   end
31
32   def maybe_invalidate_permissions_cache
33     if uuid_changed? or owner_uuid_changed? or is_trashed_changed?
34       # This can change users' permissions on other groups as well as
35       # this one.
36       invalidate_permissions_cache
37     end
38   end
39
40   def invalidate_permissions_cache
41     # Ensure a new group can be accessed by the appropriate users
42     # immediately after being created.
43     User.invalidate_permissions_cache self.async_permissions_update
44   end
45
46   def assign_name
47     if self.new_record? and (self.name.nil? or self.name.empty?)
48       self.name = self.uuid
49     end
50     true
51   end
52
53 end