14873: Fixes unit tests.
[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   # Posgresql JSONB columns should NOT be declared as serialized, Rails 5
16   # already know how to properly treat them.
17
18   after_create :invalidate_permissions_cache
19   after_update :maybe_invalidate_permissions_cache
20   before_create :assign_name
21
22   api_accessible :user, extend: :common do |t|
23     t.add :name
24     t.add :group_class
25     t.add :description
26     t.add :writable_by
27     t.add :delete_at
28     t.add :trash_at
29     t.add :is_trashed
30     t.add :properties
31   end
32
33   def maybe_invalidate_permissions_cache
34     if uuid_changed? or owner_uuid_changed? or is_trashed_changed?
35       # This can change users' permissions on other groups as well as
36       # this one.
37       invalidate_permissions_cache
38     end
39   end
40
41   def invalidate_permissions_cache
42     # Ensure a new group can be accessed by the appropriate users
43     # immediately after being created.
44     User.invalidate_permissions_cache self.async_permissions_update
45   end
46
47   def assign_name
48     if self.new_record? and (self.name.nil? or self.name.empty?)
49       self.name = self.uuid
50     end
51     true
52   end
53
54 end