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|
53 def self.attributes_required_columns
55 'can_write' => ['owner_uuid', 'uuid'],
56 'can_manage' => ['owner_uuid', 'uuid'],
60 def ensure_filesystem_compatible_name
61 # project and filter groups need filesystem-compatible names, but others
63 super if group_class == 'project' || group_class == 'filter'
67 if group_class != 'project' && group_class != 'role' && group_class != 'filter'
68 errors.add :group_class, "value must be one of 'project', 'role' or 'filter', was '#{group_class}'"
70 if group_class_changed? && !group_class_was.nil?
71 errors.add :group_class, "cannot be modified after record is created"
75 def check_filter_group_filters
76 if group_class == 'filter'
77 if !self.properties.key?("filters")
78 errors.add :properties, "filters property missing, it must be an array of arrays, each with 3 elements"
81 if !self.properties["filters"].is_a?(Array)
82 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
85 self.properties["filters"].each do |filter|
86 if !filter.is_a?(Array)
87 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
90 if filter.length() != 3
91 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
94 if !filter[0].include?(".") and filter[0].downcase != "uuid"
95 errors.add :properties, "filter attribute must be 'uuid' or contain a dot (e.g. groups.name)"
98 if (filter[0].downcase != "uuid" and filter[1].downcase == "is_a")
99 errors.add :properties, "when filter operator is 'is_a', attribute must be 'uuid'"
102 if ! ["=","<","<=",">",">=","!=","like","ilike","in","not in","is_a","exists","contains"].include?(filter[1].downcase)
103 errors.add :properties, "filter operator is not valid (must be =,<,<=,>,>=,!=,like,ilike,in,not in,is_a,exists,contains)"
110 def check_frozen_state_change_allowed
111 if frozen_by_uuid == ""
112 self.frozen_by_uuid = nil
114 if frozen_by_uuid_changed? || (new_record? && frozen_by_uuid)
115 if group_class != "project"
116 errors.add(:frozen_by_uuid, "cannot be modified on a non-project group")
119 if frozen_by_uuid_was && Rails.configuration.API.UnfreezeProjectRequiresAdmin && !current_user.is_admin
120 errors.add(:frozen_by_uuid, "can only be changed by an admin user, once set")
123 if frozen_by_uuid && frozen_by_uuid != current_user.uuid
124 errors.add(:frozen_by_uuid, "can only be set to the current user's UUID")
127 if !new_record? && !current_user.can?(manage: uuid)
128 raise PermissionDeniedError
130 if trash_at || delete_at || (!new_record? && TrashedGroup.where(group_uuid: uuid).any?)
131 errors.add(:frozen_by_uuid, "cannot be set on a trashed project")
133 if frozen_by_uuid_was.nil?
134 if Rails.configuration.API.FreezeProjectRequiresDescription && !attribute_present?(:description)
135 errors.add(:frozen_by_uuid, "can only be set if description is non-empty")
137 Rails.configuration.API.FreezeProjectRequiresProperties.andand.each do |key, _|
139 if !properties[key] || properties[key] == ""
140 errors.add(:frozen_by_uuid, "can only be set if properties[#{key}] value is non-empty")
148 return unless saved_change_to_trash_at? || saved_change_to_owner_uuid?
150 # The group was added or removed from the trash.
153 # Compute project subtree, propagating trash_at to subprojects
154 # Ensure none of the newly trashed descendants were frozen (if so, bail out)
155 # Remove groups that don't belong from trash
156 # Add/update groups that do belong in the trash
158 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
159 ActiveRecord::Base.connection.exec_query(
160 "create temporary table #{temptable} on commit drop " +
161 "as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)",
162 "Group.update_trash.select",
164 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
165 [nil, self.trash_at]])
166 frozen_descendants = ActiveRecord::Base.connection.exec_query(
167 "select uuid from frozen_groups, #{temptable} where uuid = target_uuid",
168 "Group.update_trash.check_frozen")
169 if frozen_descendants.any?
170 raise ArgumentError.new("cannot trash project containing frozen project #{frozen_descendants[0]["uuid"]}")
172 ActiveRecord::Base.connection.exec_delete(
173 "delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL)",
174 "Group.update_trash.delete")
175 ActiveRecord::Base.connection.exec_query(
176 "insert into trashed_groups (group_uuid, trash_at) "+
177 "select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL " +
178 "on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at",
179 "Group.update_trash.insert")
183 return unless saved_change_to_frozen_by_uuid? || saved_change_to_owner_uuid?
184 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
185 ActiveRecord::Base.connection.exec_query(
186 "create temporary table #{temptable} on commit drop as select * from project_subtree_with_is_frozen($1,$2)",
187 "Group.update_frozen.select",
189 [nil, !self.frozen_by_uuid.nil?]])
191 rows = ActiveRecord::Base.connection.exec_query(
192 "select cr.uuid, cr.state from container_requests cr, #{temptable} frozen " +
193 "where cr.owner_uuid = frozen.uuid and frozen.is_frozen " +
194 "and cr.state not in ($1, $2) limit 1",
195 "Group.update_frozen.check_container_requests",
196 [[nil, ContainerRequest::Uncommitted],
197 [nil, ContainerRequest::Final]])
199 raise ArgumentError.new("cannot freeze project containing container request #{rows.first['uuid']} with state = #{rows.first['state']}")
202 ActiveRecord::Base.connection.exec_delete(
203 "delete from frozen_groups where uuid in (select uuid from #{temptable} where not is_frozen)",
204 "Group.update_frozen.delete")
205 ActiveRecord::Base.connection.exec_query(
206 "insert into frozen_groups (uuid) select uuid from #{temptable} where is_frozen on conflict do nothing",
207 "Group.update_frozen.insert")
210 def before_ownership_change
211 if owner_uuid_changed? and !self.owner_uuid_was.nil?
212 MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
213 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
217 def after_ownership_change
218 if saved_change_to_owner_uuid?
219 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
223 def clear_permissions_trash_frozen
224 MaterializedPermission.where(target_uuid: uuid).delete_all
225 ActiveRecord::Base.connection.exec_delete(
226 "delete from trashed_groups where group_uuid=$1",
227 "Group.clear_permissions_trash_frozen",
229 ActiveRecord::Base.connection.exec_delete(
230 "delete from frozen_groups where uuid=$1",
231 "Group.clear_permissions_trash_frozen",
236 if self.new_record? and (self.name.nil? or self.name.empty?)
237 self.name = self.uuid
242 def ensure_owner_uuid_is_permitted
243 if group_class == "role"
244 @requested_manager_uuid = nil
246 @requested_manager_uuid = owner_uuid
247 self.owner_uuid = system_user_uuid
250 if self.owner_uuid != system_user_uuid
251 raise "Owner uuid for role must be system user"
253 raise PermissionDeniedError.new("role group cannot be modified without can_manage permission") unless current_user.can?(manage: uuid)
260 def add_role_manage_link
261 if group_class == "role" && @requested_manager_uuid
262 act_as_system_user do
263 Link.create!(tail_uuid: @requested_manager_uuid,
264 head_uuid: self.uuid,
265 link_class: "permission",
271 def permission_to_create
274 elsif group_class == "role" &&
275 !Rails.configuration.Users.CanCreateRoleGroups &&
276 !current_user.andand.is_admin
277 raise PermissionDeniedError.new("this cluster does not allow users to create role groups")
283 def permission_to_update
286 elsif frozen_by_uuid && frozen_by_uuid_was
287 errors.add :uuid, "#{uuid} is frozen and cannot be modified"
294 def self.full_text_searchable_columns
295 super - ["frozen_by_uuid"]