diff --git a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/LoginLogController.cs b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/LoginLogController.cs index 754cf4ff..19cccf8c 100644 --- a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/LoginLogController.cs +++ b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/LoginLogController.cs @@ -34,6 +34,7 @@ public AjaxResult Read(PageRequest request) { Check.NotNull(request, nameof(request)); + request.AddDefaultSortCondition(new SortCondition("CreatedTime", ListSortDirection.Descending)); Expression> exp = FilterService.GetExpression(request.FilterGroup); var page = IdentityContract.LoginLogs.ToPage(exp, request.PageCondition, @@ -44,8 +45,8 @@ public AjaxResult Read(PageRequest request) }).ToPageResult(data => data.Select(m => new LoginLogOutputDto(m.D) { UserName = m.User.UserName, - NickName = m.User.NickName - }).ToArray()); + NickName = m.User.NickName, +}).ToArray()); return new AjaxResult(page.ToPageData()); } diff --git a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/UserRoleController.cs b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/UserRoleController.cs index 4296326b..27a96202 100644 --- a/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/UserRoleController.cs +++ b/src/OSharp.Hosting.Apis/Areas/Admin/Controllers/Identity/UserRoleController.cs @@ -49,6 +49,7 @@ public AjaxResult Read(PageRequest request) { UserName = m.UserName, RoleName = m.RoleName, + IsLocked = m.D.IsLocked, Updatable = updateFunc(m.D), Deletable = deleteFunc(m.D) }).ToArray()); diff --git a/src/OSharp.Hosting.Core/Identity/Dtos/UserOutputDto.cs b/src/OSharp.Hosting.Core/Identity/Dtos/UserOutputDto.cs index f3df8019..90bae16d 100644 --- a/src/OSharp.Hosting.Core/Identity/Dtos/UserOutputDto.cs +++ b/src/OSharp.Hosting.Core/Identity/Dtos/UserOutputDto.cs @@ -40,6 +40,7 @@ public UserOutputDto(User u) LockoutEnabled = u.LockoutEnabled; AccessFailedCount = u.AccessFailedCount; IsLocked = u.IsLocked; + IsSystem = u.IsSystem; CreatedTime = u.CreatedTime; } @@ -98,6 +99,11 @@ public UserOutputDto(User u) /// public bool IsLocked { get; set; } + /// + /// 获取或设置 是否系统用户 + /// + public bool IsSystem { get; set; } + /// /// 获取或设置 创建时间 /// diff --git a/src/OSharp.Hosting.Core/Identity/IdentityService.UserRole.cs b/src/OSharp.Hosting.Core/Identity/IdentityService.UserRole.cs index edad3fb3..fa3de750 100644 --- a/src/OSharp.Hosting.Core/Identity/IdentityService.UserRole.cs +++ b/src/OSharp.Hosting.Core/Identity/IdentityService.UserRole.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) 2014-2018 OSharp. All rights reserved. // @@ -50,14 +50,21 @@ public IQueryable UserRoles /// 业务操作结果 public async Task UpdateUserRoles(params UserRoleInputDto[] dtos) { - Check2.Validate(dtos, nameof(dtos)); + Check2.Validate(dtos, nameof(dtos)); List userNames = new List(); OperationResult result = await UserRoleRepository.UpdateAsync(dtos, (dto, entity) => { - string userName = UserRoleRepository.QueryAsNoTracking(m => m.UserId == entity.UserId).Select(m => m.User.UserName).FirstOrDefault(); - userNames.AddIfNotNull(userName); + var user = UserRepository.QueryAsNoTracking(m => m.Id == entity.UserId).Select(m => new + { + m.UserName, m.IsSystem + }).First(); + if (user.IsSystem) + { + throw new OsharpException($"系统用户“{user.UserName}”的角色分配不能更新"); + } + userNames.AddIfNotNull(user.UserName); return Task.FromResult(0); }); if (result.Succeeded && userNames.Count > 0) @@ -75,17 +82,25 @@ public async Task UpdateUserRoles(params UserRoleInputDto[] dto /// 业务操作结果 public async Task DeleteUserRoles(Guid[] ids) { - ListuserNames = new List(); + List userNames = new List(); OperationResult result = await UserRoleRepository.DeleteAsync(ids, (entity) => { - string userName = UserRoleRepository.QueryAsNoTracking(m => m.UserId == entity.UserId).Select(m => m.User.UserName).FirstOrDefault(); - userNames.AddIfNotNull(userName); + var user = UserRepository.QueryAsNoTracking(m => m.Id == entity.UserId).Select(m => new + { + m.UserName, + m.IsSystem + }).First(); + if (user.IsSystem) + { + throw new OsharpException($"系统用户“{user.UserName}”的角色不能删除"); + } + userNames.AddIfNotNull(user.UserName); return Task.FromResult(0); }); if (result.Succeeded && userNames.Count > 0) { - OnlineUserCacheRemoveEventData eventData = new OnlineUserCacheRemoveEventData(){UserNames = userNames.ToArray()}; + OnlineUserCacheRemoveEventData eventData = new OnlineUserCacheRemoveEventData() { UserNames = userNames.ToArray() }; await EventBus.PublishAsync(eventData); } @@ -105,6 +120,11 @@ public async Task SetUserRoles(int userId, int[] roleIds) { return new OperationResult(OperationResultType.QueryNull, $"编号为“{userId}”的用户不存在"); } + + if (user.IsSystem) + { + return new OperationResult(OperationResultType.Error, $"系统用户“{user.UserName}”不能变更角色"); + } IList roleNames = RoleManager.Roles.Where(m => roleIds.Contains(m.Id)).Select(m => m.Name).ToList(); IList existRoleNames = await UserManager.GetRolesAsync(user); string[] addRoleNames = roleNames.Except(existRoleNames).ToArray(); @@ -148,4 +168,4 @@ public async Task SetUserRoles(int userId, int[] roleIds) return new OperationResult(OperationResultType.Success, $"用户“{user.UserName}”添加角色“{addRoleNames.ExpandAndToString()}”,移除角色“{removeRoleNames.ExpandAndToString()}”操作成功"); } -} \ No newline at end of file +} diff --git a/src/OSharp.Hosting.Core/Identity/IdentityService.cs b/src/OSharp.Hosting.Core/Identity/IdentityService.cs index 15db26a1..2f34392a 100644 --- a/src/OSharp.Hosting.Core/Identity/IdentityService.cs +++ b/src/OSharp.Hosting.Core/Identity/IdentityService.cs @@ -33,6 +33,7 @@ public IdentityService(IServiceProvider provider) protected IEventBus EventBus => _provider.GetService(); protected RoleManager RoleManager => _provider.GetService>(); + protected IRepository UserRepository => _provider.GetService>(); protected IRepository UserDetailRepository => _provider.GetService>(); protected IRepository UserLoginRepository => _provider.GetService>(); protected IRepository LoginLogReqRepository => _provider.GetService>();