SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_system_UserCanReadSection]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_system_UserCanReadSection]
GO

CREATE PROCEDURE [dbo].cs_system_UserCanReadSection
(
	@UserID int,
	@SectionID int,
	@SettingsID int
)
AS
/*
Figure out if an user has View or Read access permissions on provided Section ID.
*/
SET Transaction Isolation Level Read UNCOMMITTED
BEGIN

DECLARE @HasAccess bit
 
-- Set default values
SET @HasAccess = 0

-- Has view & read access permission?
IF EXISTS(
	SELECT TOP 1 
		UserID
	FROM
		cs_vw_UsersInRoles ur
		INNER JOIN cs_SectionPermissions sp ON ur.RoleId = sp.RoleID AND sp.SettingsID = @SettingsID
	WHERE
		ur.UserID = @UserID AND 
		sp.SectionID = @SectionID AND
		sp.AllowMask & convert(bigint, 0x0000000000000001) = convert(bigint, 0x0000000000000001) AND
    sp.AllowMask & convert(bigint, 0x0000000000000002) = convert(bigint, 0x0000000000000002)
)	
	SET @HasAccess = 1

RETURN @HasAccess

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

grant execute on [dbo].[cs_system_UserCanReadSection] to public

