USE [Elyse_DB] GO /****** Object: UserDefinedFunction [internal].[ufn_SEL_file_by_doc_fgroup_TVF] Script Date: Sun 22-11-2026 9:04:48 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Create date: 14-06-2024 -- Description: Retrieves the file ids of a document for a given filter group. /* The logic works as follows. Files are listed which: - the user has viewing rights to, - are linked to the document id via the table xref.file_to_document_links, - are linked to all the attributes which have the given filter group assigned. For example, the required attributs might be Distribution Format from the Format list. This would be as opposed to Source File Format. Only files linked to the document and which have this attribute will be retrieved. For multi-select lists, all the attributes with the filter group linked must be linked to the file for the file to be included. The function applies to both radio button lists and multi-select lists. */ /* COPYRIGHT NOTICE This database schema and contained code, including this function are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2024 */ -- ============================================= CREATE FUNCTION [internal].[ufn_SEL_file_by_doc_fgroup_TVF] ( @documentid nvarchar(50), @filtergroupid bigint ) RETURNS @FileID TABLE ( file_id bigint ) AS BEGIN DECLARE @connectedusersid varbinary(100) = SUSER_SID(ORIGINAL_LOGIN()), -- The SID of the connected user @username nvarchar(150) = ORIGINAL_LOGIN(), -- The username @now datetime2(7) = SYSDATETIME(); -- Create a CTE containing files linked to the given document ID -- and which the user has permission to view. WITH DocFiles AS ( SELECT DISTINCT fm.file_id AS fi FROM base.file_metadata AS fm INNER JOIN xref.file_to_document_links AS ftdl ON fm.file_id = ftdl.file_id WHERE ftdl.doc_id = @documentid AND fm.file_id IS NOT NULL AND ( -- No linked groups have any users: -- therefore the file is unrestricted. NOT EXISTS ( SELECT dgl2.doc_group_id FROM xref.doc_group_links AS dgl2 INNER JOIN user_restr.doc_group_view_permissions AS dgvp2 ON dgl2.doc_group_id = dgvp2.doc_group_id WHERE dgl2.doc_id = ftdl.doc_id ) OR -- At least one linked group has users, and -- the connected user has a currently valid permission -- in at least one of those groups. EXISTS ( -- Only retrieve file data for files which the user has permission to access SELECT dgl2.doc_group_id FROM xref.doc_group_links AS dgl2 INNER JOIN user_restr.doc_group_view_permissions AS dgvp2 ON dgl2.doc_group_id = dgvp2.doc_group_id INNER JOIN user_restr.sid_list AS sl2 ON dgvp2.sid_id = sl2.sid_id WHERE dgl2.doc_id = ftdl.doc_id AND sl2.sid = @connectedusersid AND (dgvp2.valid_from IS NULL OR dgvp2.valid_from <= @now) AND (dgvp2.valid_until IS NULL OR dgvp2.valid_until >= @now) ) ) -- Apply the filter group to the file id AND internal.ufn_CHK_filter_file_id_SVF( fm.file_id, @filtergroupid ) <> 'Fail' ) -- Add the final results to the return table INSERT INTO @FileID (file_id) SELECT df.fi FROM DocFiles df RETURN; END; GO