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 before_create :assign_name
23 after_create :after_ownership_change
24 after_create :update_trash
26 before_update :before_ownership_change
27 after_update :after_ownership_change
29 after_create :add_role_manage_link
31 after_update :update_trash
32 before_destroy :clear_permissions_and_trash
34 api_accessible :user, extend: :common do |t|
45 def ensure_filesystem_compatible_name
46 # project and filter groups need filesystem-compatible names, but others
48 super if group_class == 'project' || group_class == 'filter'
52 if group_class != 'project' && group_class != 'role' && group_class != 'filter'
53 errors.add :group_class, "value must be one of 'project', 'role' or 'filter', was '#{group_class}'"
55 if group_class_changed? && !group_class_was.nil?
56 errors.add :group_class, "cannot be modified after record is created"
60 def check_filter_group_filters
61 if group_class == 'filter'
62 if !self.properties.key?("filters")
63 errors.add :properties, "filters property missing, it must be an array of arrays, each with 3 elements"
66 if !self.properties["filters"].is_a?(Array)
67 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
70 self.properties["filters"].each do |filter|
71 if !filter.is_a?(Array)
72 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
75 if filter.length() != 3
76 errors.add :properties, "filters property must be an array of arrays, each with 3 elements"
79 if !filter[0].include?(".") and filter[0].downcase != "uuid"
80 errors.add :properties, "filter attribute must be 'uuid' or contain a dot (e.g. groups.name)"
83 if (filter[0].downcase != "uuid" and filter[1].downcase == "is_a")
84 errors.add :properties, "when filter operator is 'is_a', attribute must be 'uuid'"
87 if ! ["=","<","<=",">",">=","!=","like","ilike","in","not in","is_a","exists","contains"].include?(filter[1].downcase)
88 errors.add :properties, "filter operator is not valid (must be =,<,<=,>,>=,!=,like,ilike,in,not in,is_a,exists,contains)"
96 if saved_change_to_trash_at? or saved_change_to_owner_uuid?
97 # The group was added or removed from the trash.
100 # Compute project subtree, propagating trash_at to subprojects
101 # Remove groups that don't belong from trash
102 # Add/update groups that do belong in the trash
104 temptable = "group_subtree_#{rand(2**64).to_s(10)}"
105 ActiveRecord::Base.connection.exec_query %{
106 create temporary table #{temptable} on commit drop
107 as select * from project_subtree_with_trash_at($1, LEAST($2, $3)::timestamp)
109 'Group.update_trash.select',
111 [nil, TrashedGroup.find_by_group_uuid(self.owner_uuid).andand.trash_at],
112 [nil, self.trash_at]]
114 ActiveRecord::Base.connection.exec_delete %{
115 delete from trashed_groups where group_uuid in (select target_uuid from #{temptable} where trash_at is NULL);
117 "Group.update_trash.delete"
119 ActiveRecord::Base.connection.exec_query %{
120 insert into trashed_groups (group_uuid, trash_at)
121 select target_uuid as group_uuid, trash_at from #{temptable} where trash_at is not NULL
122 on conflict (group_uuid) do update set trash_at=EXCLUDED.trash_at;
124 "Group.update_trash.insert"
128 def before_ownership_change
129 if owner_uuid_changed? and !self.owner_uuid_was.nil?
130 MaterializedPermission.where(user_uuid: owner_uuid_was, target_uuid: uuid).delete_all
131 update_permissions self.owner_uuid_was, self.uuid, REVOKE_PERM
135 def after_ownership_change
136 if saved_change_to_owner_uuid?
137 update_permissions self.owner_uuid, self.uuid, CAN_MANAGE_PERM
141 def clear_permissions_and_trash
142 MaterializedPermission.where(target_uuid: uuid).delete_all
143 ActiveRecord::Base.connection.exec_delete %{
144 delete from trashed_groups where group_uuid=$1
145 }, "Group.clear_permissions_and_trash", [[nil, self.uuid]]
150 if self.new_record? and (self.name.nil? or self.name.empty?)
151 self.name = self.uuid
156 def ensure_owner_uuid_is_permitted
157 if group_class == "role"
158 @requested_manager_uuid = nil
160 @requested_manager_uuid = owner_uuid
161 self.owner_uuid = system_user_uuid
164 if self.owner_uuid != system_user_uuid
165 raise "Owner uuid for role must be system user"
167 raise PermissionDeniedError unless current_user.can?(manage: uuid)
174 def add_role_manage_link
175 if group_class == "role" && @requested_manager_uuid
176 act_as_system_user do
177 Link.create!(tail_uuid: @requested_manager_uuid,
178 head_uuid: self.uuid,
179 link_class: "permission",