diff --git a/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml b/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml index 0c88a6fd..6946d8dd 100644 --- a/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml +++ b/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml @@ -2,6 +2,13 @@ var key = (Model as Guid?); if (key.HasValue) { - @ViewData.Get(key.Value) + if(ViewData.Keys.Contains(key.Value.ToString())) + { + @ViewData.Get(key.Value) + } + else{ + var list = (ViewData[ViewData.ModelMetadata.PropertyName + "SelectList"] as SelectList) ?? new SelectList(new List()); + @list.FirstOrDefault(o=>o.Value==key.Value.ToString()).Text + } } } \ No newline at end of file diff --git a/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml b/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml index d63c30dd..bcf3232d 100644 --- a/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml +++ b/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml @@ -2,6 +2,7 @@ @using System.Linq @{ var inputClass = "form-control"; + var action = ViewContext.RouteData.Values["action"].ToString(); var scope = ViewBag.ControllerScope as string; } @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit)) @@ -26,7 +27,8 @@
@Html.Label(prop.PropertyName, prop.GetDisplayName() + ":", new { @class = htmlClass })
- @if (metadata.Attributes.Attributes.Any(o => o.GetType() == typeof(System.ComponentModel.ReadOnlyAttribute))) + @if (metadata.Attributes.Attributes.Any(o => o.GetType() == typeof(System.ComponentModel.ReadOnlyAttribute)) + ||(action=="Edit"&&metadata.Attributes.Attributes.Any(o => o.GetType() == typeof(System.ComponentModel.DataAnnotations.ReadOnlyForEditAttribute)))) {
@Html.Hidden(prop.PropertyName) diff --git a/projects/Infrastructure/Web/ReadOnlyForEditAttribute.cs b/projects/Infrastructure/Web/ReadOnlyForEditAttribute.cs new file mode 100644 index 00000000..4a7a696b --- /dev/null +++ b/projects/Infrastructure/Web/ReadOnlyForEditAttribute.cs @@ -0,0 +1,6 @@ +namespace System.ComponentModel.DataAnnotations +{ + public class ReadOnlyForEditAttribute : Attribute + { + } +} diff --git a/projects/Platform/Application/Models/EditOrganUserModel.cs b/projects/Platform/Application/Models/EditOrganUserModel.cs index c81ecc79..56ec5676 100644 --- a/projects/Platform/Application/Models/EditOrganUserModel.cs +++ b/projects/Platform/Application/Models/EditOrganUserModel.cs @@ -10,12 +10,14 @@ namespace Platform.Application.Models public class EditOrganUserModel : EditModel { [Display(Name = "机构")] - [DataType("SelectList")] + [ReadOnlyForEdit] + [AjaxSelect("GetOrgan", "Ajax", "IoTCenter")] [Required] public Guid? OrganId { get; set; } [Display(Name = "用户")] - [DataType("SelectList")] + [ReadOnlyForEdit] + [AjaxSelect("GetUser", "Ajax", "IoTCenter")] [Required] public Guid? UserId { get; set; } diff --git a/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs b/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs index bc0f129c..b5f0a6ae 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs @@ -14,8 +14,6 @@ namespace Platform.Areas.IoTCenter.Controllers [Area("IoTCenter")] public class AjaxController : IoT.Shared.Areas.IoTCenter.Controlls.AjaxBaseController { - private readonly IRepository _roleRepo; - public AjaxController(IServiceProvider services, ILogger logger, IRepository categoryRepo, @@ -79,6 +77,26 @@ namespace Platform.Areas.IoTCenter.Controllers return new JsonResult(new SelectList(list, "Id", "Name", selected)); } + public JsonResult GetUser(Guid? parentId, Guid? selected = null, string search = null) + { + using var scope = this._services.CreateScope(); + var repo = scope.ServiceProvider.GetRequiredService>(); + + var list = repo.ReadOnlyTable() + .WhereIf(parentId.HasValue, o => o.OrganId == parentId.Value) + .WhereIf(!string.IsNullOrEmpty(search), o => o.User.UserName.Contains(search) + || o.User.NickName.Contains(search) + || o.User.Email.Contains(search) + || o.User.PhoneNumber.Contains(search) + || o.User.RealName.Contains(search) + || o.User.IdentityNumber.Contains(search)) + .WhereIf(selected.HasValue, o => o.UserId == selected.Value) + .Select(o => new { Id = o.UserId, Name = $"{o.User.UserName} {o.User.RealName}" }) + .Take(20) + .ToList(); + return new JsonResult(new SelectList(list, "Id", "Name", selected)); + } + public MultiSelectList GetOrganMultiSelectList(List selected) { if (selected == null) @@ -99,7 +117,9 @@ namespace Platform.Areas.IoTCenter.Controllers { selected = new List(); } - var list = this._roleRepo.ReadOnlyTable() + using var scope = this._services.CreateScope(); + var repo = scope.ServiceProvider.GetRequiredService>(); + var list = repo.ReadOnlyTable() .Select(o => new { o.Id, o.Name }) .ToList(); return new MultiSelectList(list, "Id", "Name", selected); diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs b/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganBuildingController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Organ/OrganBuildingController.cs diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs b/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganController.cs similarity index 99% rename from projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganController.cs index 2fd43b1e..1bf8d3b8 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganController.cs @@ -28,6 +28,5 @@ namespace Platform.Areas.UserCenter.Controllers return base.Query(model, query) .WhereIf(organId.HasValue, o => o.Id == organId.Value); } - } } diff --git a/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganUserController.cs b/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganUserController.cs new file mode 100644 index 00000000..2761c6e8 --- /dev/null +++ b/projects/Platform/Areas/UserCenter/Controllers/Organ/OrganOrganUserController.cs @@ -0,0 +1,32 @@ +using Infrastructure.Application; +using Infrastructure.Data; +using Infrastructure.Extensions; +using Infrastructure.Web.Mvc; +using IoT.Shared.Application.Domain.Entities; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Platform.Application.Models; +using Platform.Areas.IoTCenter.Controllers; +using System.Linq; + +namespace Platform.Areas.UserCenter.Controllers +{ + [Authorize] + [ApiController] + [Route("UserCenter/[controller]/[action]")] + [Area("UserCenter")] + [ControllerScope(ControllerScopeType.Organ)] + public class OrganOrganUserController : OrganUserController + { + public OrganOrganUserController(IRepository repo, AjaxController ajax) : base(repo, ajax) + { + } + + public override IQueryable Query(PagedListModel model, IQueryable query) + { + var organId = User.GetOrganId(); + return base.Query(model, query) + .WhereIf(organId.HasValue, o => o.Organ.Id == organId.Value); + } + } +} diff --git a/projects/Platform/Areas/UserCenter/Controllers/AreaController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/AreaController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/AreaController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/AreaController.cs diff --git a/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/BuildingController.cs similarity index 98% rename from projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/BuildingController.cs index 71358ea7..59ebb087 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/Platform/BuildingController.cs @@ -67,7 +67,7 @@ namespace Platform.Areas.UserCenter.Controllers model.IoTGateways = entity.BuildingIoTGateways.Select(o => o.IoTGatewayId).ToList(); } this.ViewData.MultiSelectList(o => model.IoTGateways, () => this._ajax.GetNodeMultiSelectList(model.IoTGateways)); - ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); + this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); if (model.OrganId.HasValue) { ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganBuilding(model.OrganId.Value, model.ParentId, model.Id).SelectList()); diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/OrganController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/OrganController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/OrganController.cs diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/OrganUserController.cs similarity index 60% rename from projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/OrganUserController.cs index 402003de..b54731c8 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/Platform/OrganUserController.cs @@ -1,4 +1,5 @@ -using Infrastructure.Data; +using Infrastructure.Application; +using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; @@ -31,18 +32,29 @@ namespace Platform.Areas.UserCenter.Controllers .Include(o => o.Organ) .Include(o => o.User); } - + public override IQueryable Query(PagedListModel model, IQueryable query) + { + return query + .WhereIf(model.Query.OrganId.HasValue, o => o.OrganId == model.Query.OrganId.Value) + .WhereIf(model.Query.UserId.HasValue, o => o.UserId == model.Query.UserId.Value) + .WhereIf(model.Query.IsDefault.HasValue, o => o.IsDefault == model.Query.IsDefault.Value) + .WhereIf(model.Query.Type.HasValue, o => o.Type == model.Query.Type.Value) + .WhereIf(!string.IsNullOrEmpty(model.Query.CustomType), o => o.CustomType.Contains(model.Query.CustomType)); + } public override void ToDisplayModel(OrganUser entity, EditOrganUserModel model) { ViewData.Add(entity.OrganId, entity.Organ.Name); - ViewData.Add(entity.UserId, $"{entity.User.UserName}({entity.User.NickName})"); + ViewData.Add(entity.UserId, $"{entity.User.UserName} {entity.User.NickName} {entity.User.RealName}"); } public override void ToEditModel(OrganUser entity, EditOrganUserModel model) { base.ToEditModel(entity, model); this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); - //this.ViewData.SelectList(o => model.UserId, () => this._ajax.GetUser(model.UserId).SelectList()); + if (model.OrganId.HasValue) + { + ViewData.SelectList(o => model.UserId, () => this._ajax.GetUser(model.OrganId.Value, model.UserId).SelectList()); + } } } } diff --git a/projects/Platform/Areas/UserCenter/Controllers/PermissionController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/PermissionController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/PermissionController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/PermissionController.cs diff --git a/projects/Platform/Areas/UserCenter/Controllers/RoleController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/RoleController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/RoleController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/RoleController.cs diff --git a/projects/Platform/Areas/UserCenter/Controllers/UserController.cs b/projects/Platform/Areas/UserCenter/Controllers/Platform/UserController.cs similarity index 100% rename from projects/Platform/Areas/UserCenter/Controllers/UserController.cs rename to projects/Platform/Areas/UserCenter/Controllers/Platform/UserController.cs diff --git a/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml b/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml index da6c7b5d..9e33ba97 100644 --- a/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml +++ b/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml @@ -43,6 +43,10 @@ { } + @if (HasPermission("Read-Organ-OrganUser")) + { + + } @if (User.Claims.Any(o => o.Value.Contains("-User-"))) { diff --git a/projects/Platform/Areas/UserCenter/Views/Shared/_Script.cshtml b/projects/Platform/Areas/UserCenter/Views/Shared/_Script.cshtml index 4317ac1d..06546104 100644 --- a/projects/Platform/Areas/UserCenter/Views/Shared/_Script.cshtml +++ b/projects/Platform/Areas/UserCenter/Views/Shared/_Script.cshtml @@ -3,6 +3,12 @@ $('#OrganId').change(function () { var id = $(this).find(':selected').val(); update('@Url.Action("GetOrganBuilding", "Ajax",new{area="IoTCenter"})', id, "#ParentId"); + update('@Url.Action("GetUser", "Ajax",new{area="IoTCenter"})', id, "#UserId"); + });; + $('#Query_OrganId').change(function () { + var id = $(this).find(':selected').val(); + update('@Url.Action("GetOrganBuilding", "Ajax",new{area="IoTCenter"})', id, "#Query_ParentId"); + update('@Url.Action("GetUser", "Ajax",new{area="IoTCenter"})', id, "#Query_UserId"); });; }); \ No newline at end of file diff --git a/projects/Platform/DbConfig.cs b/projects/Platform/DbConfig.cs index 5adffac0..e5211d03 100644 --- a/projects/Platform/DbConfig.cs +++ b/projects/Platform/DbConfig.cs @@ -198,17 +198,22 @@ namespace Platform db.SaveChanges(); - var saRole = new Role { Name = "超级管理员", IsReadOnly = true }; + var superRole = new Role { Name = "超级管理员", IsReadOnly = true }; var adminRole = new Role { Name = "管理员", IsReadOnly = true }; + var organRole = new Role { Name = "机构管理员", IsReadOnly = true }; var skips = new string[] { "添加Api", "修改Api", "添加分类", "修改分类", "添加参数", "修改参数", "添加产品", "修改产品", "添加节点", "添加权限", "修改权限", "添加权限分类", "修改权限分类", "添加设备" }; foreach (var item in db.Set()) { - saRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true }); + superRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true }); if (!item.Name.Contains("删除") && !skips.Contains(item.Name)) { adminRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true }); + if (item.Number.Contains("-Organ-")) + { + organRole.RolePermissions.Add(new RolePermission { Permission = item, IsReadOnly = true }); + } } } @@ -222,10 +227,16 @@ namespace Platform Email = "super@test.com", EmailConfirmed = true, NickName = "超级管理员", - UserRoles = new List { new UserRole { Role = saRole } }, + UserRoles = new List { new UserRole { Role = superRole } }, OrganUsers = new List { - new OrganUser { Organ = db.Set().FirstOrDefault(o => o.Number == "jiaoyuju") }, - new OrganUser { Organ = db.Set().FirstOrDefault(o => o.Number == "xuexiao") } + new OrganUser { + Type= OrganUserType.Other,CustomType="维护人员", + Organ = db.Set().FirstOrDefault(o => o.Number == "jiaoyuju") + }, + new OrganUser { + Type= OrganUserType.Other,CustomType="维护人员", + Organ = db.Set().FirstOrDefault(o => o.Number == "xuexiao") + } } }); db.Set().Add(new User @@ -238,7 +249,29 @@ namespace Platform EmailConfirmed = true, NickName = "管理员", UserRoles = new List { new UserRole { Role = adminRole } }, - OrganUsers = new List { new OrganUser { Organ = db.Set().FirstOrDefault(o => o.Number == "xuexiao") } } + OrganUsers = new List { + new OrganUser { + Type = OrganUserType.Other, CustomType = "维护人员", + Organ = db.Set().FirstOrDefault(o => o.Number == "xuexiao") + } + } + }); + db.Set().Add(new User + { + UserName = "organ", + SecurityStamp = securityStam, + PasswordHash = _encryptionService.CreatePasswordHash("123456", securityStam), + PasswordConfirmed = true, + Email = "organ@test.com", + EmailConfirmed = true, + NickName = "机构管理员", + UserRoles = new List { new UserRole { Role = organRole } }, + OrganUsers = new List { + new OrganUser { + Type = OrganUserType.Other, CustomType = "维护人员", + Organ = db.Set().FirstOrDefault(o => o.Number == "xuexiao") + } + } }); db.SaveChanges(); //