예제 #1
0
        public void UpdatePackage(Package package)
        {
            if (_indexContainsAllVersions)
            {
                //just update everything since the last write time
                UpdateIndex(forceRefresh: false);
                return;
            }

            // when we only store the latest, we can run the rest of this
            if (_getShouldAutoUpdate())
            {
                var packageRegistrationKey = package.PackageRegistrationKey;
                var updateTerm             = new Term("PackageRegistrationKey", packageRegistrationKey.ToString(CultureInfo.InvariantCulture));

                if (!package.IsLatest || !package.IsLatestStable)
                {
                    // Someone passed us in a version which was e.g. just unlisted? Or just not the latest version which is what we want to index. Doesn't really matter. We'll find one to index.
                    using (var context = _contextThunk())
                    {
                        var packageRepo = new EntityRepository <Package>(context);
                        package = packageRepo.GetAll()
                                  .Where(p => (p.IsLatest || p.IsLatestStable) && p.PackageRegistrationKey == packageRegistrationKey)
                                  .Include(p => p.PackageRegistration)
                                  .Include(p => p.PackageRegistration.Owners)
                                  .Include(p => p.SupportedFrameworks)
                                  .FirstOrDefault();
                    }
                }

                // Just update the provided package
                EnsureIndexWriter(creatingIndex: false);
                if (package != null)
                {
                    var indexEntity = new PackageIndexEntity(package);
                    _indexWriter.UpdateDocument(updateTerm, indexEntity.ToDocument());
                }
                else
                {
                    _indexWriter.DeleteDocuments(updateTerm);
                }
                _indexWriter.Commit();
            }
        }
        private List <PackageIndexEntity> GetPackages(DateTime?lastIndexTime)
        {
            using (var context = _contextThunk())
            {
                var packageRepo = new EntityRepository <Package>(context);

                IQueryable <Package> set = packageRepo.GetAll();

                if (lastIndexTime.HasValue)
                {
                    // Retrieve the Latest and LatestStable version of packages if any package for that registration changed since we last updated the index.
                    // We need to do this because some attributes that we index such as DownloadCount are values in the PackageRegistration table that may
                    // update independent of the package.
                    set = set.Where(
                        p => (_indexContainsAllVersions || p.IsLatest || p.IsLatestStable) &&
                        p.PackageRegistration.Packages.Any(p2 => p2.LastUpdated > lastIndexTime));
                }
                else if (!_indexContainsAllVersions)
                {
                    set = set.Where(p => p.IsLatest || p.IsLatestStable); // which implies that p.IsListed by the way!
                }
                else
                {
                    // get everything including unlisted
                    set = set.Where(p => p.StatusForDatabase != _rejectedStatus || p.StatusForDatabase == null);
                }

                var list = set
                           .Include(p => p.PackageRegistration)
                           .Include(p => p.PackageRegistration.Owners)
                           .Include(p => p.SupportedFrameworks)
                           .ToList();

                var packagesForIndexing = list.Select(
                    p => new PackageIndexEntity
                {
                    Package = p
                });

                return(packagesForIndexing.ToList());
            }
        }
예제 #3
0
 public TestableUserServiceWithDBFaking()
 {
     Config         = (MockConfig = new Mock <IAppConfiguration>()).Object;
     UserRepository = new EntityRepository <User>(FakeEntitiesContext = new FakeEntitiesContext());
 }
