USE [Elyse_DB] GO /****** Object: StoredProcedure [internal].[usp_AUTHENTICATE_tag_edit] 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: 23-09-2024 -- Description: Checks whether the connected user has permission -- to edit a given tag. -- Input is a the id of the tag to be checked. -- Output is a status string of 'Pass' or 'Fail'. -- A user has permission for a tag if either there are no chain from the tag group ID through to any user, -- or if the tag is linked to a tag group that the user has rights to. -- If the user is a controller then they have rights to edit any tag by default. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2024 */ -- ============================================= CREATE PROCEDURE [internal].[usp_AUTHENTICATE_tag_edit] @tag_id_ep bigint = NULL, @user_authentication_result_tag 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_tag = '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_tag = 'Pass' -- If the user is a controller at this step then they are granted permission IF @user_authentication_result_tag <> 'Pass' AND -- User is not a controller. If there are no tag groups linked then pass. NOT EXISTS (SELECT tag_id FROM tagging.tag_to_tag_group_links AS tttgl WHERE tttgl.tag_id = @tag_id_ep) BEGIN SET @user_authentication_result_tag = 'Pass' END -- If there are no users linked to the tag through a tag group then pass IF @user_authentication_result_tag <> 'Pass' AND NOT EXISTS ( (SELECT sl.sid_id FROM tagging.doc_tags AS dt INNER JOIN tagging.tag_to_tag_group_links AS tttgl ON dt.tag_id = tttgl.tag_id INNER JOIN tagging.tag_group_identifier_names AS tgin ON tttgl.tag_group_id = tgin.tag_group_id INNER JOIN user_restr.tag_group_permissions_flist AS tgpf ON tgin.tag_group_id = tgpf.tag_group_id INNER JOIN people.function_list_names AS fln ON tgpf.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 dt.tag_id = @tag_id_ep UNION ALL (SELECT sl.sid_id FROM tagging.doc_tags AS dt2 INNER JOIN tagging.tag_to_tag_group_links AS tttgl2 ON dt2.tag_id = tttgl2.tag_id INNER JOIN tagging.tag_group_identifier_names AS tgin2 ON tttgl2.tag_group_id = tgin2.tag_group_id INNER JOIN user_restr.tag_group_permissions_plist AS tgpp ON tgin2.tag_group_id = tgpp.tag_group_id INNER JOIN people.people_list_names AS pln ON tgpp.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 dt2.tag_id = @tag_id_ep ))) BEGIN SET @user_authentication_result_tag = 'Pass' END -- If the user is linked through to the tag through a tag group then pass IF @user_authentication_result_tag <> 'Pass' AND EXISTS ( (SELECT sl.sid_id FROM tagging.doc_tags AS dt INNER JOIN tagging.tag_to_tag_group_links AS tttgl ON dt.tag_id = tttgl.tag_id INNER JOIN tagging.tag_group_identifier_names AS tgin ON tttgl.tag_group_id = tgin.tag_group_id INNER JOIN user_restr.tag_group_permissions_flist AS tgpf ON tgin.tag_group_id = tgpf.tag_group_id INNER JOIN people.function_list_names AS fln ON tgpf.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 dt.tag_id = @tag_id_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 FROM tagging.doc_tags AS dt2 INNER JOIN tagging.tag_to_tag_group_links AS tttgl2 ON dt2.tag_id = tttgl2.tag_id INNER JOIN tagging.tag_group_identifier_names AS tgin2 ON tttgl2.tag_group_id = tgin2.tag_group_id INNER JOIN user_restr.tag_group_permissions_plist AS tgpp ON tgin2.tag_group_id = tgpp.tag_group_id INNER JOIN people.people_list_names AS pln ON tgpp.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 dt2.tag_id = @tag_id_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_tag = 'Pass'; END END GO