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 before_create :assign_name
22 after_create :after_ownership_change
23 after_create :update_trash
25 before_update :before_ownership_change
26 after_update :after_ownership_change
28 after_update :update_trash
29 before_destroy :clear_permissions_and_trash
31 api_accessible :user, extend: :common do |t|
42 def ensure_filesystem_compatible_name
43 # project groups need filesystem-compatible names, but others
45 super if group_class == 'project'
49 if group_class != 'project' && group_class != 'role'
50 errors.add :group_class, "value must be one of 'project' or 'role', was '#{group_class}'"
55 if trash_at_changed? or owner_uuid_changed?
56 # The group was added or removed from the trash.
59 # Compute project subtree, propagating trash_at to subprojects
60 # Remove groups that don't belong from trash
61 # Add/update groups that do belong in the trash
63 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
64 ActiveRecord::Base.connection.exec_query %{
65 create temporary table #{temptable} on commit drop
66 as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)
68 'Group.update_trash.select',
70 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
73 ActiveRecord::Base.connection.exec_delete %{
74 delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL);
76 "Group.update_trash.delete"
78 ActiveRecord::Base.connection.exec_query %{
79 insert into trashed_groups (group_uuid, trash_at)
80 select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL
81 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at;
83 "Group.update_trash.insert"
87 def before_ownership_change
88 if owner_uuid_changed? and !self.owner_uuid_was.nil?
89 MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
90 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
94 def after_ownership_change
95 if owner_uuid_changed?
96 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
100 def clear_permissions_and_trash
101 MaterializedPermission.where(target_uuid: uuid).delete_all
102 ActiveRecord::Base.connection.exec_delete %{
103 delete from trashed_groups where group_uuid=$1
104 }, "Group.clear_permissions_and_trash", [[nil, self.uuid]]
109 if self.new_record? and (self.name.nil? or self.name.empty?)
110 self.name = self.uuid