16535: Merge branch 'master'
[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_create :add_role_manage_link
29
30   after_update :update_trash
31   before_destroy :clear_permissions_and_trash
32
33   api_accessible :user, extend: :common do |t|
34     t.add :name
35     t.add :group_class
36     t.add :description
37     t.add :writable_by
38     t.add :delete_at
39     t.add :trash_at
40     t.add :is_trashed
41     t.add :properties
42   end
43
44   def ensure_filesystem_compatible_name
45     # project groups need filesystem-compatible names, but others
46     # don't.
47     super if group_class == 'project'
48   end
49
50   def check_group_class
51     if group_class != 'project' && group_class != 'role'
52       errors.add :group_class, "value must be one of 'project' or 'role', was '#{group_class}'"
53     end
54     if group_class_changed? && !group_class_was.nil?
55       errors.add :group_class, "cannot be modified after record is created"
56     end
57   end
58
59   def update_trash
60     if saved_change_to_trash_at? or saved_change_to_owner_uuid?
61       # The group was added or removed from the trash.
62       #
63       # Strategy:
64       #   Compute project subtree, propagating trash_at to subprojects
65       #   Remove groups that don't belong from trash
66       #   Add/update groups that do belong in the trash
67
68       temptable = "group_subtree_#{rand(2**64).to_s(10)}"
69       ActiveRecord::Base.connection.exec_query %{
70 create temporary table #{temptable} on commit drop
71 as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)
72 },
73                                                'Group.update_trash.select',
74                                                [[nil, self.uuid],
75                                                 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
76                                                 [nil, self.trash_at]]
77
78       ActiveRecord::Base.connection.exec_delete %{
79 delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL);
80 },
81                                             "Group.update_trash.delete"
82
83       ActiveRecord::Base.connection.exec_query %{
84 insert into trashed_groups (group_uuid, trash_at)
85   select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL
86 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at;
87 },
88                                             "Group.update_trash.insert"
89     end
90   end
91
92   def before_ownership_change
93     if owner_uuid_changed? and !self.owner_uuid_was.nil?
94       MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
95       update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
96     end
97   end
98
99   def after_ownership_change
100     if saved_change_to_owner_uuid?
101       update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
102     end
103   end
104
105   def clear_permissions_and_trash
106     MaterializedPermission.where(target_uuid: uuid).delete_all
107     ActiveRecord::Base.connection.exec_delete %{
108 delete from trashed_groups where group_uuid=$1
109 }, "Group.clear_permissions_and_trash", [[nil, self.uuid]]
110
111   end
112
113   def assign_name
114     if self.new_record? and (self.name.nil? or self.name.empty?)
115       self.name = self.uuid
116     end
117     true
118   end
119
120   def ensure_owner_uuid_is_permitted
121     if group_class == "role"
122       @requested_manager_uuid = nil
123       if new_record?
124         @requested_manager_uuid = owner_uuid
125         self.owner_uuid = system_user_uuid
126         return true
127       end
128       if self.owner_uuid != system_user_uuid
129         raise "Owner uuid for role must be system user"
130       end
131       raise PermissionDeniedError unless current_user.can?(manage: uuid)
132       true
133     else
134       super
135     end
136   end
137
138   def add_role_manage_link
139     if group_class == "role" && @requested_manager_uuid
140       act_as_system_user do
141        Link.create!(tail_uuid: @requested_manager_uuid,
142                     head_uuid: self.uuid,
143                     link_class: "permission",
144                     name: "can_manage")
145       end
146     end
147   end
148 end