/// <summary>
        /// Check the confirmation link is valid.
        /// </summary>
        /// <param name="officeDocumentConfirmationLink"></param>
        private void CheckConfirmationLink(OfficeDocumentConfirmationLink officeDocumentConfirmationLink)
        {
            if (officeDocumentConfirmationLink.ExpiresIn != null)
            {
                if (DateTime.UtcNow >= officeDocumentConfirmationLink.CreateDateTime.AddSeconds(officeDocumentConfirmationLink.ExpiresIn.Value))
                {
                    throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, ErrorDescriptions.ConfirmationCodeIsExpired);
                }
            }

            if (officeDocumentConfirmationLink.NumberOfConfirmations != null && officeDocumentConfirmationLink.NumberOfConfirmations.Value == 0)
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, ErrorDescriptions.NotEnoughConfirmationCode);
            }
        }
コード例 #2
0
        public static OfficeDocumentInvitationLinkResponse ToDto(this OfficeDocumentConfirmationLink officeDocumentConfirmationLink)
        {
            if (officeDocumentConfirmationLink == null)
            {
                throw new ArgumentNullException(nameof(officeDocumentConfirmationLink));
            }

            return(new OfficeDocumentInvitationLinkResponse
            {
                ConfirmationCode = officeDocumentConfirmationLink.ConfirmationCode,
                CreateDateTime = officeDocumentConfirmationLink.CreateDateTime,
                DocumentId = officeDocumentConfirmationLink.DocumentId,
                ExpiresIn = officeDocumentConfirmationLink.ExpiresIn,
                NumberOfConfirmations = officeDocumentConfirmationLink.NumberOfConfirmations,
                UpdateDateTime = officeDocumentConfirmationLink.UpdateDateTime
            });
        }
        public Task <bool> Add(OfficeDocumentConfirmationLink officeDocumentConfirmationLink)
        {
            if (officeDocumentConfirmationLink == null)
            {
                throw new ArgumentNullException(nameof(officeDocumentConfirmationLink));
            }

            var confirmationLink = _confirmationLinks.FirstOrDefault(c => c.ConfirmationCode == officeDocumentConfirmationLink.ConfirmationCode);

            if (confirmationLink != null)
            {
                return(Task.FromResult(false));
            }

            officeDocumentConfirmationLink.CreateDateTime = DateTime.UtcNow;
            _confirmationLinks.Add(officeDocumentConfirmationLink);
            return(Task.FromResult(true));
        }
        public async Task <string> Execute(string documentId, GenerateConfirmationLinkParameter generateConfirmationCodeParameter)
        {
            if (string.IsNullOrWhiteSpace(documentId))
            {
                throw new ArgumentNullException(nameof(documentId));
            }

            _generateConfirmationLinkParameterValidator.Check(generateConfirmationCodeParameter);
            var officeDocument = await _officeDocumentRepository.Get(documentId);

            if (officeDocument == null)
            {
                throw new DocumentNotFoundException();
            }

            if (string.IsNullOrWhiteSpace(officeDocument.UmaResourceId))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.NoUmaResource);
            }

            if (string.IsNullOrWhiteSpace(officeDocument.UmaPolicyId))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.NoUmaPolicy);
            }

            if (officeDocument.Subject != generateConfirmationCodeParameter.Subject)
            {
                throw new NotAuthorizedException();
            }

            var confirmationLink = new OfficeDocumentConfirmationLink
            {
                ConfirmationCode      = Guid.NewGuid().ToString(),
                DocumentId            = documentId,
                ExpiresIn             = generateConfirmationCodeParameter.ExpiresIn,
                NumberOfConfirmations = generateConfirmationCodeParameter.NumberOfConfirmations
            };
            await _officeDocumentConfirmationLinkStore.Add(confirmationLink);

            return(confirmationLink.ConfirmationCode);
        }
        /// <summary>
        /// Use the confirmation code.
        /// </summary>
        /// <param name="officeDocumentConfirmationLink"></param>
        /// <returns></returns>
        private async Task UseConfirmationLink(OfficeDocumentConfirmationLink officeDocumentConfirmationLink)
        {
            if (officeDocumentConfirmationLink.NumberOfConfirmations != null)
            {
                var nbConfirmations = officeDocumentConfirmationLink.NumberOfConfirmations.Value;
                nbConfirmations--;
                officeDocumentConfirmationLink.NumberOfConfirmations = nbConfirmations;
                if (nbConfirmations == 0)
                {
                    await _officeDocumentConfirmationLinkStore.Remove(officeDocumentConfirmationLink.ConfirmationCode);

                    return;
                }

                await _officeDocumentConfirmationLinkStore.Update(officeDocumentConfirmationLink);

                return;
            }

            await _officeDocumentConfirmationLinkStore.Remove(officeDocumentConfirmationLink.ConfirmationCode);
        }