USE [Elyse_DB]
GO
/****** Object:  StoredProcedure [internal].[usp_AUTHENTICATE_wf_instance]    Script Date: Sat 05-09-2026 7:01:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Silkwood Software
-- Create date: 14-09-2024
-- Description:	Checks whether the connected user has permission
-- to access a given workflow instance according to the associated document ID. 
-- Input is a the id of the workflow instance to be checked.  
-- Output is a status string of 'Pass' or 'Fail'.
/*
COPYRIGHT NOTICE
This database schema and stored procedures are protected by copyright.
Copyright.  Silkwood Software Pty. Ltd. 2023
*/

-- =============================================
CREATE PROCEDURE [internal].[usp_AUTHENTICATE_wf_instance] 

	@wf_instance_to_check bigint,
	@wf_instance_auth_result nvarchar(10) 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
		@documentid nvarchar(50),
		@docauthenticationstatus nvarchar(10);



  -- Select the document ID associated with the step
     SELECT @documentid = wid.doc_id
       FROM workflow_instances.workflow_instance_definitions as wid
	  WHERE wid.workflow_instance_id
	       = @wf_instance_to_check;

  IF @documentid IS NULL
   SET @wf_instance_auth_result = 'Pass';
  ELSE -- A doc id exists
     BEGIN   -- Check if the document ID exists and the user has permission to access the document
			EXEC [internal].[usp_AUTHENTICATE_user_doc_id] 
 				@doc_id_to_check =  @documentid,
				@user_authentication_result = @wf_instance_auth_result OUTPUT;
	 END

END
GO
