SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

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

CREATE      PROCEDURE [dbo].cs_user_Get
(
	@UserID int,
	@Username nvarchar(64) = null,
	@IsOnline bit = 0,
	@LastAction nvarchar(1024) = '',
	@SettingsID int
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
BEGIN
DECLARE @UserIDGUID as uniqueidentifier

	
	-- Are we looking up the user by username or ID?
	IF @Username is not null
	BEGIN
		
		SELECT
			@UserIDGUID = U.UserId
		FROM
			aspnet_Users U,
			cs_SiteSettings S,
			aspnet_Applications A
		WHERE
			S.SettingsID = @SettingsID AND
			A.ApplicationName = S.ApplicationName AND
			A.ApplicationId = U.ApplicationId AND
			U.UserName = @Username

		SELECT
			@UserID = UserID
		FROM
			cs_Users M
		WHERE
			M.MembershipID = @UserIDGUID

		SELECT
			U.*
		FROM
			cs_vw_Users_FullUser U (nolock)
		WHERE 
			UserId = @UserIDGUID AND U.SettingsID = @SettingsID


	END
	ELSE BEGIN
		-- Looking up the user by ID
		
		if(@UserID = 0)
		Begin
			exec cs_GetAnonymousUserID @SettingsID, @UserID output
		End
	
		-- Get the user details
		SELECT
			U.*
		FROM 
			cs_vw_Users_FullUser U (nolock)
		WHERE 
			U.cs_UserID = @UserID and U.SettingsID = @SettingsID
	END

	IF @IsOnline = 1
	BEGIN
		EXEC cs_system_UserIsOnline @UserID, @LastAction, @SettingsID
	END
END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

grant execute on [dbo].[cs_user_Get] to public
go