Exemple #1
0
 public Task <bool> ConfirmSharedLink(ConfirmSharedLinkParameter confirmSharedLinkParameter)
 {
     return(_confirmSharedLinkAction.Execute(confirmSharedLinkParameter));
 }
        public async Task <bool> Execute(ConfirmSharedLinkParameter confirmSharedLinkParameter)
        {
            if (confirmSharedLinkParameter == null)
            {
                throw new ArgumentNullException(nameof(confirmSharedLinkParameter));
            }

            if (string.IsNullOrWhiteSpace(confirmSharedLinkParameter.ConfirmationCode))
            {
                throw new BaseUmaException(Errors.ErrorCodes.InvalidRequestCode, Errors.ErrorDescriptions.TheConfirmationCodeMustBeSpecified);
            }

            var sharedLink = await _sharedLinkRepository.Get(confirmSharedLinkParameter.ConfirmationCode).ConfigureAwait(false);

            if (sharedLink == null)
            {
                throw new BaseUmaException(Errors.ErrorCodes.InvalidRequestCode, Errors.ErrorDescriptions.TheConfirmationCodeIsInvalid);
            }

            var resourceId = sharedLink.ResourceId;
            var resource   = await _resourceSetRepository.Get(resourceId).ConfigureAwait(false);

            if (resource == null)
            {
                throw new BaseUmaException(Errors.ErrorCodes.InternalError, string.Format(Errors.ErrorDescriptions.TheResourceSetDoesntExist, resourceId));
            }

            if (resource.Owner == confirmSharedLinkParameter.Subject)
            {
                throw new BaseUmaException(Errors.ErrorCodes.InternalError, Errors.ErrorDescriptions.TheSharedLinkCannotBeUsedByTheOwner);
            }

            var policy = new Policy
            {
                Id             = Guid.NewGuid().ToString(),
                ResourceSetIds = new List <string> {
                    resourceId
                },
                Claims = new List <Claim>
                {
                    new Claim
                    {
                        Type  = "sub",
                        Value = confirmSharedLinkParameter.Subject
                    }
                },
                Scopes = sharedLink.Scopes.ToList()
            };

            using (var transaction = new CommittableTransaction(new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                try
                {
                    await _sharedLinkRepository.Delete(sharedLink.ConfirmationCode).ConfigureAwait(false);

                    await _policyRepository.Add(policy).ConfigureAwait(false);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            return(true);
        }