Ejemplo n.º 1
0
        public void Handle(EditFileCommand command)
        {
            File file = fileRepository.GetById(command.FileId).EnsureFound(command.FileId);

            if (!file.IsOwnedBy(currentUser.ToDomainUser()))
            {
                throw new PermissionException($"The user doesn't have a permission to edit the file with id {command.FileId}");
            }

            file.Edit(command.FileName, command.Description, DateTime.UtcNow);
            fileRepository.Update(file);

            eventBus.PublishAfterCommit <FileEditedEvent, File>(file);
        }
        public void Handle(UnshareFileCommand command)
        {
            File file = fileRepository.GetById(command.FileId).EnsureFound(command.FileId);

            if (!HasPermissionToUnshare(command, file))
            {
                throw new PermissionException($"The user doesn't have a permission to unshare the file with id {command.FileId}");
            }

            file.Unshare(new User(command.UserId, "N/A"));

            eventBus.PublishAfterCommit <FileSharedEvent, FileSharesChangedMessage>(
                new FileSharesChangedMessage(file, command.UserId));
        }
        public void Handle(DeleteFileCommand command)
        {
            File file = fileRepository.GetById(command.FileId)
                        .EnsureFound(command.FileId);

            if (!file.IsOwnedBy(currentUser.ToDomainUser()))
            {
                throw new PermissionException($"The user doesn't have a permission to delete the file with id {command.FileId}");
            }

            // ensure that the file is deleted from disk before deleting the entity
            fileRepository.Delete(file);
            fileStorage.DeleteFile(file);

            eventBus.PublishAfterCommit <FileDeletedEvent, File>(file);
        }
        public void Handle(AcquireFileLockCommand command)
        {
            File file = fileRepository.GetById(command.FileId).EnsureFound(command.FileId);

            User currentUser = currentUserSource.GetCurrentUser();

            if (!file.CanBeModifiedBy(currentUser))
            {
                throw new PermissionException($"The user doesn't have a permission to lock the file with id {command.FileId}");
            }

            fileLockingService.Lock(file, currentUser);
            UserDto lockOwner = fileLockingService.GetLockOwner(file);

            eventBus.PublishAfterCommit <FileLockChangedEvent, FileLockChangedMessage>(
                new FileLockChangedMessage(command.FileId, FileLockDto.ForUser(lockOwner)));
        }
Ejemplo n.º 5
0
        public void Handle(RemoveFileLockCommand command)
        {
            File file = fileRepository.GetById(command.FileId);

            UserDto lockOwner = fileLockingService.GetRequiredLockOwner(file);

            if (lockOwner.Id != currentUser.Id)
            {
                throw new PermissionException("The current user is not the lock owner");
            }

            fileLockingService.Unlock(file, currentUser.ToDomainUser());

            UserDto newLockOwner = fileLockingService.GetLockOwner(file);

            eventBus.PublishAfterCommit <FileLockChangedEvent, FileLockChangedMessage>(
                new FileLockChangedMessage(command.FileId, FileLockDto.ForUser(newLockOwner)));
        }
Ejemplo n.º 6
0
        public void Handle(ShareFileCommand command)
        {
            File file = fileRepository.GetById(command.FileId).EnsureFound(command.FileId);

            if (!file.IsOwnedBy(currentUser.ToDomainUser()))
            {
                throw new PermissionException($"The user doesn't have a permission to share the file with id {command.FileId}");
            }

            User shareWith = userRepository.GetById(command.ShareWithUserId);

            if (shareWith == null)
            {
                throw new NotFoundException($"A user with id {command.ShareWithUserId} doesn't exist in the database.");
            }

            file.ShareWith(shareWith);

            eventBus.PublishAfterCommit <FileSharedEvent, FileSharesChangedMessage>(
                new FileSharesChangedMessage(file, command.ShareWithUserId));
        }