예제 #1
0
        public async Task AddAsync(string box, string username, BoxRole?role = null)
        {
            var user = await GetUserAsyncOrFail(username);

            var boxEntry = await GetBoxAsyncOrFail(box);

            var boxUser = boxEntry.GetUser(username);

            if (boxUser != null)
            {
                throw new ArgumentException($"User '{username}' has been already added to box '{box}'.", nameof(username));
            }

            if (boxEntry.Users.Count() >= _featureSettings.UsersPerBoxLimit)
            {
                throw new InvalidOperationException($"Box: '{box}' already contains " +
                                                    $"{_featureSettings.UsersPerBoxLimit} users.");
            }

            boxUser = new BoxUser(user, role.GetValueOrDefault(BoxRole.BoxUser));
            if (user.IsActive)
            {
                boxUser.Activate();
            }

            boxUser.AddPermission(Permission.ReadEntryKeys);
            boxUser.AddPermission(Permission.ReadEntry);
            boxEntry.AddUser(boxUser);
            await _boxRepository.UpdateAsync(boxEntry);

            Logger.Info($"User '{username}' was added to the box '{boxEntry.Name}'.");
        }
예제 #2
0
        public async Task DeleteAllAsync(string box, string username)
        {
            var boxEntry = await GetBoxAsyncOrFail(box);

            var boxUser = boxEntry.GetUser(username);

            if (boxUser == null)
            {
                throw new ArgumentNullException(nameof(boxUser), $"User '{username}' has not been found in box '{box}'.");
            }

            boxUser.DeleteAllPermissions();
            await _boxRepository.UpdateAsync(boxEntry);

            Logger.Information($"User '{username}' permissions in box '{boxEntry.Name}' were deleted.");
        }
예제 #3
0
        public async Task CreateAsync(string box, string key, object value, string author, string encryptionKey)
        {
            var entryBox = await _boxRepository.GetAsync(box);

            if (entryBox == null)
            {
                throw new ArgumentException($"Box '{box}' has not been found.");
            }

            if (entryBox.Entries.Count() >= _featureSettings.EntriesPerBoxLimit)
            {
                throw new InvalidOperationException($"Box: '{box}' already contains " +
                                                    $"{_featureSettings.EntriesPerBoxLimit} entries.");
            }

            var randomSecureKey   = _encrypter.GetRandomSecureKey();
            var salt              = _encrypter.GetSalt(randomSecureKey);
            var serializedValue   = JsonConvert.SerializeObject(value);
            var encryptedValue    = _encrypter.Encrypt(serializedValue, salt, encryptionKey);
            var entry             = new Entry(key, encryptedValue, salt, author);
            var deserializedEntry = JsonConvert.SerializeObject(entry);
            var entrySize         = Encoding.UTF8.GetByteCount(deserializedEntry);

            if (entrySize > _featureSettings.EntrySizeBytesLimit)
            {
                throw new InvalidOperationException($"Entry size is too big: {entrySize} bytes " +
                                                    $"(limit: {_featureSettings.EntrySizeBytesLimit} bytes).");
            }

            await DeleteAsync(box, key);

            entryBox.AddEntry(entry);
            await _boxRepository.UpdateAsync(entryBox);

            Logger.Information($"Eentry '{key}' was added to the box '{box}'.");
        }