From b1f74ead6db889fbe57491bb4ad6c63aa23f7e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 6 Nov 2024 22:34:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96Role=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E6=9D=83=E9=99=90=E8=AE=BE=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Areas/Admin/Controllers/RoleController.cs | 140 +------------ NewLife.Cube/Common/EntityController.cs | 36 +--- NewLife.Cube/Common/EntityController2.cs | 8 - .../Areas/Admin/Controllers/RoleController.cs | 185 +----------------- NewLife.CubeNC/Common/EntityController.cs | 6 +- 5 files changed, 22 insertions(+), 353 deletions(-) diff --git a/NewLife.Cube/Areas/Admin/Controllers/RoleController.cs b/NewLife.Cube/Areas/Admin/Controllers/RoleController.cs index 944f7a4d..ff8e2674 100644 --- a/NewLife.Cube/Areas/Admin/Controllers/RoleController.cs +++ b/NewLife.Cube/Areas/Admin/Controllers/RoleController.cs @@ -1,6 +1,5 @@ using System.ComponentModel; using NewLife.Web; -using XCode; using XCode.Membership; namespace NewLife.Cube.Areas.Admin.Controllers; @@ -39,28 +38,17 @@ protected override IEnumerable Search(Pager p) return Role.Search(p["dtStart"].ToDateTime(), p["dtEnd"].ToDateTime(), p["Q"], p); } - /// - /// 添加权限授权 - /// + /// 验证实体对象 /// + /// + /// /// - /// - public override async Task> Insert(Role entity) + protected override Boolean Valid(Role entity, DataObjectMethodType type, Boolean post) { - // 检测避免乱用Add/id - if (Factory.Unique.IsIdentity && entity[Factory.Unique.Name].ToInt() != 0) - throw new Exception("我们约定添加数据时路由id部分默认没有数据,以免模型绑定器错误识别!"); - - if (!Valid(entity, DataObjectMethodType.Insert, true)) return new ApiResponse(500, "验证失败!", null); + var rs = base.Valid(entity, type, post); - var rs = false; - var err = ""; - try + if (post && type is DataObjectMethodType.Insert or DataObjectMethodType.Update) { - entity.CreateTime = DateTime.Now; - entity.CreateIP = GetHostAddresses(); - entity.Enable = true; - // 保存权限项 var menus = XCode.Membership.Menu.Root.AllChilds; var dels = new List(); @@ -77,7 +65,6 @@ public override async Task> Insert(Role entity) { // 遍历所有权限子项 var any = false; - foreach (var pf in item.Permissions) { var has2 = GetBool("pf" + item.ID + "_" + pf.Key); @@ -93,127 +80,12 @@ public override async Task> Insert(Role entity) if (!any & !entity.Has(item.ID)) entity.Set(item.ID); } } - // 删除已经被放弃权限的项 foreach (var item in dels) { if (entity.Has(item)) entity.Permissions.Remove(item); } - - OnInsert(entity); - - //var fs = await SaveFiles(entity); - //if (fs.Count > 0) OnUpdate(entity); - if (LogOnChange) LogProvider.Provider.WriteLog("Insert", entity); - - rs = true; - } - catch (ArgumentException aex) - { - err = aex.Message; - ModelState.AddModelError(aex.ParamName, aex.Message); - } - catch (Exception ex) - { - err = ex.Message; - ModelState.AddModelError("", ex.Message); - } - - var msg = ""; - if (!rs) - { - WriteLog("Add", false, err); - - msg = SysConfig.Develop ? "添加失败!" + err : "添加失败!"; - //ViewBag.StatusMessage = msg; - - // 添加失败,ID清零,否则会显示保存按钮 - entity[Role.Meta.Unique.Name] = 0; - - return new ApiResponse(500, msg); - } - - //ViewBag.StatusMessage = "添加成功!"; - msg = "添加成功!"; - - //添加明细 - rs = AddDetailed(entity); - if (!rs) - { - WriteLog("Edit", false, err); - - msg = SysConfig.Develop ? "添加明细失败!" + err : "添加明细失败!"; - //ViewBag.StatusMessage = msg; - - // 添加失败,ID清零,否则会显示保存按钮 - entity[Role.Meta.Unique.Name] = 0; - - return new ApiResponse(500, msg); - } - - return new ApiResponse(0, msg, entity); - } - - /// 保存 - /// - /// - public override async Task> Update(Role entity) - { - // 保存权限项 - var menus = XCode.Membership.Menu.Root.AllChilds; - //var pfs = EnumHelper.GetDescriptions().Where(e => e.Key > PermissionFlags.None); - var dels = new List(); - // 遍历所有权限资源 - foreach (var item in menus) - { - // 是否授权该项 - var has = GetBool("p" + item.ID); - if (!has) - dels.Add(item.ID); - else - { - // 遍历所有权限子项 - var any = false; - foreach (var pf in item.Permissions) - { - var has2 = GetBool("pf" + item.ID + "_" + pf.Key); - - if (has2) - entity.Set(item.ID, (PermissionFlags)pf.Key); - else - entity.Reset(item.ID, (PermissionFlags)pf.Key); - any |= has2; - } - // 如果原来没有权限,这是首次授权,且右边没有勾选任何子项,则授权全部 - if (!any & !entity.Has(item.ID)) entity.Set(item.ID); - } } - // 删除已经被放弃权限的项 - foreach (var item in dels) - if (entity.Has(item)) entity.Permissions.Remove(item); - - return await base.Update(entity); - } - - /// - /// 获取客户端IP地址 - /// - /// - protected virtual String GetHostAddresses() => HttpContext.GetUserHost(); - - /// 添加实体主表对应的从表记录 - /// - /// - protected virtual Boolean AddDetailed(IEntity entity) => entity != null; - - /// 验证实体对象 - /// - /// - /// - /// - protected override Boolean Valid(Role entity, DataObjectMethodType type, Boolean post) - { - var rs = base.Valid(entity, type, post); // 清空缓存 if (post) Role.Meta.Session.ClearCache($"{type}-{entity}", true); diff --git a/NewLife.Cube/Common/EntityController.cs b/NewLife.Cube/Common/EntityController.cs index 032a61b0..c67a2ce7 100644 --- a/NewLife.Cube/Common/EntityController.cs +++ b/NewLife.Cube/Common/EntityController.cs @@ -1,20 +1,15 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; -using NewLife.Cube.Entity; using NewLife.Data; -using NewLife.Log; using NewLife.Reflection; using NewLife.Remoting; -using XCode; using XCode.Membership; namespace NewLife.Cube; /// 实体控制器基类 -/// 实体类型 -/// 数据模型,用于接口数据传输 -public partial class EntityController : ReadOnlyEntityController where TEntity : Entity, new() +public partial class EntityController { #region 默认Action /// 删除数据 @@ -52,7 +47,7 @@ public virtual ApiResponse Delete([Required] String id) public virtual async Task> Insert(TModel model) { // 实例化实体对象,然后拷贝 - if (model is TEntity entity) return await Insert(entity); + if (model is TEntity entity) return await ProcessInsert(entity); entity = Factory.Create(false) as TEntity; @@ -61,14 +56,14 @@ public virtual async Task> Insert(TModel model) else entity.Copy(model); - return await Insert(entity); + return await ProcessInsert(entity); } /// 添加数据 /// /// [NonAction] - public virtual async Task> Insert(TEntity entity) + protected virtual async Task> ProcessInsert(TEntity entity) { // 检测避免乱用Add/id if (Factory.Unique.IsIdentity && entity[Factory.Unique.Name].ToInt() != 0) @@ -114,7 +109,7 @@ public virtual async Task> Insert(TEntity entity) public virtual async Task> Update(TModel model) { // 实例化实体对象,然后拷贝 - if (model is TEntity entity) return await Update(entity); + if (model is TEntity entity) return await ProcessUpdate(entity); var uk = Factory.Unique; var key = model is IModel ext ? ext[uk.Name] : model.GetValue(uk.Name); @@ -127,14 +122,14 @@ public virtual async Task> Update(TModel model) else entity.Copy(model, false, uk.Name); - return await Update(entity); + return await ProcessUpdate(entity); } /// 更新数据 /// /// [NonAction] - public virtual async Task> Update(TEntity entity) + protected virtual async Task> ProcessUpdate(TEntity entity) { try { @@ -161,21 +156,4 @@ public virtual async Task> Update(TEntity entity) } } #endregion - - #region 实体操作重载 - /// 添加实体对象 - /// - /// - protected virtual Int32 OnInsert(TEntity entity) => entity.Insert(); - - /// 更新实体对象 - /// - /// - protected virtual Int32 OnUpdate(TEntity entity) => entity.Update(); - - /// 删除实体对象 - /// - /// - protected virtual Int32 OnDelete(TEntity entity) => entity.Delete(); - #endregion } \ No newline at end of file diff --git a/NewLife.Cube/Common/EntityController2.cs b/NewLife.Cube/Common/EntityController2.cs index d341ae4c..d66c4036 100644 --- a/NewLife.Cube/Common/EntityController2.cs +++ b/NewLife.Cube/Common/EntityController2.cs @@ -1,16 +1,8 @@ using System.ComponentModel; -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using NewLife.Cube.Common; using NewLife.Cube.Entity; -using NewLife.Cube.Extensions; using NewLife.Cube.ViewModels; -using NewLife.Data; using NewLife.Log; -using NewLife.Reflection; -using NewLife.Remoting; using NewLife.Serialization; -using NewLife.Web; using XCode; using XCode.Configuration; using XCode.Membership; diff --git a/NewLife.CubeNC/Areas/Admin/Controllers/RoleController.cs b/NewLife.CubeNC/Areas/Admin/Controllers/RoleController.cs index 8175e255..a6f1c9c6 100644 --- a/NewLife.CubeNC/Areas/Admin/Controllers/RoleController.cs +++ b/NewLife.CubeNC/Areas/Admin/Controllers/RoleController.cs @@ -1,9 +1,6 @@ using System.ComponentModel; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using NewLife.Cube.ViewModels; using NewLife.Web; -using XCode; using XCode.Membership; namespace NewLife.Cube.Areas.Admin.Controllers; @@ -52,34 +49,17 @@ protected override IEnumerable Search(Pager p) return Role.Search(p["dtStart"].ToDateTime(), p["dtEnd"].ToDateTime(), p["Q"], p); } - /// - /// 添加权限授权 - /// + /// 验证实体对象 /// + /// + /// /// - /// - public override async Task Add(Role entity) + protected override Boolean Valid(Role entity, DataObjectMethodType type, Boolean post) { - // 检测避免乱用Add/id - if (Factory.Unique.IsIdentity && entity[Factory.Unique.Name].ToInt() != 0) throw new Exception("我们约定添加数据时路由id部分默认没有数据,以免模型绑定器错误识别!"); - - if (!Valid(entity, DataObjectMethodType.Insert, true)) - { - ViewBag.StatusMessage = "验证失败!"; - ViewBag.Fields = OnGetFields(ViewKinds.AddForm, entity); - - return View("AddForm", entity); - } + var rs = base.Valid(entity, type, post); - var rs = false; - var err = ""; - try + if (post && type is DataObjectMethodType.Insert or DataObjectMethodType.Update) { - //SaveFiles(entity); - entity.CreateTime = DateTime.Now; - entity.CreateIP = GetHostAddresses(); - entity.Enable = true; - // 保存权限项 var menus = XCode.Membership.Menu.Root.AllChilds; var dels = new List(); @@ -89,12 +69,13 @@ public override async Task Add(Role entity) // 是否授权该项 var has = GetBool("p" + item.ID); if (!has) + { dels.Add(item.ID); + } else { // 遍历所有权限子项 var any = false; - foreach (var pf in item.Permissions) { var has2 = GetBool("pf" + item.ID + "_" + pf.Key); @@ -110,162 +91,12 @@ public override async Task Add(Role entity) if (!any & !entity.Has(item.ID)) entity.Set(item.ID); } } - // 删除已经被放弃权限的项 foreach (var item in dels) { if (entity.Has(item)) entity.Permissions.Remove(item); } - - OnInsert(entity); - - var fs = await SaveFiles(entity); - if (fs.Count > 0) OnUpdate(entity); - if (LogOnChange) LogProvider.Provider.WriteLog("Insert", entity); - - rs = true; - - //var masterCode = Entity.Meta.Factory.AllFields.Find(x => { - // return x.Field.ColumnName.Equals("QrCode"); - //}); - //if (masterCode != null) - //{ - // //更新二维码 - // entity.SetValue("QrCode", entity["QrCode"].ToString() + "?data={\"name\":\""+ entity["Name"]+ "\",\"code\":\""+entity["Code"]+"\"}"; - // entity.Update(); - //} - } - catch (ArgumentException aex) - { - err = aex.Message; - ModelState.AddModelError(aex.ParamName, aex.Message); - } - catch (Exception ex) - { - err = ex.Message; - ModelState.AddModelError("", ex.Message); - } - - if (!rs) - { - WriteLog("Add", false, err); - - ViewBag.StatusMessage = SysConfig.Develop ? ("添加失败!" + err) : "添加失败!"; - - // 添加失败,ID清零,否则会显示保存按钮 - entity[Role.Meta.Unique.Name] = 0; - - if (IsJsonRequest) return Json(500, ViewBag.StatusMessage); - - ViewBag.Fields = OnGetFields(ViewKinds.AddForm, entity); - - return View("AddForm", entity); - } - - ViewBag.StatusMessage = "添加成功!"; - - //添加明细 - rs = AddDetailed(entity); - if (!rs) - { - WriteLog("Edit", false, err); - - ViewBag.StatusMessage = SysConfig.Develop ? ("添加明细失败!" + err) : "添加明细失败!"; - - // 添加失败,ID清零,否则会显示保存按钮 - entity[Role.Meta.Unique.Name] = 0; - - if (IsJsonRequest) return Json(500, ViewBag.StatusMessage); - - ViewBag.Fields = OnGetFields(ViewKinds.AddForm, entity); - - return View("AddForm", entity); - } - - if (IsJsonRequest) return Json(0, ViewBag.StatusMessage); - - - var url = Session["Cube_Add_Referrer"] as String; - if (!url.IsNullOrEmpty()) - return Redirect(url); - else - // 新增完成跳到列表页,更新完成保持本页 - return RedirectToAction("Index"); - } - - /// 保存 - /// - /// - public override async Task Edit(Role entity) - { - // 保存权限项 - var menus = XCode.Membership.Menu.Root.AllChilds; - //var pfs = EnumHelper.GetDescriptions().Where(e => e.Key > PermissionFlags.None); - var dels = new List(); - // 遍历所有权限资源 - foreach (var item in menus) - { - // 是否授权该项 - var has = GetBool("p" + item.ID); - if (!has) - dels.Add(item.ID); - else - { - // 遍历所有权限子项 - var any = false; - foreach (var pf in item.Permissions) - { - var has2 = GetBool("pf" + item.ID + "_" + pf.Key); - - if (has2) - entity.Set(item.ID, (PermissionFlags)pf.Key); - else - entity.Reset(item.ID, (PermissionFlags)pf.Key); - any |= has2; - } - // 如果原来没有权限,这是首次授权,且右边没有勾选任何子项,则授权全部 - if (!any & !entity.Has(item.ID)) entity.Set(item.ID); - } } - // 删除已经被放弃权限的项 - foreach (var item in dels) - { - if (entity.Has(item)) entity.Permissions.Remove(item); - } - - return await base.Edit(entity); - } - - /// - /// 获取客户端IP地址 - /// - /// - protected virtual String GetHostAddresses() - { - return HttpContext.GetUserHost(); - } - - /// 添加实体主表对应的从表记录 - /// - /// - protected virtual bool AddDetailed(IEntity entity) - { - if (entity == null) - { - return false; - } - // TO DO - return true; - } - - /// 验证实体对象 - /// - /// - /// - /// - protected override Boolean Valid(Role entity, DataObjectMethodType type, Boolean post) - { - var rs = base.Valid(entity, type, post); // 清空缓存 if (post) Role.Meta.Session.ClearCache($"{type}-{entity}", true); diff --git a/NewLife.CubeNC/Common/EntityController.cs b/NewLife.CubeNC/Common/EntityController.cs index d64b0574..88d66a4c 100644 --- a/NewLife.CubeNC/Common/EntityController.cs +++ b/NewLife.CubeNC/Common/EntityController.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using NewLife.Cube.Common; -using NewLife.Cube.Entity; using NewLife.Cube.Extensions; using NewLife.Cube.ViewModels; using NewLife.Data; @@ -12,15 +11,12 @@ using NewLife.Serialization; using NewLife.Web; using XCode; -using XCode.Configuration; using XCode.Membership; namespace NewLife.Cube; /// 实体控制器基类 -/// -/// -public partial class EntityController : ReadOnlyEntityController where TEntity : Entity, new() +public partial class EntityController { #region 默认Action /// 删除