1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require 'can_be_an_owner'
7 require 'update_priorities'
9 class Group < ArvadosModel
12 include CommonApiTemplate
16 # Posgresql JSONB columns should NOT be declared as serialized, Rails 5
17 # already know how to properly treat them.
18 attribute :properties, :jsonbHash, default: {}
20 validate :ensure_filesystem_compatible_name
21 validate :check_group_class
22 validate :check_filter_group_filters
23 validate :check_frozen_state_change_allowed
24 before_create :assign_name
25 after_create :after_ownership_change
26 after_create :update_trash
27 after_create :update_frozen
29 before_update :before_ownership_change
30 after_update :after_ownership_change
32 after_create :add_role_manage_link
34 after_update :update_trash
35 after_update :update_frozen
36 before_destroy :clear_permissions_trash_frozen
38 api_accessible :user, extend: :common do |t|
52 def default_delete_after_trash_interval
53 if self.group_class == 'role'
54 ActiveSupport::Duration.build(0)
60 def minimum_delete_after_trash_interval
61 if self.group_class == 'role'
62 ActiveSupport::Duration.build(0)
68 def validate_trash_and_delete_timing
69 if self.group_class == 'role' && delete_at && delete_at != trash_at
70 errors.add :delete_at, "must be == trash_at for role groups"
76 # check if admins are allowed to make changes to the project, e.g. it
77 # isn't trashed or frozen.
78 def admin_change_permitted
79 !(FrozenGroup.where(uuid: self.uuid).any? || TrashedGroup.where(group_uuid: self.uuid).any?)
84 def self.attributes_required_columns
86 'can_write' => ['owner_uuid', 'uuid'],
87 'can_manage' => ['owner_uuid', 'uuid'],
88 'writable_by' => ['owner_uuid', 'uuid'],
92 def ensure_filesystem_compatible_name
93 # project and filter groups need filesystem-compatible names, but others
95 super if group_class == 'project' || group_class == 'filter'
99 if group_class != 'project' && group_class != 'role' && group_class != 'filter'
100 errors.add :group_class, "value must be one of 'project', 'role' or 'filter', was '#{group_class}'"
102 if group_class_changed? && !group_class_was.nil?
103 errors.add :group_class, "cannot be modified after record is created"
107 def check_filter_group_filters
108 if group_class == 'filter'
109 if !self.properties.key?("filters")
110 errors.add :properties, "filters property missing, it must be an array of arrays, each with 3 elements"
113 if !self.properties["filters"].is_a?(Array)
114 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
117 self.properties["filters"].each do |filter|
118 if !filter.is_a?(Array)
119 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
122 if filter.length() != 3
123 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
126 if !filter[0].include?(".") and filter[0].downcase != "uuid"
127 errors.add :properties, "filter attribute must be 'uuid' or contain a dot (e.g. groups.name)"
130 if (filter[0].downcase != "uuid" and filter[1].downcase == "is_a")
131 errors.add :properties, "when filter operator is 'is_a', attribute must be 'uuid'"
134 if ! ["=","<","<=",">",">=","!=","like","ilike","in","not in","is_a","exists","contains"].include?(filter[1].downcase)
135 errors.add :properties, "filter operator is not valid (must be =,<,<=,>,>=,!=,like,ilike,in,not in,is_a,exists,contains)"
142 def check_frozen_state_change_allowed
143 if frozen_by_uuid == ""
144 self.frozen_by_uuid = nil
146 if frozen_by_uuid_changed? || (new_record? && frozen_by_uuid)
147 if group_class != "project"
148 errors.add(:frozen_by_uuid, "cannot be modified on a non-project group")
151 if frozen_by_uuid_was && Rails.configuration.API.UnfreezeProjectRequiresAdmin && !current_user.is_admin
152 errors.add(:frozen_by_uuid, "can only be changed by an admin user, once set")
155 if frozen_by_uuid && frozen_by_uuid != current_user.uuid
156 errors.add(:frozen_by_uuid, "can only be set to the current user's UUID")
159 if !new_record? && !current_user.can?(manage: uuid)
160 raise PermissionDeniedError
162 if trash_at || delete_at || (!new_record? && TrashedGroup.where(group_uuid: uuid).any?)
163 errors.add(:frozen_by_uuid, "cannot be set on a trashed project")
165 if frozen_by_uuid_was.nil?
166 if Rails.configuration.API.FreezeProjectRequiresDescription && !attribute_present?(:description)
167 errors.add(:frozen_by_uuid, "can only be set if description is non-empty")
169 Rails.configuration.API.FreezeProjectRequiresProperties.andand.each do |key, _|
171 if !properties[key] || properties[key] == ""
172 errors.add(:frozen_by_uuid, "can only be set if properties[#{key}] value is non-empty")
180 return unless saved_change_to_trash_at? || saved_change_to_owner_uuid?
182 # The group was added or removed from the trash.
185 # Compute project subtree, propagating trash_at to subprojects
186 # Ensure none of the newly trashed descendants were frozen (if so, bail out)
187 # Remove groups that don't belong from trash
188 # Add/update groups that do belong in the trash
190 frozen_descendants = ActiveRecord::Base.connection.exec_query(%{
191 with temptable as (select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp))
192 select uuid from frozen_groups, temptable where uuid = target_uuid
194 "Group.update_trash.select",
196 TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at,
199 if frozen_descendants.any?
200 raise ArgumentError.new("cannot trash project containing frozen project #{frozen_descendants[0]["uuid"]}")
203 if self.trash_at and self.group_class == 'role'
204 # if this is a role group that is now in the trash, it loses all
205 # of its outgoing permissions.
206 Link.where(link_class: 'permission', tail_uuid: self.uuid).destroy_all
209 ActiveRecord::Base.connection.exec_query(%{
210 with temptable as (select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)),
212 delete_rows as (delete from trashed_groups where group_uuid in (select target_uuid from temptable where trash_at is NULL)),
214 insert_rows as (insert into trashed_groups (group_uuid, trash_at)
215 select target_uuid as group_uuid, trash_at from temptable where trash_at is not NULL
216 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at)
218 select container_uuid from container_requests where
219 owner_uuid in (select target_uuid from temptable) and
220 requesting_container_uuid is NULL and state = 'Committed' and container_uuid is not NULL
222 "Group.update_trash.select",
224 TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at,
225 self.trash_at]).each do |container_uuid|
226 update_priorities container_uuid["container_uuid"]
231 return unless saved_change_to_frozen_by_uuid? || saved_change_to_owner_uuid?
234 rows = ActiveRecord::Base.connection.exec_query(%{
235 with temptable as (select * from project_subtree_with_is_frozen($1,$2))
237 select cr.uuid, cr.state from container_requests cr, temptable frozen
238 where cr.owner_uuid = frozen.uuid and frozen.is_frozen
239 and cr.state not in ($3, $4) limit 1
241 "Group.update_frozen.check_container_requests",
243 !self.frozen_by_uuid.nil?,
244 ContainerRequest::Uncommitted,
245 ContainerRequest::Final])
247 raise ArgumentError.new("cannot freeze project containing container request #{rows.first['uuid']} with state = #{rows.first['state']}")
251 ActiveRecord::Base.connection.exec_query(%{
252 with temptable as (select * from project_subtree_with_is_frozen($1,$2)),
254 delete_rows as (delete from frozen_groups where uuid in (select uuid from temptable where not is_frozen))
256 insert into frozen_groups (uuid) select uuid from temptable where is_frozen on conflict do nothing
257 }, "Group.update_frozen.update",
259 !self.frozen_by_uuid.nil?])
263 def before_ownership_change
264 if owner_uuid_changed? and !self.owner_uuid_was.nil?
265 ComputedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
266 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
270 def after_ownership_change
271 if saved_change_to_owner_uuid?
272 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
276 def clear_permissions_trash_frozen
277 Link.where(link_class: 'permission', tail_uuid: self.uuid).destroy_all
278 ComputedPermission.where(target_uuid: uuid).delete_all
279 ActiveRecord::Base.connection.exec_delete(
280 "delete from trashed_groups where group_uuid=$1",
281 "Group.clear_permissions_trash_frozen",
283 ActiveRecord::Base.connection.exec_delete(
284 "delete from frozen_groups where uuid=$1",
285 "Group.clear_permissions_trash_frozen",
290 if self.new_record? and (self.name.nil? or self.name.empty?)
291 self.name = self.uuid
296 def ensure_owner_uuid_is_permitted
297 if group_class == "role"
298 @requested_manager_uuid = nil
300 @requested_manager_uuid = owner_uuid
301 self.owner_uuid = system_user_uuid
304 if self.owner_uuid != system_user_uuid
305 raise "Owner uuid for role must be system user"
307 raise PermissionDeniedError.new("role group cannot be modified without can_manage permission") unless current_user.can?(manage: uuid)
314 def add_role_manage_link
315 if group_class == "role" && @requested_manager_uuid
316 act_as_system_user do
317 Link.create!(tail_uuid: @requested_manager_uuid,
318 head_uuid: self.uuid,
319 link_class: "permission",
325 def permission_to_create
328 elsif group_class == "role" &&
329 !Rails.configuration.Users.CanCreateRoleGroups &&
330 !current_user.andand.is_admin
331 raise PermissionDeniedError.new("this cluster does not allow users to create role groups")
337 def permission_to_update
340 elsif frozen_by_uuid && frozen_by_uuid_was
341 errors.add :uuid, "#{uuid} is frozen and cannot be modified"
348 def self.full_text_searchable_columns
349 super - ["frozen_by_uuid"]