Esempio n. 1
0
        public async Task <(bool success, string?subject)> Execute(
            ResourceOwner resourceOwner, CancellationToken cancellationToken)
        {
            if (!resourceOwner.IsLocalAccount || string.IsNullOrWhiteSpace(resourceOwner.Subject))
            {
                var subject = await _subjectBuilder.BuildSubject(resourceOwner.Claims, cancellationToken)
                              .ConfigureAwait(false);

                resourceOwner.Subject = subject;
            }

            // 1. Check if the resource owner already exists.
            if (await _resourceOwnerRepository.Get(resourceOwner.Subject, cancellationToken).ConfigureAwait(false) !=
                null)
            {
                return(false, null);
            }

            var additionalClaims = _settings.ClaimsIncludedInUserCreation
                                   .Except(resourceOwner.Claims.Select(x => x.Type))
                                   .Select(x => new Claim(x, string.Empty));

            resourceOwner.Claims = resourceOwner.Claims.Add(additionalClaims);

            // Customize new resource owner.
            _settings.OnResourceOwnerCreated(resourceOwner);

            var now = DateTimeOffset.UtcNow;

            if (_accountFilters != null)
            {
                var isFilterValid = true;
                foreach (var resourceOwnerFilter in _accountFilters)
                {
                    var userFilterResult = await resourceOwnerFilter.Check(resourceOwner.Claims, cancellationToken)
                                           .ConfigureAwait(false);

                    if (userFilterResult.IsValid)
                    {
                        continue;
                    }

                    isFilterValid = false;
                    foreach (var ruleResult in userFilterResult.AccountFilterRules.Where(x => !x.IsValid))
                    {
                        await _eventPublisher.Publish(
                            new FilterValidationFailure(
                                Id.Create(),
                                string.Format(Strings.TheFilterRuleFailed, ruleResult.RuleName),
                                now))
                        .ConfigureAwait(false);

                        foreach (var errorMessage in ruleResult.ErrorMessages)
                        {
                            await _eventPublisher
                            .Publish(new FilterValidationFailure(Id.Create(), errorMessage, now))
                            .ConfigureAwait(false);
                        }
                    }
                }

                if (!isFilterValid)
                {
                    return(false, null);
                }
            }

            resourceOwner.UpdateDateTime = now;
            resourceOwner.CreateDateTime = now;
            if (!await _resourceOwnerRepository.Insert(resourceOwner, cancellationToken).ConfigureAwait(false))
            {
                return(false, null);
            }

            await _eventPublisher.Publish(
                new ResourceOwnerAdded(
                    Id.Create(),
                    resourceOwner.Subject,
                    resourceOwner.Claims.Select(claim => new ClaimData
            {
                Type = claim.Type,
                Value = claim.Value
            })
                    .ToArray(),
                    now))
            .ConfigureAwait(false);

            return(true, resourceOwner.Subject);
        }