1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require 'can_be_an_owner'
8 class Group < ArvadosModel
11 include CommonApiTemplate
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: {}
19 validate :ensure_filesystem_compatible_name
20 validate :check_group_class
21 validate :check_filter_group_filters
22 validate :check_frozen_state_change_allowed
23 before_create :assign_name
24 after_create :after_ownership_change
25 after_create :update_trash
26 after_create :update_frozen
28 before_update :before_ownership_change
29 after_update :after_ownership_change
31 after_create :add_role_manage_link
33 after_update :update_trash
34 after_update :update_frozen
35 before_destroy :clear_permissions_trash_frozen
37 api_accessible :user, extend: :common do |t|
49 def ensure_filesystem_compatible_name
50 # project and filter groups need filesystem-compatible names, but others
52 super if group_class == 'project' || group_class == 'filter'
56 if group_class != 'project' && group_class != 'role' && group_class != 'filter'
57 errors.add :group_class, "value must be one of 'project', 'role' or 'filter', was '#{group_class}'"
59 if group_class_changed? && !group_class_was.nil?
60 errors.add :group_class, "cannot be modified after record is created"
64 def check_filter_group_filters
65 if group_class == 'filter'
66 if !self.properties.key?("filters")
67 errors.add :properties, "filters property missing, it must be an array of arrays, each with 3 elements"
70 if !self.properties["filters"].is_a?(Array)
71 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
74 self.properties["filters"].each do |filter|
75 if !filter.is_a?(Array)
76 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
79 if filter.length() != 3
80 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
83 if !filter[0].include?(".") and filter[0].downcase != "uuid"
84 errors.add :properties, "filter attribute must be 'uuid' or contain a dot (e.g. groups.name)"
87 if (filter[0].downcase != "uuid" and filter[1].downcase == "is_a")
88 errors.add :properties, "when filter operator is 'is_a', attribute must be 'uuid'"
91 if ! ["=","<","<=",">",">=","!=","like","ilike","in","not in","is_a","exists","contains"].include?(filter[1].downcase)
92 errors.add :properties, "filter operator is not valid (must be =,<,<=,>,>=,!=,like,ilike,in,not in,is_a,exists,contains)"
99 def check_frozen_state_change_allowed
100 if frozen_by_uuid == ""
101 self.frozen_by_uuid = nil
103 if frozen_by_uuid_changed? || (new_record? && frozen_by_uuid)
104 if group_class != "project"
105 errors.add(:frozen_by_uuid, "cannot be modified on a non-project group")
108 if frozen_by_uuid_was && Rails.configuration.API.UnfreezeProjectRequiresAdmin && !current_user.is_admin
109 errors.add(:frozen_by_uuid, "can only be changed by an admin user, once set")
112 if frozen_by_uuid && frozen_by_uuid != current_user.uuid
113 errors.add(:frozen_by_uuid, "can only be set to the current user's UUID")
116 if !new_record? && !current_user.can?(manage: uuid)
117 raise PermissionDeniedError
119 if trash_at || delete_at || (!new_record? && TrashedGroup.where(group_uuid: uuid).any?)
120 errors.add(:frozen_by_uuid, "cannot be set on a trashed project")
122 if frozen_by_uuid_was.nil?
123 if Rails.configuration.API.FreezeProjectRequiresDescription && !attribute_present?(:description)
124 errors.add(:frozen_by_uuid, "can only be set if description is non-empty")
126 Rails.configuration.API.FreezeProjectRequiresProperties.andand.each do |key, _|
128 if !properties[key] || properties[key] == ""
129 errors.add(:frozen_by_uuid, "can only be set if properties[#{key}] value is non-empty")
137 return unless saved_change_to_trash_at? || saved_change_to_owner_uuid?
139 # The group was added or removed from the trash.
142 # Compute project subtree, propagating trash_at to subprojects
143 # Ensure none of the newly trashed descendants were frozen (if so, bail out)
144 # Remove groups that don't belong from trash
145 # Add/update groups that do belong in the trash
147 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
148 ActiveRecord::Base.connection.exec_query(
149 "create temporary table #{temptable} on commit drop " +
150 "as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)",
151 "Group.update_trash.select",
153 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
154 [nil, self.trash_at]])
155 frozen_descendants = ActiveRecord::Base.connection.exec_query(
156 "select uuid from frozen_groups, #{temptable} where uuid = target_uuid",
157 "Group.update_trash.check_frozen")
158 if frozen_descendants.any?
159 raise ArgumentError.new("cannot trash project containing frozen project #{frozen_descendants[0]["uuid"]}")
161 ActiveRecord::Base.connection.exec_delete(
162 "delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL)",
163 "Group.update_trash.delete")
164 ActiveRecord::Base.connection.exec_query(
165 "insert into trashed_groups (group_uuid, trash_at) "+
166 "select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL " +
167 "on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at",
168 "Group.update_trash.insert")
172 return unless saved_change_to_frozen_by_uuid? || saved_change_to_owner_uuid?
173 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
174 ActiveRecord::Base.connection.exec_query(
175 "create temporary table #{temptable} on commit drop as select * from project_subtree_with_is_frozen($1,$2)",
176 "Group.update_frozen.select",
178 [nil, !self.frozen_by_uuid.nil?]])
180 rows = ActiveRecord::Base.connection.exec_query(
181 "select cr.uuid, cr.state from container_requests cr, #{temptable} frozen " +
182 "where cr.owner_uuid = frozen.uuid and frozen.is_frozen " +
183 "and cr.state not in ($1, $2) limit 1",
184 "Group.update_frozen.check_container_requests",
185 [[nil, ContainerRequest::Uncommitted],
186 [nil, ContainerRequest::Final]])
188 raise ArgumentError.new("cannot freeze project containing container request #{rows.first['uuid']} with state = #{rows.first['state']}")
191 ActiveRecord::Base.connection.exec_delete(
192 "delete from frozen_groups where uuid in (select uuid from #{temptable} where not is_frozen)",
193 "Group.update_frozen.delete")
194 ActiveRecord::Base.connection.exec_query(
195 "insert into frozen_groups (uuid) select uuid from #{temptable} where is_frozen on conflict do nothing",
196 "Group.update_frozen.insert")
199 def before_ownership_change
200 if owner_uuid_changed? and !self.owner_uuid_was.nil?
201 MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
202 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
206 def after_ownership_change
207 if saved_change_to_owner_uuid?
208 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
212 def clear_permissions_trash_frozen
213 MaterializedPermission.where(target_uuid: uuid).delete_all
214 ActiveRecord::Base.connection.exec_delete(
215 "delete from trashed_groups where group_uuid=$1",
216 "Group.clear_permissions_trash_frozen",
218 ActiveRecord::Base.connection.exec_delete(
219 "delete from frozen_groups where uuid=$1",
220 "Group.clear_permissions_trash_frozen",
225 if self.new_record? and (self.name.nil? or self.name.empty?)
226 self.name = self.uuid
231 def ensure_owner_uuid_is_permitted
232 if group_class == "role"
233 @requested_manager_uuid = nil
235 @requested_manager_uuid = owner_uuid
236 self.owner_uuid = system_user_uuid
239 if self.owner_uuid != system_user_uuid
240 raise "Owner uuid for role must be system user"
242 raise PermissionDeniedError unless current_user.can?(manage: uuid)
249 def add_role_manage_link
250 if group_class == "role" && @requested_manager_uuid
251 act_as_system_user do
252 Link.create!(tail_uuid: @requested_manager_uuid,
253 head_uuid: self.uuid,
254 link_class: "permission",
260 def permission_to_update
263 elsif frozen_by_uuid && frozen_by_uuid_was
264 errors.add :uuid, "#{uuid} is frozen and cannot be modified"
271 def self.full_text_searchable_columns
272 super - ["frozen_by_uuid"]