Пример #1
0
        public async Task ShareManyAsync(IEnumerable <Cipher> ciphers, Guid organizationId,
                                         IEnumerable <Guid> collectionIds, Guid sharingUserId)
        {
            var cipherIds = new List <Guid>();

            foreach (var cipher in ciphers)
            {
                if (cipher.Id == default(Guid))
                {
                    throw new BadRequestException("All ciphers must already exist.");
                }

                if (cipher.OrganizationId.HasValue)
                {
                    throw new BadRequestException("One or more ciphers already belong to an organization.");
                }

                if (!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
                {
                    throw new BadRequestException("One or more ciphers do not belong to you.");
                }

                if (!string.IsNullOrWhiteSpace(cipher.Attachments))
                {
                    throw new BadRequestException("One or more ciphers have attachments.");
                }

                cipher.UserId         = null;
                cipher.OrganizationId = organizationId;
                cipher.RevisionDate   = DateTime.UtcNow;
                cipherIds.Add(cipher.Id);
            }

            await _cipherRepository.UpdateCiphersAsync(sharingUserId, ciphers);

            await _collectionCipherRepository.UpdateCollectionsForCiphersAsync(cipherIds, sharingUserId,
                                                                               organizationId, collectionIds);

            // TODO: move this to a single event?
            foreach (var cipher in ciphers)
            {
                await _eventService.LogCipherEventAsync(cipher, Enums.EventType.Cipher_Shared);
            }

            // push
            await _pushService.PushSyncCiphersAsync(sharingUserId);
        }
Пример #2
0
        public async Task ShareManyAsync(IEnumerable <Cipher> ciphers, Guid organizationId,
                                         IEnumerable <Guid> collectionIds, Guid sharingUserId)
        {
            var cipherIds = new List <Guid>();

            foreach (var cipher in ciphers)
            {
                if (cipher.Id == default(Guid))
                {
                    throw new BadRequestException("All ciphers must already exist.");
                }

                if (cipher.OrganizationId.HasValue)
                {
                    throw new BadRequestException("One or more ciphers already belong to an organization.");
                }

                if (!cipher.UserId.HasValue || cipher.UserId.Value != sharingUserId)
                {
                    throw new BadRequestException("One or more ciphers do not belong to you.");
                }

                cipher.UserId         = null;
                cipher.OrganizationId = organizationId;
                cipher.RevisionDate   = DateTime.UtcNow;
                cipherIds.Add(cipher.Id);
            }

            await _cipherRepository.UpdateCiphersAsync(sharingUserId, ciphers);

            await _collectionCipherRepository.UpdateCollectionsForCiphersAsync(cipherIds, sharingUserId,
                                                                               organizationId, collectionIds);

            var events = ciphers.Select(c =>
                                        new Tuple <Cipher, EventType, DateTime?>(c, EventType.Cipher_Shared, null));

            foreach (var eventsBatch in events.Batch(100))
            {
                await _eventService.LogCipherEventsAsync(eventsBatch);
            }

            // push
            await _pushService.PushSyncCiphersAsync(sharingUserId);
        }