Esempio n. 1
0
        public void Setup(SysSetting setting) {
            var user = _membershipService.CreateUser(setting.AdminUsername, setting.AdminPassword);
            var permissions = _permissionProviders.Select(m => m.GetDefaultStereotypes()).Aggregate((f, s) => f.Concat(s).ToArray()).ToArray();

            var permissionGroups = permissions.GroupBy(m => m.Name);
            foreach (var permissionGroup in permissionGroups) {
                var role = new Role {
                    Name = permissionGroup.Key,
                    CreateAt = DateTime.UtcNow,
                    Description = permissionGroup.Key,
                    UpdateAt = DateTime.UtcNow
                };
                _roleService.Create(role);
                foreach (var permissionStereotype in permissionGroup.Select(m => m.Permissions).Aggregate((f, s) => f.Concat(s))) {
                    _rpRepository.Create(new RolePermission() {
                        Role_Id = role.Id,
                        Permission = permissionStereotype.Name
                    });
                }
                _accountRoleServicecs.AddUserRoles(user.Id, new[] { role.Id });
            }
            _siteService.AddOrUpdate(new SiteSetting {
                SiteName = setting.SiteName,
                SuperUser = setting.AdminUsername,
                PageSize = 20
            });
        }
Esempio n. 2
0
 protected virtual IPersistenceConfigurer DbConfigurer(SysSetting setting) {
     switch (setting.DataProvider) {
         case DatabaseTypes.MySql:
             return MySQLConfiguration.Standard.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.MsSql2000:
             return MsSqlConfiguration.MsSql2000.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.MsSql2005:
             return MsSqlConfiguration.MsSql2005.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.MsSql2008:
             return MsSqlConfiguration.MsSql2008.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.MsSql2012:
             return MsSqlConfiguration.MsSql2012.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.MsSqlCe:
             return MsSqlCeConfiguration.Standard.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.Oracle9:
             return OracleClientConfiguration.Oracle9.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.Oracle10:
             return OracleClientConfiguration.Oracle10.ConnectionString(setting.DataConnectionString);
         case DatabaseTypes.SqLite:
             return SQLiteConfiguration.Standard.UsingFile(setting.DataConnectionString);
         case DatabaseTypes.Db2:
             return DB2Configuration.Standard.ConnectionString(setting.DataConnectionString);
     }
     throw new ArgumentException("database config error");
 }
Esempio n. 3
0
        public void Save(SysSetting setting, IDatabaseSetup databaseSetup) {
            if (setting == null)
                throw new ArgumentNullException("setting");
            if (String.IsNullOrEmpty(setting.SiteName))
                throw new ArgumentException("系统设置名称不能为空", "setting");

            setting.EncryptionAlgorithm = EncryptionAlgorithm;
            setting.EncryptionKey = SymmetricAlgorithm.Create(setting.EncryptionAlgorithm).Key.ToHexString();
            setting.HashAlgorithm = "HMACSHA256";
            setting.HashKey = HMAC.Create(setting.HashAlgorithm).Key.ToHexString();

            Action<Configuration> alterCfg = null;
            if (setting.DataProvider == DatabaseTypes.MsSqlCe || setting.DataProvider == DatabaseTypes.SqLite) {
                var path = Path.Combine(_appDataFolder.MapPath("Sites"), setting.SiteName, setting.DataConnectionString);
                if (setting.DataProvider == DatabaseTypes.MsSqlCe) {
                    setting.DataConnectionString = String.Format("Data Source={0}", path);
                    CreateSqlCeDatabaseFile(path, setting.DataConnectionString);
                    alterCfg = cfg => cfg.SetProperty("connection.release_mode", "on_close");
                } else
                    setting.DataConnectionString = path;
            }

            var filePath = Path.Combine(Path.Combine("Sites", setting.SiteName), SettingsFileName);
            databaseSetup.Setup(setting, alterCfg);
            _appDataFolder.CreateFile(filePath, SettingsSerializer.ComposeSettings(setting));
        }
Esempio n. 4
0
        private ISessionFactory Initialize(SysSetting setting, Boolean installOrUpdate = true, Action<Configuration> alterCfg = null) {
            var fluentConfig = Fluently.Configure()
                .Database(DbConfigurer(setting))
                .Mappings(m => {
                    foreach (var dataProvider in _dataProviders) {
                        dataProvider.MapTypes(m.FluentMappings);
                    }
                }).ExposeConfiguration(cfg => {
                    cfg
                        .SetProperty(NHibernate.Cfg.Environment.FormatSql, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.Hbm2ddlKeyWords, Hbm2DDLKeyWords.None.ToString())
                        .SetProperty(NHibernate.Cfg.Environment.PropertyBytecodeProvider, "lcg")
                        .SetProperty(NHibernate.Cfg.Environment.PropertyUseReflectionOptimizer, Boolean.TrueString)
                        .SetProperty(NHibernate.Cfg.Environment.QueryStartupChecking, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.ShowSql, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.StatementFetchSize, "100")
                        .SetProperty(NHibernate.Cfg.Environment.UseProxyValidator, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.UseSqlComments, Boolean.FalseString)
                        .SetProperty(NHibernate.Cfg.Environment.WrapResultSets, Boolean.TrueString)
                        .SetProperty(NHibernate.Cfg.Environment.BatchSize, "256");

                    if (alterCfg != null)
                        alterCfg(cfg);

                    if (installOrUpdate)
                        new SchemaUpdate(cfg).Execute(false, true);
                });

            var dbConfig = fluentConfig.BuildConfiguration();
#if DEBUG
            dbConfig.SetInterceptor(new SqlStatementInterceptor(Logger));
#endif
            return dbConfig.BuildSessionFactory();
        }
