Merge branch '14670-new-java-sdk-docs' of git.curoverse.com:arvados into 14670-new...
[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   attribute :properties, :jsonbHash, default: {}
18
19   after_create :invalidate_permissions_cache
20   after_update :maybe_invalidate_permissions_cache
21   before_create :assign_name
22
23   api_accessible :user, extend: :common do |t|
24     t.add :name
25     t.add :group_class
26     t.add :description
27     t.add :writable_by
28     t.add :delete_at
29     t.add :trash_at
30     t.add :is_trashed
31     t.add :properties
32   end
33
34   def maybe_invalidate_permissions_cache
35     if uuid_changed? or owner_uuid_changed? or is_trashed_changed?
36       # This can change users' permissions on other groups as well as
37       # this one.
38       invalidate_permissions_cache
39     end
40   end
41
42   def invalidate_permissions_cache
43     # Ensure a new group can be accessed by the appropriate users
44     # immediately after being created.
45     User.invalidate_permissions_cache self.async_permissions_update
46   end
47
48   def assign_name
49     if self.new_record? and (self.name.nil? or self.name.empty?)
50       self.name = self.uuid
51     end
52     true
53   end
54 end