diff --git a/projects/Infrastructure/Extensions/ClaimsPrincipalExtensions.cs b/projects/Infrastructure/Extensions/ClaimsPrincipalExtensions.cs index e25fcd5e..8e5bf6e5 100644 --- a/projects/Infrastructure/Extensions/ClaimsPrincipalExtensions.cs +++ b/projects/Infrastructure/Extensions/ClaimsPrincipalExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Security.Claims; namespace Infrastructure.Extensions @@ -15,9 +16,10 @@ namespace Infrastructure.Extensions return role == null ? false : user.IsInRole(role.ToString()); } - public static string GetUserData(this ClaimsPrincipal user) + public static Guid? GetOrganId(this ClaimsPrincipal user) { - return user.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value; + var value = user.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value; + return Guid.TryParse(value, out Guid id) ? id : null; } } } diff --git a/projects/Infrastructure/Extensions/HttpContextExtensions.cs b/projects/Infrastructure/Extensions/HttpContextExtensions.cs index 2ff27972..c7594a7e 100644 --- a/projects/Infrastructure/Extensions/HttpContextExtensions.cs +++ b/projects/Infrastructure/Extensions/HttpContextExtensions.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; @@ -21,18 +21,18 @@ namespace Infrastructure.Extensions return $"jwt{httpContext.RequestServices.GetService()["jwt:cookie"]}"; } - public static void JwtSignIn(this HttpContext httpContext, string userName, bool rememberMe,string organNumber = null) + public static void JwtSignIn(this HttpContext httpContext, string userName, bool rememberMe, string organId = null) { if (httpContext is null) { throw new ArgumentNullException(nameof(httpContext)); } var claims = new List { - new Claim(ClaimTypes.Name, userName), + new Claim(ClaimTypes.Name, userName), }; - if (!string.IsNullOrEmpty(organNumber)) + if (!string.IsNullOrEmpty(organId)) { - claims.Add(new Claim(ClaimTypes.UserData, organNumber)); + claims.Add(new Claim(ClaimTypes.UserData, organId)); } var token = httpContext.CreateJwtToken(claims, DateTime.Now.AddYears(1)); var cookieOptions = new CookieOptions @@ -83,4 +83,4 @@ namespace Infrastructure.Extensions return new JwtSecurityTokenHandler().ReadJwtToken(token); } } -} \ No newline at end of file +} diff --git a/projects/Infrastructure/Extensions/JsonResultExtensions.cs b/projects/Infrastructure/Extensions/JsonResultExtensions.cs new file mode 100644 index 00000000..137880b6 --- /dev/null +++ b/projects/Infrastructure/Extensions/JsonResultExtensions.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace Infrastructure.Extensions +{ + public static class JsonResultExtensions + { + public static SelectList SelectList(this JsonResult result) + { + return result.Value as SelectList; + } + } +} diff --git a/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml b/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml new file mode 100644 index 00000000..0c88a6fd --- /dev/null +++ b/projects/Infrastructure/Views/Shared/DisplayTemplates/AjaxSelect.cshtml @@ -0,0 +1,7 @@ +@{ + var key = (Model as Guid?); + if (key.HasValue) + { + @ViewData.Get(key.Value) + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Views/Shared/DisplayTemplates/Object.cshtml b/projects/Infrastructure/Views/Shared/DisplayTemplates/Object.cshtml index ab744cb9..71641966 100644 --- a/projects/Infrastructure/Views/Shared/DisplayTemplates/Object.cshtml +++ b/projects/Infrastructure/Views/Shared/DisplayTemplates/Object.cshtml @@ -1,8 +1,15 @@ +@{ + var scope = ViewBag.ControllerScope as string; +}
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !pm.HideSurroundingHtml)) { + if(scope=="Organ"&&prop.PropertyName=="OrganId") + { + continue; + } var uihit = prop.DataTypeName ?? prop.TemplateHint;
@Html.Label(prop.PropertyName, prop.GetDisplayName() + ":", new { @class = "col-sm-2 col-form-label" }) diff --git a/projects/Infrastructure/Views/Shared/Edit.cshtml b/projects/Infrastructure/Views/Shared/Edit.cshtml index 78cdb526..ccd4d8eb 100644 --- a/projects/Infrastructure/Views/Shared/Edit.cshtml +++ b/projects/Infrastructure/Views/Shared/Edit.cshtml @@ -1,5 +1,5 @@ @{ - HtmlTitle = "编辑" + ViewContext.ViewData.ModelMetadata.ModelType.GetDisplayName(); + HtmlTitle = "编辑" + ViewContext.ViewData.ModelMetadata.ModelType.GetDisplayName(); } @Html.EditorForModel() @section scripts{ diff --git a/projects/Infrastructure/Views/Shared/EditorTemplates/AjaxSelect.cshtml b/projects/Infrastructure/Views/Shared/EditorTemplates/AjaxSelect.cshtml new file mode 100644 index 00000000..a3a7c045 --- /dev/null +++ b/projects/Infrastructure/Views/Shared/EditorTemplates/AjaxSelect.cshtml @@ -0,0 +1,5 @@ +@{ + var htmlClass = "form-control select2bs4 search"; + var list = (ViewData[ViewData.ModelMetadata.PropertyName + "SelectList"] as SelectList) ?? new SelectList(new List()); + @Html.DropDownList("", list, "请选择", new { @class = htmlClass }) +} \ 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 9c8bced7..d63c30dd 100644 --- a/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml +++ b/projects/Infrastructure/Views/Shared/EditorTemplates/Input.cshtml @@ -2,11 +2,17 @@ @using System.Linq @{ var inputClass = "form-control"; + var scope = ViewBag.ControllerScope as string; } @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit)) { var metadata = prop as Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata; object propValue = Model == null ? (prop.ModelType.IsValueType ? Activator.CreateInstance(prop.ModelType) : null) : ViewData.ModelMetadata.ModelType.GetProperty(prop.PropertyName).GetValue(ViewData.Model); + if(scope=="Organ"&&prop.PropertyName=="OrganId") + { + @Html.Hidden(prop.PropertyName) + continue; + } if (prop.HideSurroundingHtml) { @Html.Hidden(prop.PropertyName) diff --git a/projects/Infrastructure/Views/Shared/EditorTemplates/SelectList.cshtml b/projects/Infrastructure/Views/Shared/EditorTemplates/SelectList.cshtml index 658a4172..ed4aa104 100644 --- a/projects/Infrastructure/Views/Shared/EditorTemplates/SelectList.cshtml +++ b/projects/Infrastructure/Views/Shared/EditorTemplates/SelectList.cshtml @@ -1,6 +1,6 @@ @{ var list = new SelectList(new List()); - var htmlClass = "form-control select2bs4 search"; + var htmlClass = "form-control search"; if (!string.IsNullOrEmpty(ViewData.ModelMetadata.TemplateHint)) { htmlClass += " " + ViewData.ModelMetadata.TemplateHint; diff --git a/projects/Infrastructure/Views/Shared/Index.cshtml b/projects/Infrastructure/Views/Shared/Index.cshtml index f1e0924b..95f12ecf 100644 --- a/projects/Infrastructure/Views/Shared/Index.cshtml +++ b/projects/Infrastructure/Views/Shared/Index.cshtml @@ -57,7 +57,7 @@ }
- +
@@ -67,6 +67,10 @@ { continue; } + if(scope=="Organ"&&item.Name=="OrganId") + { + continue; + } } @if (User.IsInRole($"Read-{scope}-{entityType.Name}")) diff --git a/projects/Infrastructure/Views/Shared/_Index_Row.cshtml b/projects/Infrastructure/Views/Shared/_Index_Row.cshtml index 50454838..5e5f296a 100644 --- a/projects/Infrastructure/Views/Shared/_Index_Row.cshtml +++ b/projects/Infrastructure/Views/Shared/_Index_Row.cshtml @@ -1,6 +1,12 @@ -@{var props = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !pm.HideSurroundingHtml && !pm.IsComplexType && !pm.IsCollectionType && pm.PropertyName != "Id").ToList(); +@{ + var scope = ViewBag.ControllerScope as string; + var props = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !pm.HideSurroundingHtml && !pm.IsComplexType && !pm.IsCollectionType && pm.PropertyName != "Id").ToList(); foreach (var prop in props) { + if(scope=="Organ"&&prop.PropertyName=="OrganId") + { + continue; + } var templateName = prop.DataTypeName ?? prop.TemplateHint ?? (prop.ModelType == typeof(string) ? typeof(string).Name : null);
行号@item.GetDisplayName() @if (prop.UnderlyingOrModelType == typeof(bool)) diff --git a/projects/Infrastructure/Web/AjaxSelectAttribute.cs b/projects/Infrastructure/Web/AjaxSelectAttribute.cs new file mode 100644 index 00000000..130818b4 --- /dev/null +++ b/projects/Infrastructure/Web/AjaxSelectAttribute.cs @@ -0,0 +1,53 @@ +using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; +using Microsoft.AspNetCore.Mvc.Routing; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using System.Collections.Generic; + +namespace System.ComponentModel.DataAnnotations +{ + public class AjaxSelectAttribute : UIHintAttribute, IClientModelValidator + { + protected string RouteName { get; set; } + protected RouteValueDictionary RouteData { get; } = new RouteValueDictionary(); + + public AjaxSelectAttribute(string action, string controller, string areaName = null) : base("AjaxSelect") + { + if (action != null) + { + RouteData["action"] = action; + } + if (controller != null) + { + RouteData["controller"] = controller; + } + if (controller != null) + { + RouteData["area"] = areaName; + } + } + + public void AddValidation(ClientModelValidationContext context) + { + MergeAttribute(context.Attributes, "data-ajax-url", GetUrl(context)); + } + + private string GetUrl(ClientModelValidationContext context) + { + return context.ActionContext.HttpContext.RequestServices.GetRequiredService() + .GetUrlHelper(context.ActionContext).RouteUrl(new UrlRouteContext + { + RouteName = RouteName, + Values = RouteData + }); + } + + private static void MergeAttribute(IDictionary attributes, string key, string value) + { + if (!attributes.ContainsKey(key)) + { + attributes.Add(key, value); + } + } + } +} diff --git a/projects/Infrastructure/Web/IUserService.cs b/projects/Infrastructure/Web/IUserService.cs index 0463f6b6..584979a6 100644 --- a/projects/Infrastructure/Web/IUserService.cs +++ b/projects/Infrastructure/Web/IUserService.cs @@ -5,8 +5,8 @@ namespace Infrastructure.Web { public interface IUserService { - List GetOrganNumbers(string userName); + List GetOrgans(string userName); List GetRoles(string userName); } -} \ No newline at end of file +} diff --git a/projects/Infrastructure/Web/JwtTokenValidator.cs b/projects/Infrastructure/Web/JwtTokenValidator.cs index 70721405..807b9182 100644 --- a/projects/Infrastructure/Web/JwtTokenValidator.cs +++ b/projects/Infrastructure/Web/JwtTokenValidator.cs @@ -3,8 +3,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using System; using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; using System.Linq; +using System.Security.Claims; namespace Infrastructure.Web { @@ -33,11 +33,11 @@ namespace Infrastructure.Web using var scope = this._serviceProvider.CreateScope(); var userService = scope.ServiceProvider.GetService(); - var organNumber = claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value; - if (!string.IsNullOrEmpty(organNumber)) + var organId = claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value; + if (!string.IsNullOrEmpty(organId)) { - var organNumbers = userService.GetOrganNumbers(userName); - if (!organNumbers.Any(o => o == organNumber)) + var organNumbers = userService.GetOrgans(userName); + if (!organNumbers.Any(o => o == organId)) { validatedToken = null; return null; @@ -57,4 +57,4 @@ namespace Infrastructure.Web return null; } } -} \ No newline at end of file +} diff --git a/projects/Infrastructure/Web/Mvc/CrudController.cs b/projects/Infrastructure/Web/Mvc/CrudController.cs index f9967876..1a8d1805 100644 --- a/projects/Infrastructure/Web/Mvc/CrudController.cs +++ b/projects/Infrastructure/Web/Mvc/CrudController.cs @@ -16,9 +16,12 @@ namespace Infrastructure.Web.Mvc { protected IRepository Repo { get; } + private readonly ControllerScopeType? _scope; + public CrudController(IRepository repo) { this.Repo = repo; + this._scope = (this.GetType().GetCustomAttributes(false).FirstOrDefault(o => o.GetType() == typeof(ControllerScopeAttribute)) as ControllerScopeAttribute)?.Scope; } [HttpGet] @@ -253,6 +256,14 @@ namespace Infrastructure.Web.Mvc [ApiExplorerSettings(IgnoreApi = true)] public virtual void ToEditModel(TEntity entity, TEditModel model) { + if (_scope == ControllerScopeType.Organ) + { + var organId = User.GetOrganId(); + if (organId.HasValue) + { + typeof(TEditModel).GetProperty("OrganId").SetValue(model, organId); + } + } } [ApiExplorerSettings(IgnoreApi = true)] @@ -272,29 +283,28 @@ namespace Infrastructure.Web.Mvc { throw new ArgumentNullException(nameof(context)); } - var scope = (this.GetType().GetCustomAttributes(false).FirstOrDefault(o => o.GetType() == typeof(ControllerScopeAttribute)) as ControllerScopeAttribute)?.Scope; - var type = scope.ToString(); + var type = _scope.ToString(); var name = (context.ActionDescriptor as ControllerActionDescriptor).ActionName; var permission = string.Empty; if (name == "Index") { - permission = $"Read-{scope}-{typeof(TEntity).Name}"; + permission = $"Read-{_scope}-{typeof(TEntity).Name}"; } else if (name == "Details") { - permission = $"Read-{scope}-{typeof(TEntity).Name}"; + permission = $"Read-{_scope}-{typeof(TEntity).Name}"; } else if (name == "Add") { - permission = $"Add-{scope}-{typeof(TEntity).Name}"; + permission = $"Add-{_scope}-{typeof(TEntity).Name}"; } else if (name == "Edit" || name == "Remove" || name == "Restore") { - permission = $"Edit-{scope}-{typeof(TEntity).Name}"; + permission = $"Edit-{_scope}-{typeof(TEntity).Name}"; } else if (name == "Delete") { - permission = $"Delete-{scope}-{typeof(TEntity).Name}"; + permission = $"Delete-{_scope}-{typeof(TEntity).Name}"; } if (!string.IsNullOrEmpty(permission)) { diff --git a/projects/Platform/Application/Domain/LiveRecord.cs b/projects/IoT.Shared/Application/Domain/Entities/IoT/LiveRecord.cs similarity index 87% rename from projects/Platform/Application/Domain/LiveRecord.cs rename to projects/IoT.Shared/Application/Domain/Entities/IoT/LiveRecord.cs index 7111bb38..7e292dd2 100644 --- a/projects/Platform/Application/Domain/LiveRecord.cs +++ b/projects/IoT.Shared/Application/Domain/Entities/IoT/LiveRecord.cs @@ -1,7 +1,7 @@ using Infrastructure.Domain; using System.ComponentModel.DataAnnotations; -namespace Platform.Application.Domain +namespace IoT.Shared.Application.Domain.Entities { [Display(Name = "回放")] public class LiveRecord : BaseEntity @@ -11,4 +11,4 @@ namespace Platform.Application.Domain public string Name { get; set; } public string Value { get; set; } } -} \ No newline at end of file +} diff --git a/projects/IoT.Shared/Areas/IoTCenter/Controlls/AjaxController.cs b/projects/IoT.Shared/Areas/IoTCenter/Controlls/AjaxController.cs index 06df7116..4841e9ea 100644 --- a/projects/IoT.Shared/Areas/IoTCenter/Controlls/AjaxController.cs +++ b/projects/IoT.Shared/Areas/IoTCenter/Controlls/AjaxController.cs @@ -86,16 +86,18 @@ namespace IoT.Shared.Areas.IoTCenter.Controlls #region Organ - public SelectList GetOrganSelectList(Guid? selected = null, Guid? currentId = null) + public JsonResult GetOrgan(Guid? selected = null, Guid? currentId = null, string search = null) { var list = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) + .WhereIf(!string.IsNullOrEmpty(search), o => o.Name.Contains(search) || o.Number.Contains(search)) + .WhereIf(selected.HasValue, o => o.Id == selected.Value) .WhereIf(currentId.HasValue, o => o.Id != currentId.Value) .OrderBy(o => o.Parent) .ThenBy(o => o.DisplayOrder) .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) .ToList(); - return new SelectList(list, "Id", "Name", selected); + return new JsonResult(new SelectList(list, "Id", "Name", selected)); } #endregion Organ diff --git a/projects/IoT.Shared/wwwroot/js/site.js b/projects/IoT.Shared/wwwroot/js/site.js index 657bd6f8..35e8690f 100644 --- a/projects/IoT.Shared/wwwroot/js/site.js +++ b/projects/IoT.Shared/wwwroot/js/site.js @@ -149,10 +149,29 @@ function InitControls() { $(this).treeMultiselect({ enableSelectAll: true, searchable: true, selectAllText: "全选", unselectAllText: "反选" }); }); // - //$('select.search').select2({ - // theme: "bootstrap4", - // language: "zh-CN" - //}); + $('select.select2bs4').select2({ + theme: "bootstrap4", + language: "zh-CN", + placeholder: '请选择', + allowClear: true, + minimumInputLength: 2, + ajax: { + url: $(this).attr('data-ajax-url'), + data: function (params) { + var query = { + search: params.term, + type: 'public' + } + return query; + }, + dataType: 'json', + processResults: function (data) { + return { + results: Enumerable.from(data).select(function (o) { return { id: o.value, text: o.text }; }).toArray() + }; + } + } + }); // $('input.cron').each(function () { var cron = $(this).jqCron({ diff --git a/projects/IoTNode/UserService.cs b/projects/IoTNode/UserService.cs index 0caa52f3..084151f0 100644 --- a/projects/IoTNode/UserService.cs +++ b/projects/IoTNode/UserService.cs @@ -16,7 +16,7 @@ namespace IoTNode this._userRepo = userRepo; } - public List GetOrganNumbers(string userName) + public List GetOrgans(string userName) { throw new System.NotImplementedException(); } diff --git a/projects/Platform/Api/SrsController.cs b/projects/Platform/Api/SrsController.cs index 86822f14..2d201fa2 100644 --- a/projects/Platform/Api/SrsController.cs +++ b/projects/Platform/Api/SrsController.cs @@ -3,7 +3,6 @@ using IoT.Shared.Application.Domain.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; -using Platform.Application.Domain; using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/projects/Platform/Application/Models/EditAreaModel.cs b/projects/Platform/Application/Models/EditAreaModel.cs index a343b235..94e90ca9 100644 --- a/projects/Platform/Application/Models/EditAreaModel.cs +++ b/projects/Platform/Application/Models/EditAreaModel.cs @@ -28,7 +28,7 @@ namespace Platform.Application.Models public string CustomType { get; set; } [Display(Name = "上级")] - [DataType("SelectList")] + [AjaxSelect("GetArea", "Ajax", "IoTCenter")] public Guid? ParentId { get; set; } [Display(Name = "序号")] diff --git a/projects/Platform/Application/Models/EditOrganModel.cs b/projects/Platform/Application/Models/EditOrganModel.cs index f3872299..db03761f 100644 --- a/projects/Platform/Application/Models/EditOrganModel.cs +++ b/projects/Platform/Application/Models/EditOrganModel.cs @@ -9,11 +9,11 @@ namespace Platform.Application.Models [Display(Name = "机构")] public class EditOrganModel : EditModel { - [DataType("SelectList")] + [AjaxSelect("GetArea", "Ajax", "IoTCenter")] [Display(Name = "所属区域")] public Guid? AreaId { get; set; } - [DataType("SelectList")] + [AjaxSelect("GetOrgan", "Ajax", "IoTCenter")] [Display(Name = "上级机构")] public Guid? ParentId { get; set; } diff --git a/projects/Platform/Application/Models/EditOrganUserModel.cs b/projects/Platform/Application/Models/EditOrganUserModel.cs new file mode 100644 index 00000000..c81ecc79 --- /dev/null +++ b/projects/Platform/Application/Models/EditOrganUserModel.cs @@ -0,0 +1,34 @@ +using Infrastructure.Application; +using IoT.Shared.Application.Domain.Entities; +using System; +using System.ComponentModel.DataAnnotations; +using UoN.ExpressiveAnnotations.NetCore.Attributes; + +namespace Platform.Application.Models +{ + [Display(Name = "机构用户")] + public class EditOrganUserModel : EditModel + { + [Display(Name = "机构")] + [DataType("SelectList")] + [Required] + public Guid? OrganId { get; set; } + + [Display(Name = "用户")] + [DataType("SelectList")] + [Required] + public Guid? UserId { get; set; } + + [Display(Name = "类型")] + [DataType("SelectList")] + [Required] + public OrganUserType? Type { get; set; } + + [Display(Name = "自定义类型")] + [RequiredIf("Type==1000", ErrorMessage = "类型为自定时必须填写此字段")] + public string CustomType { get; set; } + + [Display(Name = "默认机构")] + public bool? IsDefault { get; set; } + } +} diff --git a/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs b/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs index d853112e..7db32b65 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/AjaxController.cs @@ -55,16 +55,19 @@ namespace Platform.Areas.IoTCenter.Controllers this._roleRepo = roleRepo; } - public SelectList GetAreaSelectList(Guid? selected = null, Guid? currentId = null) + public JsonResult GetArea(Guid? selected = null, Guid? currentId = null, string search = null) { var list = this._areaRepo.ReadOnlyTable() .Where(o => o.ParentId != null) + .WhereIf(selected.HasValue, o => o.Id == selected.Value) + .WhereIf(!string.IsNullOrEmpty(search), o => o.Name.Contains(search) || o.Number.Contains(search)) .WhereIf(currentId.HasValue, o => o.Id != currentId.Value) .OrderBy(o => o.Parent) .ThenBy(o => o.DisplayOrder) - .Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" }) + .Select(o => new { o.Id, Name = $"{o.Name}:{o.Number}" }) + .Take(20) .ToList(); - return new SelectList(list, "Id", "Name", selected); + return new JsonResult(new SelectList(list, "Id", "Name", selected)); } public JsonResult GetOrganBuilding(Guid parentId, Guid? selected = null, Guid? currentId = null) diff --git a/projects/Platform/Areas/IoTCenter/Controllers/LiveRecordController.cs b/projects/Platform/Areas/IoTCenter/Controllers/LiveRecordController.cs index 7e32e459..389d21ab 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/LiveRecordController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/LiveRecordController.cs @@ -2,11 +2,11 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; -using IoT.Shared.Areas.IoTCenter.Controlls; +using IoT.Shared.Application.Domain.Entities; using IoT.Shared.Application.Models; +using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Platform.Application.Domain; using System.Linq; namespace Platform.Areas.IoTCenter.Controllers diff --git a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneCommandController.cs b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneCommandController.cs index 61cd7b6b..684ffbf6 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneCommandController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneCommandController.cs @@ -2,8 +2,8 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; -using IoT.Shared.Areas.IoTCenter.Controlls; using IoT.Shared.Application.Domain.Entities; +using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -43,7 +43,7 @@ namespace Platform.Areas.IoTCenter.Controllers public override void ToEditModel(OrganIoTSceneIoTCommand entity, EditOrganSceneCommandModel model) { this.ToDisplayModel(entity, model); - ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrganSelectList(model.OrganId)); + ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.OrganSceneId, () => this._ajax.GetOrganSceneSelectList(model.OrganId.Value, model.OrganSceneId), model.OrganId.HasValue); ViewData.SelectList(o => model.NodeId, () => this._ajax.GetOrganNodeSelectList(model.OrganId.Value, model.NodeId), model.OrganId.HasValue); ViewData.SelectList(o => model.CommandId, () => this._ajax.GetCommandSelectList(model.NodeId.Value, model.CommandId), model.NodeId.HasValue); diff --git a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneController.cs b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneController.cs index 2d4ba65b..79c7c2d3 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneController.cs @@ -2,8 +2,8 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; -using IoT.Shared.Areas.IoTCenter.Controlls; using IoT.Shared.Application.Domain.Entities; +using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -32,7 +32,7 @@ namespace Platform.Areas.IoTCenter.Controllers public override IQueryable Query(PagedListModel model, IQueryable query) { - ViewData.SelectList(o => model.Query.OrganId, () => this._ajax.GetOrganSelectList(model.Query.OrganId)); + ViewData.SelectList(o => model.Query.OrganId, () => this._ajax.GetOrgan(model.Query.OrganId).SelectList()); return query .WhereIf(model.Query.OrganId.HasValue, o => o.OrganId == model.Query.OrganId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) @@ -50,7 +50,7 @@ namespace Platform.Areas.IoTCenter.Controllers public override void ToEditModel(OrganIoTScene entity, EditOrganSceneModel model) { - ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrganSelectList(model.OrganId)); + ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); } } } diff --git a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTiggerController.cs b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTiggerController.cs index ce024921..6da70df6 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTiggerController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTiggerController.cs @@ -2,8 +2,8 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; -using IoT.Shared.Areas.IoTCenter.Controlls; using IoT.Shared.Application.Domain.Entities; +using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -53,7 +53,7 @@ namespace Platform.Areas.IoTCenter.Controllers public override void ToEditModel(OrganIoTSceneTigger entity, EditOrganSceneTiggerModel model) { this.ToDisplayModel(entity, model); - ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrganSelectList(model.OrganId)); + ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId), model.OrganId.HasValue); ViewData.SelectList(o => model.OrganSceneId, () => this._ajax.GetOrganSceneSelectList(model.OrganId.Value, model.OrganSceneId), model.OrganId.HasValue); ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetDeviceSelectList(model.NodeId.Value), model.NodeId.HasValue); diff --git a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTimerController.cs b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTimerController.cs index 082a9e18..cca33517 100644 --- a/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTimerController.cs +++ b/projects/Platform/Areas/IoTCenter/Controllers/OrganSceneTimerController.cs @@ -2,8 +2,8 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; -using IoT.Shared.Areas.IoTCenter.Controlls; using IoT.Shared.Application.Domain.Entities; +using IoT.Shared.Areas.IoTCenter.Controlls; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -46,7 +46,7 @@ namespace Platform.Areas.IoTCenter.Controllers public override void ToEditModel(OrganIoTSceneTimer entity, EditOrganSceneTimerModel model) { this.ToDisplayModel(entity, model); - ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrganSelectList(model.OrganId)); + ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.OrganSceneId, () => this._ajax.GetOrganSceneSelectList(model.OrganId.Value, model.OrganSceneId), model.OrganId.HasValue); } diff --git a/projects/Platform/Areas/UserCenter/Controllers/AreaController.cs b/projects/Platform/Areas/UserCenter/Controllers/AreaController.cs index a6b4cd87..7101cce7 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/AreaController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/AreaController.cs @@ -53,7 +53,7 @@ namespace Platform.Areas.UserCenter.Controllers public override void ToEditModel(Area entity, EditAreaModel model) { - ViewData.SelectList(o => model.ParentId, () => this._ajax.GetAreaSelectList(model.ParentId, model.Id)); + ViewData.SelectList(o => model.ParentId, () => this._ajax.GetArea(model.ParentId, model.Id).SelectList()); } public override void ToEntity(EditAreaModel model, Area entity) diff --git a/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs b/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs index 6a832f8a..71358ea7 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/BuildingController.cs @@ -5,7 +5,6 @@ using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Platform.Application.Models; using Platform.Areas.IoTCenter.Controllers; @@ -62,15 +61,16 @@ namespace Platform.Areas.UserCenter.Controllers public override void ToEditModel(Building entity, EditBuildingModel model) { + base.ToEditModel(entity, model); if (entity != null) { 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.GetOrganSelectList(model.OrganId)); + 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).Value as SelectList); + ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganBuilding(model.OrganId.Value, model.ParentId, model.Id).SelectList()); } } diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs b/projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs index ef703154..df1b1e73 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/OrganBuildingController.cs @@ -24,9 +24,9 @@ namespace Platform.Areas.UserCenter.Controllers public override IQueryable Query(PagedListModel model, IQueryable query) { - var currentOrganNumber = User.GetUserData(); + var organId = User.GetOrganId(); return base.Query(model, query) - .Where(o => o.Organ.Number == currentOrganNumber); + .WhereIf(organId.HasValue, o => o.Organ.Id == organId.Value); } } } diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganController.cs b/projects/Platform/Areas/UserCenter/Controllers/OrganController.cs index 1562c83c..35d421f3 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/OrganController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/OrganController.cs @@ -60,8 +60,8 @@ namespace Platform.Areas.UserCenter.Controllers public override void ToEditModel(Organ entity, EditOrganModel model) { - this.ViewData.SelectList(o => model.AreaId, () => this._ajax.GetAreaSelectList(model.AreaId)); - this.ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrganSelectList(model.ParentId, model.Id)); + this.ViewData.SelectList(o => model.AreaId, () => this._ajax.GetArea(model.AreaId).SelectList()); + this.ViewData.SelectList(o => model.ParentId, () => this._ajax.GetOrgan(model.ParentId, model.Id).SelectList()); } public override void ToEntity(EditOrganModel model, Organ entity) diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs b/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs index 71fca83a..2fd43b1e 100644 --- a/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs +++ b/projects/Platform/Areas/UserCenter/Controllers/OrganOrganController.cs @@ -24,9 +24,9 @@ namespace Platform.Areas.UserCenter.Controllers public override IQueryable Query(PagedListModel model, IQueryable query) { - var currentOrganNumber = User.GetUserData(); + var organId = User.GetOrganId(); return base.Query(model, query) - .Where(o => o.Number == currentOrganNumber); + .WhereIf(organId.HasValue, o => o.Id == organId.Value); } } diff --git a/projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs b/projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs new file mode 100644 index 00000000..402003de --- /dev/null +++ b/projects/Platform/Areas/UserCenter/Controllers/OrganUserController.cs @@ -0,0 +1,48 @@ +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 Microsoft.EntityFrameworkCore; +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.Platform)] + public class OrganUserController : CrudController + { + private readonly AjaxController _ajax; + + public OrganUserController(IRepository repo, AjaxController ajax) : base(repo) + { + this._ajax = ajax; + } + + public override IQueryable Include(IQueryable query) + { + return query + .Include(o => o.Organ) + .Include(o => o.User); + } + + 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})"); + } + + 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()); + } + } +} diff --git a/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml b/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml index 903e0fc3..da6c7b5d 100644 --- a/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml +++ b/projects/Platform/Areas/UserCenter/Views/Shared/_Menu.cshtml @@ -19,6 +19,10 @@ { } + @if (HasPermission("Read-Platform-OrganUser")) + { + + } @if (HasPermission("Read-Platform-Role")) { diff --git a/projects/Platform/Controllers/AccountController.cs b/projects/Platform/Controllers/AccountController.cs index b4e5b215..4bc40dca 100644 --- a/projects/Platform/Controllers/AccountController.cs +++ b/projects/Platform/Controllers/AccountController.cs @@ -198,7 +198,7 @@ namespace Platform.Controllers } else { - HttpContext.JwtSignIn(model.UserName, model.RememberMe, mainOrgan?.Number); + HttpContext.JwtSignIn(model.UserName, model.RememberMe, mainOrgan?.Id.ToString()); if (string.IsNullOrEmpty(returnUrl)) { returnUrl = Url.Action("Index", "Home"); diff --git a/projects/Platform/Controllers/HomeController.cs b/projects/Platform/Controllers/HomeController.cs index 19c9da28..97b1fc14 100644 --- a/projects/Platform/Controllers/HomeController.cs +++ b/projects/Platform/Controllers/HomeController.cs @@ -65,15 +65,16 @@ namespace Platform.Controllers public IActionResult Index2(HomeModel model) { - var currentOrganNumber = User.GetUserData(); + var organId = User.GetOrganId(); var organ = this._organRepo.ReadOnlyTable() - .FirstOrDefault(o => o.Number == currentOrganNumber); + .WhereIf(organId.HasValue, o => o.Id == organId.Value) + .FirstOrDefault(); var children = this._organRepo.Table() .Where(o => o.Left >= organ.Left && o.Right <= organ.Right) .Include(o => o.Buildings) .ToList(); - var root = children.FirstOrDefault(o => o.Number == currentOrganNumber); + var root = children.FirstOrDefault(o => o.Id == organId.Value); model.Organ = root; if (string.IsNullOrEmpty(model.OrganNumber)) diff --git a/projects/Platform/DbConfig.cs b/projects/Platform/DbConfig.cs index 2cf328dc..5adffac0 100644 --- a/projects/Platform/DbConfig.cs +++ b/projects/Platform/DbConfig.cs @@ -5,7 +5,6 @@ using Infrastructure.Security; using IoT.Shared; using IoT.Shared.Application.Domain.Entities; using Microsoft.EntityFrameworkCore; -using Platform.Application.Domain; using System.Collections.Generic; using System.Linq; diff --git a/projects/Platform/UserService.cs b/projects/Platform/UserService.cs index d2140ba5..4f2da6a4 100644 --- a/projects/Platform/UserService.cs +++ b/projects/Platform/UserService.cs @@ -17,11 +17,11 @@ namespace Platform this._userRepo = userRepo; } - public List GetOrganNumbers(string userName) + public List GetOrgans(string userName) { var list = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) .SelectMany(o => o.OrganUsers) - .Select(o => o.Organ.Number) + .Select(o => o.OrganId.ToString()) .ToList(); return list; } @@ -46,4 +46,4 @@ namespace Platform return list; } } -} \ No newline at end of file +} diff --git a/projects/Platform/Views/Shared/_Layout.cshtml b/projects/Platform/Views/Shared/_Layout.cshtml index fa3d220d..ec105a35 100644 --- a/projects/Platform/Views/Shared/_Layout.cshtml +++ b/projects/Platform/Views/Shared/_Layout.cshtml @@ -66,23 +66,24 @@ @{ var hasOrgan = false; var organs = new List(); - var organNumber = User.Claims.FirstOrDefault(o => o.Type == System.Security.Claims.ClaimTypes.UserData)?.Value; - if (!string.IsNullOrEmpty(organNumber) && User.Identity.IsAuthenticated) + var organIdValue = User.Claims.FirstOrDefault(o => o.Type == System.Security.Claims.ClaimTypes.UserData)?.Value; + var organId = Guid.Empty; + if (User.Identity.IsAuthenticated && Guid.TryParse(organIdValue ,out organId)) { var userName = User.Identity.Name; organs = organUserRepo.ReadOnlyTable() .Where(o => o.User.UserName == userName) .Select(o => o.Organ) .ToList(); - if (organs != null && organs.Any(o => o.Number == organNumber)) + if (organs != null && organs.Any(o => o.Id == organId)) { hasOrgan = true; } } if (hasOrgan) { - var organImage = organs.FirstOrDefault(o => o.Number == organNumber)?.Image ?? logo; - var list = new SelectList(organs, "Number", "Name", organNumber); + var organImage = organs.FirstOrDefault(o => o.Id == organId)?.Image ?? logo; + var list = new SelectList(organs, "Id", "Name", organId);