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 before_create :assign_name
21 after_create :update_permissions
22 after_create :update_trash
24 after_update :update_permissions, :if => :owner_uuid_changed?
25 after_update :update_trash
27 after_destroy :clear_permissions_and_trash
30 api_accessible :user, extend: :common do |t|
41 def ensure_filesystem_compatible_name
42 # project groups need filesystem-compatible names, but others
44 super if group_class == 'project'
48 if trash_at_changed? or owner_uuid_changed?
49 # The group was added or removed from the trash.
52 # Compute project subtree, propagating trash_at to subprojects
53 # Remove groups that don't belong from trash
54 # Add/update groups that do belong in the trash
56 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
57 ActiveRecord::Base.connection.exec_query %{
58 create temporary table #{temptable} on commit drop
59 as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)
63 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
66 ActiveRecord::Base.connection.exec_query %{
67 delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL);
70 ActiveRecord::Base.connection.exec_query %{
71 insert into trashed_groups (group_uuid, trash_at)
72 select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL
73 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at;
78 def update_permissions
79 User.update_permissions self.owner_uuid, self.uuid, 3
82 def clear_permissions_and_trash
83 User.update_permissions self.owner_uuid, self.uuid, 0
84 ActiveRecord::Base.connection.exec_query %{
85 delete from trashed_groups where group_uuid=$1
86 }, "Group.clear_trash", [[nil, self.uuid]]
91 if self.new_record? and (self.name.nil? or self.name.empty?)