Merge branch '12195-nodemanager-quota-error'
[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   after_create :invalidate_permissions_cache
16   after_update :maybe_invalidate_permissions_cache
17   before_create :assign_name
18
19   api_accessible :user, extend: :common do |t|
20     t.add :name
21     t.add :group_class
22     t.add :description
23     t.add :writable_by
24     t.add :delete_at
25     t.add :trash_at
26     t.add :is_trashed
27   end
28
29   def maybe_invalidate_permissions_cache
30     if uuid_changed? or owner_uuid_changed? or is_trashed_changed?
31       # This can change users' permissions on other groups as well as
32       # this one.
33       invalidate_permissions_cache
34     end
35   end
36
37   def invalidate_permissions_cache
38     # Ensure a new group can be accessed by the appropriate users
39     # immediately after being created.
40     User.invalidate_permissions_cache db_current_time.to_i
41   end
42
43   def assign_name
44     if self.new_record? and (self.name.nil? or self.name.empty?)
45       self.name = self.uuid
46     end
47     true
48   end
49
50 end