USE [Elyse_DB] GO /****** Object: StoredProcedure [internal].[usp_AUTHENTICATE_tag_br_tree] Script Date: Wed 21-10-2026 4:50:16 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Create date: 26-09-2024 -- Description: Checks whether the connected user has permission -- to edit a given tag browsing tree. -- Also returns fail if the id does not exist. -- Input is a the id of the tag tree to be checked. -- Output is a status string of 'Pass' or 'Fail'. -- A user has permission to edit a tag browsing tree if there is a chain from the tag browsing tree ID through -- either a function list or a people list to a sid in the sid list which is the sid for the user or a sid the user is a member of. -- If the user is a controller then they have rights to any tag tree by default. -- The user also has permission if the tree is linked directly to the SID_ID via the sid_id column. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2024 */ -- ============================================= CREATE PROCEDURE [internal].[usp_AUTHENTICATE_tag_br_tree] @tag_brtreeid_ep bigint = NULL, @user_authentication_result_bt nvarchar(10) = NULL OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @usersid varbinary(100) = NULL, -- The SID of the connected user @iscontroller nvarchar(5) = NULL, @now datetime2(7) = SYSDATETIME(); -- Parameters which have been initialised at declaration but not explicitly set will be output as null to calling functions. SET @user_authentication_result_bt = 'Fail' SET @usersid = SUSER_SID(ORIGINAL_LOGIN()); -- Read the SID of the connected user EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Controller', @user_authentication_result = @iscontroller OUTPUT; -- If user is a controller then they have editing rights to any tag group IF @iscontroller = 'Pass' SET @user_authentication_result_bt = 'Pass' -- If the user is a controller at this step then they are granted permission IF @user_authentication_result_bt <> 'Pass' -- User is not a controller BEGIN IF (EXISTS (SELECT sl.sid_id AS sid FROM tagging.tag_browsing_tree_names AS tbtn INNER JOIN user_restr.tag_br_tree_permissions_flist AS tbtpf ON tbtn.tag_browsing_tree_id = tbtpf.tag_browsing_tree_id INNER JOIN people.function_list_names AS fln ON tbtpf.function_list_id = fln.function_list_id INNER JOIN people.function_lists AS fl ON fln.function_list_id = fl.function_list_id INNER JOIN people.duty_functions AS df ON fl.function_id = df.function_id INNER JOIN people.duty_function_sid_links AS dfsl ON dfsl.function_id = df.function_id INNER JOIN user_restr.sid_list AS sl ON dfsl.sid_id = sl.sid_id WHERE tbtn.tag_browsing_tree_id = @tag_brtreeid_ep AND sl.sid = @usersid -- Check time-bound restrictions AND (fl.valid_from IS NULL OR fl.valid_from <= @now) AND (fl.valid_until IS NULL OR fl.valid_until >= @now) AND (dfsl.valid_from IS NULL OR dfsl.valid_from <= @now) AND (dfsl.valid_until IS NULL OR dfsl.valid_until >= @now) UNION ALL (SELECT sl.sid_id AS sid FROM tagging.tag_browsing_tree_names AS tbtn2 INNER JOIN user_restr.tag_br_tree_permissions_plist AS tbtpp ON tbtn2.tag_browsing_tree_id = tbtpp.tag_browsing_tree_id INNER JOIN people.people_list_names AS pln ON tbtpp.people_list_id = pln.people_list_id INNER JOIN people.people_lists AS pl ON pln.people_list_id = pl.people_list_id INNER JOIN user_restr.sid_list AS sl ON pl.sid_id = sl.sid_id WHERE tbtn2.tag_browsing_tree_id = @tag_brtreeid_ep AND sl.sid = @usersid -- Check time-bound restrictions AND (pl.valid_from IS NULL OR pl.valid_from <= @now) AND (pl.valid_until IS NULL OR pl.valid_until >= @now) ))) BEGIN -- The user has permission for this action SET @user_authentication_result_bt = 'Pass'; END END -- END IF @user_authentication_result_bt <> 'Pass' -- Check if the user is linked directly to the given browsing tree ID. IF @user_authentication_result_bt <> 'Pass' AND EXISTS ( SELECT tbtn.tag_browsing_tree_id FROM tagging.tag_browsing_tree_names AS tbtn INNER JOIN user_restr.sid_list AS sl ON tbtn.sid_id = sl.sid_id WHERE sl.sid = @usersid AND tbtn.tag_browsing_tree_id = @tag_brtreeid_ep ) BEGIN -- The user has permission for this action SET @user_authentication_result_bt = 'Pass'; END END GO