Exemplo n.º 1
0
        public async Task <ResultResponseDto> AddScope(ScopeDto dto)
        {
            if (!dto.IsValid())
            {
                return(Result.ReFailure <ResultResponseDto>("请求参数错误", ResultCodes.InvalidParameter));
            }
            if (base.State == null)
            {
                return(Result.ReFailure <ResultResponseDto>("API资源不存在", ResultCodes.InvalidParameter));
            }

            ApiScope scope = Mapper.Map <ApiScope>(dto);

            if (!scope.IsValid())
            {
                return(Result.ReFailure <ResultResponseDto>("请求参数错误", ResultCodes.InvalidParameter));
            }

            IApiResourceRepositories dataStore = this.ServiceProvider.GetRequiredService <IApiResourceRepositories>();

            if (await dataStore.GetScopeIdAsync(scope.Name) > 0)
            {
                return(Result.ReFailure <ResultResponseDto>(scope.Name + "Api范围已经存在", ResultCodes.InvalidParameter));
            }
            this.State.Scopes.Add(scope);
            await base.WriteStateAsync();

            return(Result.ReSuccess <ResultResponseDto>());
        }
Exemplo n.º 2
0
        public async Task <ResultResponseDto> AddAsync(ApiResourceAddRequestDto dto)
        {
            var result = dto.ValidResult();

            if (!result.Success)
            {
                return(Result.ReFailure <ResultResponseDto>(result));
            }
            ApiResource apiResource = new ApiResource(dto.Name, dto.DisplayName, dto.UserClaims);

            apiResource.Description = dto.Description;

            //获取数据商店
            IApiResourceRepositories dataStore = this.ServiceProvider.GetRequiredService <IApiResourceRepositories>();

            if (await dataStore.GetIdAsync(apiResource.Name) > 0)
            {
                return(Result.ReFailure <ResultResponseDto>(apiResource.Name + " Api资源已经存在", ResultCodes.InvalidParameter));
            }
            //添加秘钥
            foreach (var item in dto.Secrets)
            {
                Secret secret = new Secret(item.Value, item.Type, item.Description, item.Expiration);
                if (!secret.IsValid())
                {
                    return(Result.ReFailure <ResultResponseDto>("请求参数错误", ResultCodes.InvalidParameter));
                }
                apiResource.Secrets.Add(secret);
            }
            //添加包含范围
            foreach (var item in dto.Scopes)
            {
                ApiScope scope = Mapper.Map <ApiScope>(item);
                if (!scope.IsValid())
                {
                    return(Result.ReFailure <ResultResponseDto>("请求参数错误", ResultCodes.InvalidParameter));
                }
                //判断数据库以及当前Api资源是否存在当前Api范围
                if (await dataStore.GetScopeIdAsync(scope.Name) > 0 || apiResource.Scopes.ToList().Exists(f => f.Name == scope.Name))
                {
                    return(Result.ReFailure <ResultResponseDto>(scope.Name + "Api范围已经存在", ResultCodes.InvalidParameter));
                }
                apiResource.Scopes.Add(scope);
            }

            if (!apiResource.IsValid())
            {
                return(Result.ReFailure <ResultResponseDto>("请求参数错误,Api资源不合法", ResultCodes.InvalidParameter));
            }
            base.State = apiResource;
            await base.WriteStateAsync();

            return(Result.ReSuccess <ResultResponseDto>());
        }