VJSGlobalContext* VRIAServerJSContextMgr::RetainJSContext( VError& outError, const VJSContext& inParentContext, bool inReusable)
{
	VJSGlobalContext *result = NULL;
	outError = VE_OK;

	if (inParentContext != NULL)
	{
		VRIAJSRuntimeContext *runtimeContext = VRIAJSRuntimeContext::GetFromJSContext( inParentContext);
		if (runtimeContext != NULL)
		{
			VRIAServerProject *application = runtimeContext->GetRootApplication();
			if (application != NULL)
			{
				result = application->RetainJSContext( outError, inReusable, NULL);
			}
			else
			{
				result = VRIAServerApplication::Get()->RetainJSContext( outError, inReusable);
			}
		}
	}

	return result;
}
bool VJSDebuggerSettings::UserCanDebug ( const UniChar* inUserName, const UniChar* inUserPassword ) const
{
	if ( fServerSolution == 0 )
	{
		xbox_assert ( false );

		return false; // Something went really wrong
	}

	if ( !NeedsAuthentication ( ) )
		return true;

	CUAGDirectory*						uagDirectory = fServerSolution-> RetainUAGDirectory ( );
	if ( uagDirectory == 0 )
		return true; // No directory defined at all so access is not protected

	VString								vstrUserName ( inUserName );
	VString								vstrUserPassword ( inUserPassword );

	bool								bCanDebug = false;
	VError								vError = VE_OK;
	CUAGSession*						cSession = NULL;

	if ((uagDirectory != NULL) && uagDirectory->HasLoginListener())
	{
		// sc 22/06/2012, custom JavaScript authentication support
		VectorOfApplication applications;
		fServerSolution->GetApplications( applications);
		VRIAServerProject *app = (!applications.empty()) ? applications.front() : NULL;
		if (app != NULL)
		{
			VJSGlobalContext *globalContext = app->RetainJSContext( vError, true, NULL);
			if (vError == VE_OK)
			{
				if (testAssert(globalContext != NULL))
				{
					VJSContext jsContext( globalContext);
					cSession = uagDirectory-> OpenSession ( inUserName, inUserPassword, &vError, &jsContext );
				}
				else
				{
					vError = VE_UNKNOWN_ERROR;
				}
			}
			app->ReleaseJSContext( globalContext, NULL);
		}
	}
	else
	{
		cSession = uagDirectory-> OpenSession ( inUserName, inUserPassword, &vError, NULL );
	}
	
	if ( cSession != 0 && vError  == VE_OK )
	{
		VUUID			vuuidDebuggerGroup;
		bool			bOK = uagDirectory-> GetSpecialGroupID ( CUAGDirectory::DebuggerGroup, vuuidDebuggerGroup );
		xbox_assert ( bOK );
		if ( cSession-> BelongsTo ( vuuidDebuggerGroup ) )
			bCanDebug = true;
	}

	ReleaseRefCountable ( &cSession );
	ReleaseRefCountable ( &uagDirectory );
	
	return bCanDebug;
}