Exemplo n.º 1
0
        /// <summary>
        /// 异步添加角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <OperationResponse> AddRoleAsync(RoleInputDto dto)
        {
            dto.NotNull(nameof(dto));
            var role = dto.MapTo <Role>();

            return(await _roleMenuRepository.UnitOfWork.UseTranAsync(async() =>
            {
                var result = await _roleManager.CreateAsync(role);
                if (!result.Succeeded)
                {
                    return result.ToOperationResponse();
                }
                if (dto.MenuIds?.Any() == true)
                {
                    var list = dto.MenuIds.Select(x => new RoleMenuEntity
                    {
                        MenuId = x.Value,
                        RoleId = role.Id,
                    }).ToArray();
                    int count = await _roleMenuRepository.InsertAsync(list);
                    await _eventBus?.PublishAsync(new RoleMenuCacheAddOrUpdateEvent()
                    {
                        RoleId = role.Id, MenuIds = dto.MenuIds.Select(o => o.Value), EventState = Flow.Events.EventState.Add
                    });
                    if (count <= 0)
                    {
                        return new OperationResponse("保存失败", OperationResponseType.Error);
                    }
                }
                return new OperationResponse("保存成功", OperationResponseType.Success);
            }));
        }
Exemplo n.º 2
0
        public override async Task <int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            var auditlist = _changeTracker.GetChangeTrackerList(this.ChangeTracker.Entries());
            var result    = await base.SaveChangesAsync(cancellationToken);

            await _bus.PublishAsync(new AuditEvent()
            {
                AuditList = auditlist
            });

            return(result);
        }
Exemplo n.º 3
0
        private async Task <OperationResponse> DeleteUserRoleAsync(User user)
        {
            IList <string> existRoleNames = await _userManager.GetRolesAsync(user);

            IdentityResult result = await _userManager.RemoveFromRolesAsync(user, existRoleNames);

            if (!result.Succeeded)
            {
                return(result.ToOperationResponse());
            }
            _bus?.PublishAsync(new UserRoleCacheDeleteEvent()
            {
                UserId = user.Id, EventState = Events.EventState.Remove
            });
            return(result.ToOperationResponse());
        }
Exemplo n.º 4
0
        public async Task <(OperationResponse item, Claim[] cliams)> Login(LoginDto loginDto)
        {
            loginDto.NotNull(nameof(loginDto));
            var user = await _userManager.FindByNameAsync(loginDto.UserName);

            if (user == null)
            {
                return(new OperationResponse("此用户不存在!!", OperationResponseType.Error), new Claim[] { });
            }
            var signInResult = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, true);

            if (!signInResult.Succeeded)
            {
                if (signInResult.IsLockedOut)
                {
                    return(new OperationResponse($"用户因密码错误次数过多而被锁定 {_userManager.Options.Lockout.DefaultLockoutTimeSpan.TotalMinutes} 分钟,请稍后重试", OperationResponseType.Error), new Claim[] { });
                }
                if (signInResult.IsNotAllowed)
                {
                    return(new OperationResponse("不允许登录。", OperationResponseType.Error), new Claim[] { });
                }
                return(new OperationResponse("登录失败,用户名或账号无效。", OperationResponseType.Error), new Claim[] { });
            }

            var jwtToken = _jwtBearerService.CreateToken(user.Id, user.UserName);
            await _bus.PublishAsync(new IdentityEvent()
            {
                UserName = loginDto.UserName
            });

            return(new OperationResponse("登录成功", new
            {
                AccessToken = jwtToken.AccessToken,
                NickName = user.NickName,
                UserId = user.Id.ToString(),
                AccessExpires = jwtToken.AccessExpires
            }, OperationResponseType.Success), jwtToken.claims);
        }
Exemplo n.º 5
0
 protected async Task PublishEventAsync(Event @event) => await mediator.PublishAsync(@event);