16526: Merge branch 'master' into 16526-ruby-and-python-build-script-updates
[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   validate :ensure_filesystem_compatible_name
20   before_create :assign_name
21   after_create :after_ownership_change
22   after_create :update_trash
23
24   before_update :before_ownership_change
25   after_update :after_ownership_change
26
27   after_update :update_trash
28   before_destroy :clear_permissions_and_trash
29
30   api_accessible :user, extend: :common do |t|
31     t.add :name
32     t.add :group_class
33     t.add :description
34     t.add :writable_by
35     t.add :delete_at
36     t.add :trash_at
37     t.add :is_trashed
38     t.add :properties
39   end
40
41   def ensure_filesystem_compatible_name
42     # project groups need filesystem-compatible names, but others
43     # don't.
44     super if group_class == 'project'
45   end
46
47   def update_trash
48     if trash_at_changed? or owner_uuid_changed?
49       # The group was added or removed from the trash.
50       #
51       # Strategy:
52       #   Compute project subtree, propagating trash_at to subprojects
53       #   Remove groups that don't belong from trash
54       #   Add/update groups that do belong in the trash
55
56       temptable = "group_subtree_#{rand(2**64).to_s(10)}"
57       ActiveRecord::Base.connection.exec_query %{
58 create temporary table #{temptable} on commit drop
59 as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)
60 },
61                                                'Group.update_trash.select',
62                                                [[nil, self.uuid],
63                                                 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
64                                                 [nil, self.trash_at]]
65
66       ActiveRecord::Base.connection.exec_delete %{
67 delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL);
68 },
69                                             "Group.update_trash.delete"
70
71       ActiveRecord::Base.connection.exec_query %{
72 insert into trashed_groups (group_uuid, trash_at)
73   select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL
74 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at;
75 },
76                                             "Group.update_trash.insert"
77     end
78   end
79
80   def before_ownership_change
81     if owner_uuid_changed? and !self.owner_uuid_was.nil?
82       MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
83       update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
84     end
85   end
86
87   def after_ownership_change
88     if owner_uuid_changed?
89       update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
90     end
91   end
92
93   def clear_permissions_and_trash
94     MaterializedPermission.where(target_uuid: uuid).delete_all
95     ActiveRecord::Base.connection.exec_delete %{
96 delete from trashed_groups where group_uuid=$1
97 }, "Group.clear_permissions_and_trash", [[nil, self.uuid]]
98
99   end
100
101   def assign_name
102     if self.new_record? and (self.name.nil? or self.name.empty?)
103       self.name = self.uuid
104     end
105     true
106   end
107 end