예제 #4
0
        public override void Load()
        {
            IConfiguration configuration = new Configuration();

            Bind <IConfiguration>()
            .ToMethod(context => configuration);

            Lazy <GallerySetting> gallerySetting = new Lazy <GallerySetting>(() =>
            {
                using (var entitiesContext = new EntitiesContext())
                {
                    var settingsRepo = new EntityRepository <GallerySetting>(entitiesContext);
                    return(settingsRepo.GetAll().FirstOrDefault());
                }
            });

            Bind <GallerySetting>().ToMethod(c => gallerySetting.Value);

            Bind <EntitiesContext>()
            .ToMethod(context => new EntitiesContext())
            .InRequestScope();

            Bind <IEntityRepository <User> >()
            .To <EntityRepository <User> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageRegistration> >()
            .To <EntityRepository <PackageRegistration> >()
            .InRequestScope();

            Bind <IEntityRepository <Package> >()
            .To <EntityRepository <Package> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageAuthor> >()
            .To <EntityRepository <PackageAuthor> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageDependency> >()
            .To <EntityRepository <PackageDependency> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageStatistics> >()
            .To <EntityRepository <PackageStatistics> >()
            .InRequestScope();

            Bind <IUserService>()
            .To <UserService>()
            .InRequestScope();

            Bind <IPackageService>()
            .To <PackageService>()
            .InRequestScope();

            Bind <ICryptographyService>()
            .To <CryptographyService>()
            .InRequestScope();

            Bind <IFormsAuthenticationService>()
            .To <FormsAuthenticationService>()
            .InSingletonScope();

            Bind <IControllerFactory>()
            .To <NuGetControllerFactory>()
            .InRequestScope();

            Lazy <IMailSender> mailSenderThunk = new Lazy <IMailSender>(() =>
            {
                var settings = Kernel.Get <GallerySetting>();
                if (settings.UseSmtp)
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod        = SmtpDeliveryMethod.Network,
                        Host                  = settings.SmtpHost,
                        Port                  = settings.SmtpPort,
                        EnableSsl             = true,
                        UseDefaultCredentials = false,
                        Credentials           = new NetworkCredential(
                            settings.SmtpUsername,
                            settings.SmtpPassword)
                    };

                    return(new MailSender(mailSenderConfiguration));
                }
                else
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                        PickupDirectoryLocation = HostingEnvironment.MapPath("~/App_Data/Mail")
                    };

                    return(new MailSender(mailSenderConfiguration));
                }
            });

            Bind <IMailSender>()
            .ToMethod(context => mailSenderThunk.Value);

            Bind <IMessageService>()
            .To <MessageService>();

            Bind <IPrincipal>().ToMethod(context => HttpContext.Current.User);

            switch (configuration.PackageStoreType)
            {
            case PackageStoreType.FileSystem:
            case PackageStoreType.NotSpecified:
                Bind <IFileSystemService>()
                .To <FileSystemService>()
                .InSingletonScope();
                Bind <IFileStorageService>()
                .To <FileSystemFileStorageService>()
                .InSingletonScope();
                break;

            case PackageStoreType.AzureStorageBlob:
                Bind <ICloudBlobClient>()
                .ToMethod(context => new CloudBlobClientWrapper(new CloudBlobClient(
                                                                    new Uri(configuration.AzureStorageBlobUrl, UriKind.Absolute),
                                                                    new StorageCredentialsAccountAndKey(configuration.AzureStorageAccountName, configuration.AzureStorageAccessKey))))
                .InSingletonScope();
                Bind <IFileStorageService>()
                .To <CloudBlobFileStorageService>()
                .InSingletonScope();
                break;
            }

            Bind <IPackageFileService>()
            .To <PackageFileService>();

            Bind <IEntityRepository <PackageOwnerRequest> >()
            .To <EntityRepository <PackageOwnerRequest> >()
            .InRequestScope();

            Bind <IUploadFileService>()
            .To <UploadFileService>();
        }
