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 # check if admins are allowed to make changes to the project, e.g. it
53 # isn't trashed or frozen.
54 def admin_change_permitted
55 !(FrozenGroup.where(uuid: self.uuid).any? || TrashedGroup.where(group_uuid: self.uuid).any?)
60 def self.attributes_required_columns
62 'can_write' => ['owner_uuid', 'uuid'],
63 'can_manage' => ['owner_uuid', 'uuid'],
64 'writable_by' => ['owner_uuid', 'uuid'],
68 def ensure_filesystem_compatible_name
69 # project and filter groups need filesystem-compatible names, but others
71 super if group_class == 'project' || group_class == 'filter'
75 if group_class != 'project' && group_class != 'role' && group_class != 'filter'
76 errors.add :group_class, "value must be one of 'project', 'role' or 'filter', was '#{group_class}'"
78 if group_class_changed? && !group_class_was.nil?
79 errors.add :group_class, "cannot be modified after record is created"
83 def check_filter_group_filters
84 if group_class == 'filter'
85 if !self.properties.key?("filters")
86 errors.add :properties, "filters property missing, it must be an array of arrays, each with 3 elements"
89 if !self.properties["filters"].is_a?(Array)
90 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
93 self.properties["filters"].each do |filter|
94 if !filter.is_a?(Array)
95 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
98 if filter.length() != 3
99 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
102 if !filter[0].include?(".") and filter[0].downcase != "uuid"
103 errors.add :properties, "filter attribute must be 'uuid' or contain a dot (e.g. groups.name)"
106 if (filter[0].downcase != "uuid" and filter[1].downcase == "is_a")
107 errors.add :properties, "when filter operator is 'is_a', attribute must be 'uuid'"
110 if ! ["=","<","<=",">",">=","!=","like","ilike","in","not in","is_a","exists","contains"].include?(filter[1].downcase)
111 errors.add :properties, "filter operator is not valid (must be =,<,<=,>,>=,!=,like,ilike,in,not in,is_a,exists,contains)"
118 def check_frozen_state_change_allowed
119 if frozen_by_uuid == ""
120 self.frozen_by_uuid = nil
122 if frozen_by_uuid_changed? || (new_record? && frozen_by_uuid)
123 if group_class != "project"
124 errors.add(:frozen_by_uuid, "cannot be modified on a non-project group")
127 if frozen_by_uuid_was && Rails.configuration.API.UnfreezeProjectRequiresAdmin && !current_user.is_admin
128 errors.add(:frozen_by_uuid, "can only be changed by an admin user, once set")
131 if frozen_by_uuid && frozen_by_uuid != current_user.uuid
132 errors.add(:frozen_by_uuid, "can only be set to the current user's UUID")
135 if !new_record? && !current_user.can?(manage: uuid)
136 raise PermissionDeniedError
138 if trash_at || delete_at || (!new_record? && TrashedGroup.where(group_uuid: uuid).any?)
139 errors.add(:frozen_by_uuid, "cannot be set on a trashed project")
141 if frozen_by_uuid_was.nil?
142 if Rails.configuration.API.FreezeProjectRequiresDescription && !attribute_present?(:description)
143 errors.add(:frozen_by_uuid, "can only be set if description is non-empty")
145 Rails.configuration.API.FreezeProjectRequiresProperties.andand.each do |key, _|
147 if !properties[key] || properties[key] == ""
148 errors.add(:frozen_by_uuid, "can only be set if properties[#{key}] value is non-empty")
156 return unless saved_change_to_trash_at? || saved_change_to_owner_uuid?
158 # The group was added or removed from the trash.
161 # Compute project subtree, propagating trash_at to subprojects
162 # Ensure none of the newly trashed descendants were frozen (if so, bail out)
163 # Remove groups that don't belong from trash
164 # Add/update groups that do belong in the trash
166 frozen_descendants = ActiveRecord::Base.connection.exec_query(%{
167 with temptable as (select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp))
168 select uuid from frozen_groups, temptable where uuid = target_uuid
170 "Group.update_trash.select",
172 TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at,
174 if frozen_descendants.any?
175 raise ArgumentError.new("cannot trash project containing frozen project #{frozen_descendants[0]["uuid"]}")
178 ActiveRecord::Base.connection.exec_query(%{
179 with temptable as (select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)),
181 delete_rows as (delete from trashed_groups where group_uuid in (select target_uuid from temptable where trash_at is NULL)),
183 insert_rows as (insert into trashed_groups (group_uuid, trash_at)
184 select target_uuid as group_uuid, trash_at from temptable where trash_at is not NULL
185 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at)
187 select container_uuid from container_requests where
188 owner_uuid in (select target_uuid from temptable) and
189 requesting_container_uuid is NULL and state = 'Committed' and container_uuid is not NULL
191 "Group.update_trash.select",
193 TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at,
194 self.trash_at]).each do |container_uuid|
195 update_priorities container_uuid["container_uuid"]
200 return unless saved_change_to_frozen_by_uuid? || saved_change_to_owner_uuid?
203 rows = ActiveRecord::Base.connection.exec_query(%{
204 with temptable as (select * from project_subtree_with_is_frozen($1,$2))
206 select cr.uuid, cr.state from container_requests cr, temptable frozen
207 where cr.owner_uuid = frozen.uuid and frozen.is_frozen
208 and cr.state not in ($3, $4) limit 1
210 "Group.update_frozen.check_container_requests",
212 !self.frozen_by_uuid.nil?,
213 ContainerRequest::Uncommitted,
214 ContainerRequest::Final])
216 raise ArgumentError.new("cannot freeze project containing container request #{rows.first['uuid']} with state = #{rows.first['state']}")
220 ActiveRecord::Base.connection.exec_query(%{
221 with temptable as (select * from project_subtree_with_is_frozen($1,$2)),
223 delete_rows as (delete from frozen_groups where uuid in (select uuid from temptable where not is_frozen))
225 insert into frozen_groups (uuid) select uuid from temptable where is_frozen on conflict do nothing
226 }, "Group.update_frozen.update",
228 !self.frozen_by_uuid.nil?])
232 def before_ownership_change
233 if owner_uuid_changed? and !self.owner_uuid_was.nil?
234 MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
235 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
239 def after_ownership_change
240 if saved_change_to_owner_uuid?
241 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
245 def clear_permissions_trash_frozen
246 MaterializedPermission.where(target_uuid: uuid).delete_all
247 ActiveRecord::Base.connection.exec_delete(
248 "delete from trashed_groups where group_uuid=$1",
249 "Group.clear_permissions_trash_frozen",
251 ActiveRecord::Base.connection.exec_delete(
252 "delete from frozen_groups where uuid=$1",
253 "Group.clear_permissions_trash_frozen",
258 if self.new_record? and (self.name.nil? or self.name.empty?)
259 self.name = self.uuid
264 def ensure_owner_uuid_is_permitted
265 if group_class == "role"
266 @requested_manager_uuid = nil
268 @requested_manager_uuid = owner_uuid
269 self.owner_uuid = system_user_uuid
272 if self.owner_uuid != system_user_uuid
273 raise "Owner uuid for role must be system user"
275 raise PermissionDeniedError.new("role group cannot be modified without can_manage permission") unless current_user.can?(manage: uuid)
282 def add_role_manage_link
283 if group_class == "role" && @requested_manager_uuid
284 act_as_system_user do
285 Link.create!(tail_uuid: @requested_manager_uuid,
286 head_uuid: self.uuid,
287 link_class: "permission",
293 def permission_to_create
296 elsif group_class == "role" &&
297 !Rails.configuration.Users.CanCreateRoleGroups &&
298 !current_user.andand.is_admin
299 raise PermissionDeniedError.new("this cluster does not allow users to create role groups")
305 def permission_to_update
308 elsif frozen_by_uuid && frozen_by_uuid_was
309 errors.add :uuid, "#{uuid} is frozen and cannot be modified"
316 def self.full_text_searchable_columns
317 super - ["frozen_by_uuid"]