Exemplo n.º 1
0
        public virtual async Task <IActionResult> Index(InstallModel model)
        {
            if (DataSettingsManager.IsDatabaseInstalled())
            {
                return(RedirectToRoute("Homepage"));
            }

            model.DisableSampleDataOption  = _appSettings.Get <InstallationConfig>().DisableSampleData;
            model.InstallRegionalResources = _appSettings.Get <InstallationConfig>().InstallRegionalResources;

            PrepareAvailableDataProviders(model);
            PrepareLanguageList(model);
            PrepareCountryList(model);

            //Consider granting access rights to the resource to the ASP.NET request identity.
            //ASP.NET has a base process identity
            //(typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7,
            //and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating.
            //If the application is impersonating via <identity impersonate="true"/>,
            //the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

            //validate permissions
            var dirsToCheck = _fileProvider.GetDirectoriesWrite();

            foreach (var dir in dirsToCheck)
            {
                if (!_fileProvider.CheckPermissions(dir, false, true, true, false))
                {
                    ModelState.AddModelError(string.Empty, string.Format(_locService.Value.GetResource("ConfigureDirectoryPermissions"), CurrentOSUser.FullName, dir));
                }
            }

            var filesToCheck = _fileProvider.GetFilesWrite();

            foreach (var file in filesToCheck)
            {
                if (!_fileProvider.FileExists(file))
                {
                    continue;
                }

                if (!_fileProvider.CheckPermissions(file, false, true, true, true))
                {
                    ModelState.AddModelError(string.Empty, string.Format(_locService.Value.GetResource("ConfigureFilePermissions"), CurrentOSUser.FullName, file));
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                var dataProvider = DataProviderManager.GetDataProvider(model.DataProvider);

                var connectionString = model.ConnectionStringRaw ? model.ConnectionString : dataProvider.BuildConnectionString(model);

                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new Exception(_locService.Value.GetResource("ConnectionStringWrongFormat"));
                }

                DataSettingsManager.SaveSettings(new DataConfig
                {
                    DataProvider     = model.DataProvider,
                    ConnectionString = connectionString
                }, _fileProvider);

                if (model.CreateDatabaseIfNotExists)
                {
                    try
                    {
                        dataProvider.CreateDatabase(model.Collation);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format(_locService.Value.GetResource("DatabaseCreationError"), ex.Message));
                    }
                }
                else
                {
                    //check whether database exists
                    if (!await dataProvider.DatabaseExistsAsync())
                    {
                        throw new Exception(_locService.Value.GetResource("DatabaseNotExists"));
                    }
                }

                dataProvider.InitializeDatabase();

                var cultureInfo = new CultureInfo(NopCommonDefaults.DefaultLanguageCulture);
                var regionInfo  = new RegionInfo(NopCommonDefaults.DefaultLanguageCulture);

                var languagePackInfo = (DownloadUrl : string.Empty, Progress : 0);
                if (model.InstallRegionalResources)
                {
                    //try to get CultureInfo and RegionInfo
                    try
                    {
                        cultureInfo = new CultureInfo(model.Country[3..]);
                        regionInfo  = new RegionInfo(model.Country[3..]);