When you create a new application with the Security System enabled in the Solution Wizard, the end-user settings (model differences) are stored in the database using the ModelDifferenceDbStore storage, by default. This topic describes how to enable this feature in an existing application, along with how to store shared model differences (administrator's settings) in the database.
当您在解决方案向导中启用了安全系统创建新应用程序时,最终用户设置(模型差异)默认使用 ModelDifferenceDbStore 存储存储在数据库中。本主题介绍如何在现有应用程序中启用此功能,以及如何在数据库中存储共享模型差异(管理员设置)。
Edit the WinModule.cs (WinModule.vb) file located in the WinForms module project. In the overridden ModuleBase.Setup method, subscribe to the XafApplication.CreateCustomModelDifferenceStore and XafApplication.CreateCustomUserModelDifferenceStore events. In these event handlers, pass the ModelDifferenceDbStore instance to the e.Store parameter. Pass the ModelDifference type to the ModelDifferenceDbStore constructor. Here, the ModelDifference is a built-in persistent object from the DevExpress.Persistent.BaseImpl namespace (for XPO) or from the DevExpress.Persistent.BaseImpl.EF namespace (for Entity Framework) implementing the IModelDifference interface. Note the constructor's contextId last parameter (used to initialize the IModelDifference.ContextId property). In the WinForms module, set it to "Win" to distinguish model differences created for the same user in different platforms.
编辑位于 WinForms 模块项目中的WinModule.cs (WinModule.vb) 文件。在重写的 ModuleBase.安装程序方法中,订阅 Xaf 应用程序.创建自定义模型差异存储和 Xaf 应用程序.创建自定义用户模型差异存储事件。在这些事件处理程序中,将 ModelDifferenceDbStore 实例传递给 e.Store 参数。将模型差异类型传递给模型差异DbStore构造函数。此处,ModelDifference 是来自 DevExpress.持久性.baseImpl 命名空间(用于 XPO)或来自 DevExpress.持久性.Baseimpl.EF 命名空间(用于实体框架)实现 IModelDifference 接口的内置持久对象。请注意构造函数的上下文 Id 最后一个参数(用于初始化 IModelDifference.ContextId 属性)。在 WinForms 模块中,将其设置为"Win",以区分在不同平台上为同一用户创建的模型差异。
public sealed partial class MySolutionWindowsFormsModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Win"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Win"); e.Handled = true; } //... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
Analogously, edit the WebModule.cs (WebModule.vb) file located in the ASP.NET module project, but set the contextId parameter to "Web" instead of "Win".
类似地,编辑位于ASP.NET模块项目中的WebModule.cs (WebModule.vb) 文件,但将上下文 Id 参数设置为"Web"而不是"Win"。。
public sealed partial class MySolutionAspNetModule : ModuleBase { private void Application_CreateCustomModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), true, "Web"); e.Handled = true; } private void Application_CreateCustomUserModelDifferenceStore(Object sender, CreateCustomModelDifferenceStoreEventArgs e) { e.Store = new ModelDifferenceDbStore((XafApplication)sender, typeof(ModelDifference), false, "Web"); e.Handled = true; } // ... public override void Setup(XafApplication application) { base.Setup(application); application.CreateCustomModelDifferenceStore += Application_CreateCustomModelDifferenceStore; application.CreateCustomUserModelDifferenceStore += Application_CreateCustomUserModelDifferenceStore; } }
If you use the Entity Framework, register the ModelDifference and ModelDifferenceAspect entity types within your DbConext.
如果使用实体框架,请在 DbConext 中注册模型差异和模型差异方面实体类型。
using DevExpress.Persistent.BaseImpl.EF; // ... public class MyDbContext : DbContext { // ... public DbSet<ModelDifference> ModelDifferences { get; set; } public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; } }
I you use XPO, run the Module Designer and add the ModelDifference and ModelDifferenceAspect persistent types to the Exported Types section.
使用 XPO,运行模块设计器,并将模型差异和模型差异方面持久性类型添加到"导出类型"部分。
Ensure that all users have read/write access to ModelDifference and ModelDifferenceAspect types.
确保所有用户都具有对模型差异和模型差异方面类型的读/写访问权限。
public class Updater : ModuleUpdater { public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); PermissionPolicyRole defaultRole = ObjectSpace.FindObject<PermissionPolicyRole>(new BinaryOperator("Name", "Default")); if(defaultRole == null) { defaultRole = ObjectSpace.CreateObject<PermissionPolicyRole>(); defaultRole.Name = "Default"; defaultRole.AddObjectPermission<PermissionPolicyUser>(SecurityOperations.Read, "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddNavigationPermission(@"Application/NavigationItems/Items/Default/Items/MyDetails", SecurityPermissionState.Allow); defaultRole.AddMemberPermission<PermissionPolicyUser>(SecurityOperations.Write, "ChangePasswordOnFirstLogon", "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddMemberPermission<PermissionPolicyUser>(SecurityOperations.Write, "StoredPassword", "[Oid] = CurrentUserId()", SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<PermissionPolicyRole>(SecurityOperations.Read, SecurityPermissionState.Deny); defaultRole.AddTypePermissionsRecursively<ModelDifference>(SecurityOperations.ReadWriteAccess, SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<ModelDifferenceAspect>(SecurityOperations.ReadWriteAccess, SecurityPermissionState.Allow); // The 'Create' permission is additionally required if you use the Middle Tier Application Server defaultRole.AddTypePermissionsRecursively<ModelDifference>(SecurityOperations.Create, SecurityPermissionState.Allow); defaultRole.AddTypePermissionsRecursively<ModelDifferenceAspect>(SecurityOperations.Create, SecurityPermissionState.Allow); } sampleUser.Roles.Add(defaultRole); // ... ObjectSpace.CommitChanges(); } // ... }