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