Esempio n. 5
0
        public ActionResult Setup(SetupViewModel model) {
            HttpContext.Server.ScriptTimeout = 600;

            if (string.IsNullOrEmpty(model.DatabaseConnectionString))
                ModelState.AddModelError("DatabaseConnectionString", "链接字符串不能为空");

            if (!String.IsNullOrWhiteSpace(model.ConfirmPassword) && model.AdminPassword != model.ConfirmPassword) {
                ModelState.AddModelError("ConfirmPassword", "两次输入的密码不一致");
            }

            if (!String.IsNullOrWhiteSpace(model.DatabaseTablePrefix)) {
                model.DatabaseTablePrefix = model.DatabaseTablePrefix.Trim();
                if (!Char.IsLetter(model.DatabaseTablePrefix[0])) {
                    ModelState.AddModelError("DatabaseTablePrefix", "表前缀必须是英文字符");
                }

                if (model.DatabaseTablePrefix.Any(x => !Char.IsLetterOrDigit(x))) {
                    ModelState.AddModelError("DatabaseTablePrefix", "表前缀只能包含数字和英文字符");
                }
            }

            try {
                var setting = new SysSetting {
                    SiteName = model.SiteName,
                    DataProvider = model.DatabaseProvider,
                    DataConnectionString = model.DatabaseConnectionString,
                    DataTablePrefix = model.DatabaseTablePrefix,
                    State = TenantState.Running,
                    AdminUsername = model.AdminUsername,
                    AdminPassword = model.AdminPassword
                };

                _settingManager.Save(setting, _databaseSetup);
                _userSetup.Setup(setting);

                return Redirect("/");
            } catch (Exception ex) {
                Logger.Error("Error:{0}", ex);
                return IndexViewResult(model);
            }
        }
Esempio n. 6
0
        public static string ComposeSettings(SysSetting settings) {
            if (settings == null)
                return "";

            var sb = new StringBuilder();
            foreach (var key in settings.Keys) {
                sb.AppendLine(key + ": " + (settings[key] ?? EmptyValue));
            }

            return sb.ToString();
        }
Esempio n. 7
0
        public static SysSetting ParseSettings(string text) {
            var shellSettings = new SysSetting();
            if (String.IsNullOrEmpty(text))
                return shellSettings;

            var settings = new StringReader(text);
            string setting;
            while ((setting = settings.ReadLine()) != null) {
                if (string.IsNullOrWhiteSpace(setting)) continue;
                var separatorIndex = setting.IndexOf(Separator);
                if (separatorIndex == -1) {
                    continue;
                }
                string key = setting.Substring(0, separatorIndex).Trim();
                string value = setting.Substring(separatorIndex + 1).Trim();

                if (!value.Equals(EmptyValue, StringComparison.OrdinalIgnoreCase)) {
                    switch (key) {
                        case "SiteName":
                            shellSettings.SiteName = value;
                            break;
                        case "AdminUsername":
                            shellSettings.AdminUsername = value;
                            break;
                        case "DataProvider":
                            shellSettings.DataProvider = (DatabaseTypes)Enum.Parse(typeof(DatabaseTypes), value);
                            break;
                        case "State":
                            TenantState state;
                            shellSettings.State = Enum.TryParse(value, true, out state) ? state : TenantState.Uninitialized;
                            break;
                        case "DataConnectionString":
                            shellSettings.DataConnectionString = value;
                            break;
                        case "DataPrefix":
                            shellSettings.DataTablePrefix = value;
                            break;
                        case "EncryptionAlgorithm":
                            shellSettings.EncryptionAlgorithm = value;
                            break;
                        case "EncryptionKey":
                            shellSettings.EncryptionKey = value;
                            break;
                        case "HashAlgorithm":
                            shellSettings.HashAlgorithm = value;
                            break;
                        case "HashKey":
                            shellSettings.HashKey = value;
                            break;
                    }
                }
            }

            return shellSettings;
        }
Esempio n. 8
0
 public void Setup(SysSetting setting, Action<Configuration> alterCfg) {
     _cacheProvider.Expire("_sessionFactory");
     _cacheProvider.GetOrCreate("_sessionFactory", () => Initialize(setting, true));
     BuildSessionFactory();
 }