Merge branch '8784-dir-listings'
[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
7 class Group < ArvadosModel
8   include HasUuid
9   include KindAndEtag
10   include CommonApiTemplate
11   include CanBeAnOwner
12   after_create :invalidate_permissions_cache
13   after_update :maybe_invalidate_permissions_cache
14   before_create :assign_name
15
16   api_accessible :user, extend: :common do |t|
17     t.add :name
18     t.add :group_class
19     t.add :description
20     t.add :writable_by
21   end
22
23   def maybe_invalidate_permissions_cache
24     if uuid_changed? or owner_uuid_changed?
25       # This can change users' permissions on other groups as well as
26       # this one.
27       invalidate_permissions_cache
28     end
29   end
30
31   def invalidate_permissions_cache
32     # Ensure a new group can be accessed by the appropriate users
33     # immediately after being created.
34     User.invalidate_permissions_cache db_current_time.to_i
35   end
36
37   def assign_name
38     if self.new_record? and (self.name.nil? or self.name.empty?)
39       self.name = self.uuid
40     end
41     true
42   end
43
44 end