Exemple #1
0
    public async Task <AppSrvResult <long> > CreateAsync(CfgCreationDto input)
    {
        var exists = await _cfgRepository.AnyAsync(x => x.Name.Equals(input.Name));

        if (exists)
        {
            return(Problem(HttpStatusCode.BadRequest, "参数名称已经存在"));
        }

        var cfg = Mapper.Map <SysCfg>(input);

        cfg.Id = IdGenerater.GetNextId();

        var cacheKey    = _cacheService.ConcatCacheKey(CachingConsts.CfgSingleKeyPrefix, cfg.Id);
        var cahceBf     = _bloomFilterFactory.Create(CachingConsts.BloomfilterOfCacheKey);
        var addedStatus = await cahceBf.AddAsync(cacheKey);

        if (!addedStatus)
        {
            return(Problem(HttpStatusCode.BadRequest, "添加到布隆过滤器失败!"));
        }
        else
        {
            await _cfgRepository.InsertAsync(cfg);
        }

        return(cfg.Id);
    }
Exemple #2
0
    public async Task <AppSrvResult <long> > CreateAsync(UserCreationDto input)
    {
        if (await _userRepository.AnyAsync(x => x.Account == input.Account))
        {
            return(Problem(HttpStatusCode.BadRequest, "账号已经存在"));
        }

        var user = Mapper.Map <SysUser>(input);

        user.Id       = IdGenerater.GetNextId();
        user.Account  = user.Account.ToLower();
        user.Salt     = InfraHelper.Security.GenerateRandomCode(5);
        user.Password = InfraHelper.Security.MD5(user.Password + user.Salt);

        var cacheKey            = _cacheService.ConcatCacheKey(CachingConsts.UserValidatedInfoKeyPrefix, user.Id);
        var bloomFilterCacheKey = _bloomFilterFactory.Create(CachingConsts.BloomfilterOfCacheKey);
        await bloomFilterCacheKey.AddAsync(cacheKey);

        var bloomFilterAccount = _bloomFilterFactory.Create(CachingConsts.BloomfilterOfAccountsKey);
        await bloomFilterAccount.AddAsync(user.Account);

        await _userRepository.InsertAsync(user);

        return(user.Id);
    }
Exemple #3
0
    public async Task <AppSrvResult <long> > CreateAsync(DictCreationDto input)
    {
        var exists = await _dictRepository.AnyAsync(x => x.Name.Equals(input.Name.Trim()));

        if (exists)
        {
            return(Problem(HttpStatusCode.BadRequest, "字典名字已经存在"));
        }

        var  dists = new List <SysDict>();
        long id    = IdGenerater.GetNextId();
        var  dict  = new SysDict {
            Id = id, Name = input.Name, Value = input.Value, Ordinal = input.Ordinal, Pid = 0
        };

        dists.Add(dict);
        input.Children?.ForEach(x =>
        {
            dists.Add(new SysDict
            {
                Id      = IdGenerater.GetNextId(),
                Pid     = id,
                Name    = x.Name,
                Value   = x.Value,
                Ordinal = x.Ordinal
            });
        });

        var cacheKey    = _cacheService.ConcatCacheKey(CachingConsts.DictSingleKeyPrefix, id);
        var cahceBf     = _bloomFilterFactory.Create(CachingConsts.BloomfilterOfCacheKey);
        var addedStatus = await cahceBf.AddAsync(cacheKey);

        if (!addedStatus)
        {
            return(Problem(HttpStatusCode.BadRequest, "添加到布隆过滤器失败!"));
        }
        else
        {
            await _dictRepository.InsertRangeAsync(dists);
        }
        return(id);
    }
Exemple #4
0
    public async Task <AppSrvResult <UserValidatedInfoDto> > LoginAsync(UserLoginDto input)
    {
        var accountsFilter = _bloomFilterFactory.Create(CachingConsts.BloomfilterOfAccountsKey);
        var exists         = await accountsFilter.ExistsAsync(input.Account.ToLower());

        if (!exists)
        {
            return(Problem(HttpStatusCode.BadRequest, "用户名或密码错误"));
        }

        var user = await _userRepository.FetchAsync(x => new
        {
            x.Id,
            x.Account,
            x.Password,
            x.Salt,
            x.Status,
            x.Email,
            x.Name,
            x.RoleIds
        }, x => x.Account == input.Account);

        if (user is null)
        {
            return(Problem(HttpStatusCode.BadRequest, "用户名或密码错误"));
        }

        var httpContext   = InfraHelper.Accessor.GetCurrentHttpContext();
        var channelWriter = ChannelHelper <LoginLog> .Instance.Writer;
        var log           = new LoginLog
        {
            Account         = input.Account,
            Succeed         = false,
            UserId          = user.Id,
            UserName        = user.Name,
            CreateTime      = DateTime.Now,
            Device          = httpContext.Request.Headers["device"].FirstOrDefault() ?? "web",
            RemoteIpAddress = httpContext.Connection.RemoteIpAddress.MapToIPv4().ToString()
        };

        if (user.Status != 1)
        {
            var problem = Problem(HttpStatusCode.TooManyRequests, "账号已锁定");
            log.Message    = problem.Detail;
            log.StatusCode = problem.Status.Value;
            await channelWriter.WriteAsync(log);

            return(problem);
        }

        // TODO
        var failLoginCount = 2;

        if (failLoginCount >= 5)
        {
            var problem = Problem(HttpStatusCode.TooManyRequests, "连续登录失败次数超过5次,账号已锁定");
            log.Message    = problem.Detail;
            log.StatusCode = problem.Status.Value;
            await channelWriter.WriteAsync(log);

            await _cacheService.RemoveCachesAsync(async (cancellToken) =>
            {
                await _userRepository.UpdateAsync(new SysUser()
                {
                    Id = user.Id, Status = 0
                }, UpdatingProps <SysUser>(x => x.Status), cancellToken);
            }, _cacheService.ConcatCacheKey(CachingConsts.UserValidatedInfoKeyPrefix, user.Id));

            return(problem);
        }

        if (InfraHelper.Security.MD5(input.Password + user.Salt) != user.Password)
        {
            var problem = Problem(HttpStatusCode.BadRequest, "用户名或密码错误");
            log.Message    = problem.Detail;
            log.StatusCode = problem.Status.Value;
            await channelWriter.WriteAsync(log);

            return(problem);
        }

        if (user.RoleIds.IsNullOrEmpty())
        {
            var problem = Problem(HttpStatusCode.Forbidden, "未分配任务角色,请联系管理员");
            log.Message    = problem.Detail;
            log.StatusCode = problem.Status.Value;
            await channelWriter.WriteAsync(log);

            return(problem);
        }

        log.Message    = "登录成功";
        log.StatusCode = (int)HttpStatusCode.Created;
        log.Succeed    = true;
        await channelWriter.WriteAsync(log);

        var userValidtedInfo = new UserValidatedInfoDto(user.Id, user.Account, user.Name, user.RoleIds, user.Status);
        await _cacheService.SetValidateInfoToCacheAsync(userValidtedInfo);

        return(userValidtedInfo);
    }