예제 #5
0
        /// <summary>
        ///   Loads the module into the kernel.
        /// </summary>
        public void RegisterComponents(SimpleInjector.Container container)
        {
            IConfiguration configuration = new Configuration();

            container.Register <IConfiguration>(() => configuration, Lifestyle.Singleton);

            Lazy <GallerySetting> gallerySetting = new Lazy <GallerySetting>(() =>
            {
                using (var entitiesContext = new EntitiesContext())
                {
                    var settingsRepo = new EntityRepository <GallerySetting>(entitiesContext);
                    return(settingsRepo.GetAll().FirstOrDefault());
                }
            });

            container.Register <GallerySetting>(() => gallerySetting.Value);
            //Bind<GallerySetting>().ToMethod(c => gallerySetting.Value);

            container.RegisterPerWebRequest <ISearchService, LuceneSearchService>();

            container.RegisterPerWebRequest <IEntitiesContext>(() => new EntitiesContext());
            container.RegisterPerWebRequest <IEntityRepository <User>, EntityRepository <User> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageRegistration>, EntityRepository <PackageRegistration> >();
            container.RegisterPerWebRequest <IEntityRepository <Package>, EntityRepository <Package> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageAuthor>, EntityRepository <PackageAuthor> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageFramework>, EntityRepository <PackageFramework> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageDependency>, EntityRepository <PackageDependency> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageFile>, EntityRepository <PackageFile> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageStatistics>, EntityRepository <PackageStatistics> >();
            container.RegisterPerWebRequest <IEntityRepository <PackageOwnerRequest>, EntityRepository <PackageOwnerRequest> >();

            container.RegisterPerWebRequest <IUserService, UserService>();
            container.RegisterPerWebRequest <IPackageService, PackageService>();
            container.RegisterPerWebRequest <ICryptographyService, CryptographyService>();

            container.Register <IFormsAuthenticationService, FormsAuthenticationService>(Lifestyle.Singleton);

            container.RegisterPerWebRequest <IControllerFactory, NuGetControllerFactory>();
            container.RegisterPerWebRequest <IIndexingService, LuceneIndexingService>();
            container.RegisterPerWebRequest <INuGetExeDownloaderService, NuGetExeDownloaderService>();

            Lazy <IMailSender> mailSenderThunk = new Lazy <IMailSender>(() =>
            {
                var settings = container.GetInstance <GallerySetting>();
                if (settings.UseSmtp)
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Host           = settings.SmtpHost,
                        Port           = settings.SmtpPort,
                        EnableSsl      = configuration.SmtpEnableSsl,
                    };

                    if (!String.IsNullOrWhiteSpace(settings.SmtpUsername))
                    {
                        mailSenderConfiguration.UseDefaultCredentials = false;
                        mailSenderConfiguration.Credentials           = new NetworkCredential(
                            settings.SmtpUsername,
                            settings.SmtpPassword);
                    }

                    return(new NuGetGallery.MailSender(mailSenderConfiguration));
                }
                else
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                        PickupDirectoryLocation = HostingEnvironment.MapPath("~/App_Data/Mail")
                    };

                    return(new NuGetGallery.MailSender(mailSenderConfiguration));
                }
            });

            container.Register <IMailSender>(() => mailSenderThunk.Value, Lifestyle.Singleton);

            container.Register <IMessageService, MessageService>(Lifestyle.Singleton);

            container.RegisterPerWebRequest <IPrincipal>(() => HttpContext.Current.User);
            //Bind<IPrincipal>().ToMethod(context => HttpContext.Current.User);

            switch (configuration.PackageStoreType)
            {
            case PackageStoreType.FileSystem:
            case PackageStoreType.NotSpecified:
                container.Register <IFileSystemService, FileSystemService>(Lifestyle.Singleton);
                container.Register <IFileStorageService, FileSystemFileStorageService>(Lifestyle.Singleton);
                break;

            case PackageStoreType.AzureStorageBlob:
                container.Register <ICloudBlobClient>(() => new CloudBlobClientWrapper(new CloudBlobClient(
                                                                                           new Uri(configuration.AzureStorageBlobUrl, UriKind.Absolute),
                                                                                           new StorageCredentialsAccountAndKey(configuration.AzureStorageAccountName, configuration.AzureStorageAccessKey)
                                                                                           ))
                                                      , Lifestyle.Singleton);
                container.Register <IFileStorageService, CloudBlobFileStorageService>(Lifestyle.Singleton);
                break;

            case PackageStoreType.AmazonS3Storage:
                container.Register <IAmazonS3Client, AmazonS3ClientWrapper>(Lifestyle.Singleton);
                container.Register <IFileStorageService, AmazonS3FileStorageService>(Lifestyle.Singleton);
                break;
            }

            container.Register <IPackageFileService, PackageFileService>(Lifestyle.Singleton);
            container.Register <IUploadFileService, UploadFileService>();

            // todo: bind all package curators by convention
            container.Register <IAutomaticPackageCurator, WebMatrixPackageCurator>(Lifestyle.Singleton);
            container.Register <IAutomaticPackageCurator, Windows8PackageCurator>(Lifestyle.Singleton);

            // todo: bind all commands by convention
            container.RegisterPerWebRequest <IAutomaticallyCuratePackageCommand, AutomaticallyCuratePackageCommand>();
            container.RegisterPerWebRequest <ICreateCuratedPackageCommand, CreateCuratedPackageCommand>();
            container.RegisterPerWebRequest <IDeleteCuratedPackageCommand, DeleteCuratedPackageCommand>();
            container.RegisterPerWebRequest <IModifyCuratedPackageCommand, ModifyCuratedPackageCommand>();

            // todo: bind all queries by convention
            container.RegisterPerWebRequest <ICuratedFeedByKeyQuery, CuratedFeedByKeyQuery>();
            container.RegisterPerWebRequest <ICuratedFeedByNameQuery, CuratedFeedByNameQuery>();
            container.RegisterPerWebRequest <ICuratedFeedsByManagerQuery, CuratedFeedsByManagerQuery>();
            container.RegisterPerWebRequest <IPackageRegistrationByKeyQuery, PackageRegistrationByKeyQuery>();
            container.RegisterPerWebRequest <IPackageRegistrationByIdQuery, PackageRegistrationByIdQuery>();
            container.RegisterPerWebRequest <IUserByUsernameQuery, UserByUsernameQuery>();
            container.RegisterPerWebRequest <IPackageIdsQuery, PackageIdsQuery>();
            container.RegisterPerWebRequest <IPackageVersionsQuery, PackageVersionsQuery>();

            container.RegisterPerWebRequest <IAggregateStatsService, AggregateStatsService>();

            RegisterChocolateySpecific(container);
        }
예제 #6
0
        public override void Load()
        {
            IConfiguration configuration = new Configuration();

            Bind <IConfiguration>()
            .ToMethod(context => configuration);

            Lazy <GallerySetting> gallerySetting = new Lazy <GallerySetting>(() =>
            {
                using (var entitiesContext = new EntitiesContext())
                {
                    var settingsRepo = new EntityRepository <GallerySetting>(entitiesContext);
                    return(settingsRepo.GetAll().FirstOrDefault());
                }
            });

            Bind <GallerySetting>().ToMethod(c => gallerySetting.Value);

            Bind <ISearchService>()
            .To <LuceneSearchService>()
            .InRequestScope();

            Bind <IEntitiesContext>()
            .ToMethod(context => new EntitiesContext())
            .InRequestScope();

            Bind <IEntityRepository <User> >()
            .To <EntityRepository <User> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageRegistration> >()
            .To <EntityRepository <PackageRegistration> >()
            .InRequestScope();

            Bind <IEntityRepository <Package> >()
            .To <EntityRepository <Package> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageAuthor> >()
            .To <EntityRepository <PackageAuthor> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageDependency> >()
            .To <EntityRepository <PackageDependency> >()
            .InRequestScope();

            Bind <IEntityRepository <PackageStatistics> >()
            .To <EntityRepository <PackageStatistics> >()
            .InRequestScope();

            Bind <IUserService>()
            .To <UserService>()
            .InRequestScope();

            Bind <IPackageService>()
            .To <PackageService>()
            .InRequestScope();

            Bind <ICryptographyService>()
            .To <CryptographyService>()
            .InRequestScope();

            Bind <IFormsAuthenticationService>()
            .To <FormsAuthenticationService>()
            .InSingletonScope();

            Bind <IControllerFactory>()
            .To <NuGetControllerFactory>()
            .InRequestScope();

            Bind <IIndexingService>()
            .To <LuceneIndexingService>()
            .InRequestScope();

            Bind <INuGetExeDownloaderService>()
            .To <NuGetExeDownloaderService>()
            .InRequestScope();

            Lazy <IMailSender> mailSenderThunk = new Lazy <IMailSender>(() =>
            {
                var settings = Kernel.Get <GallerySetting>();
                if (settings.UseSmtp)
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Host           = settings.SmtpHost,
                        Port           = settings.SmtpPort,
                        EnableSsl      = true
                    };

                    if (!String.IsNullOrWhiteSpace(settings.SmtpUsername))
                    {
                        mailSenderConfiguration.UseDefaultCredentials = false;
                        mailSenderConfiguration.Credentials           = new NetworkCredential(
                            settings.SmtpUsername,
                            settings.SmtpPassword);
                    }

                    return(new MailSender(mailSenderConfiguration));
                }
                else
                {
                    var mailSenderConfiguration = new MailSenderConfiguration()
                    {
                        DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                        PickupDirectoryLocation = HostingEnvironment.MapPath("~/App_Data/Mail")
                    };

                    return(new MailSender(mailSenderConfiguration));
                }
            });

            Bind <IMailSender>()
            .ToMethod(context => mailSenderThunk.Value);

            Bind <IMessageService>()
            .To <MessageService>();

            Bind <IPrincipal>().ToMethod(context => HttpContext.Current.User);

            switch (configuration.PackageStoreType)
            {
            case PackageStoreType.FileSystem:
            case PackageStoreType.NotSpecified:
                Bind <IFileStorageService>()
                .To <FileSystemFileStorageService>()
                .InSingletonScope();
                break;

            case PackageStoreType.AzureStorageBlob:
                Bind <ICloudBlobClient>()
                .ToMethod(context => new CloudBlobClientWrapper(new CloudBlobClient(
                                                                    new Uri(configuration.AzureStorageBlobUrl, UriKind.Absolute),
                                                                    configuration.UseEmulator ?
                                                                    CloudStorageAccount.DevelopmentStorageAccount.Credentials :
                                                                    new StorageCredentialsAccountAndKey(configuration.AzureStorageAccountName, configuration.AzureStorageAccessKey))))
                .InSingletonScope();
                Bind <IFileStorageService>()
                .To <CloudBlobFileStorageService>()
                .InSingletonScope();
                break;
            }

            Bind <IFileSystemService>()
            .To <FileSystemService>()
            .InSingletonScope();

            Bind <IPackageFileService>()
            .To <PackageFileService>();

            Bind <IEntityRepository <PackageOwnerRequest> >()
            .To <EntityRepository <PackageOwnerRequest> >()
            .InRequestScope();

            Bind <IUploadFileService>()
            .To <UploadFileService>();

            // todo: bind all package curators by convention
            Bind <IAutomaticPackageCurator>()
            .To <WebMatrixPackageCurator>();
            Bind <IAutomaticPackageCurator>()
            .To <Windows8PackageCurator>();

            // todo: bind all commands by convention
            Bind <IAutomaticallyCuratePackageCommand>()
            .To <AutomaticallyCuratePackageCommand>()
            .InRequestScope();
            Bind <ICreateCuratedPackageCommand>()
            .To <CreateCuratedPackageCommand>()
            .InRequestScope();
            Bind <IDeleteCuratedPackageCommand>()
            .To <DeleteCuratedPackageCommand>()
            .InRequestScope();
            Bind <IModifyCuratedPackageCommand>()
            .To <ModifyCuratedPackageCommand>()
            .InRequestScope();

            // todo: bind all queries by convention
            Bind <ICuratedFeedByKeyQuery>()
            .To <CuratedFeedByKeyQuery>()
            .InRequestScope();
            Bind <ICuratedFeedByNameQuery>()
            .To <CuratedFeedByNameQuery>()
            .InRequestScope();
            Bind <ICuratedFeedsByManagerQuery>()
            .To <CuratedFeedsByManagerQuery>()
            .InRequestScope();
            Bind <IPackageRegistrationByKeyQuery>()
            .To <PackageRegistrationByKeyQuery>()
            .InRequestScope();
            Bind <IPackageRegistrationByIdQuery>()
            .To <PackageRegistrationByIdQuery>()
            .InRequestScope();
            Bind <IUserByUsernameQuery>()
            .To <UserByUsernameQuery>()
            .InRequestScope();

            Bind <IAggregateStatsService>()
            .To <AggregateStatsService>()
            .InRequestScope();
            Bind <IPackageIdsQuery>()
            .To <PackageIdsQuery>()
            .InRequestScope();
            Bind <IPackageVersionsQuery>()
            .To <PackageVersionsQuery>()
            .InRequestScope();
        }