diff --git a/app/Entities/Controllers/BookController.php b/app/Entities/Controllers/BookController.php index a1c586f47e7..6cdde20d4c2 100644 --- a/app/Entities/Controllers/BookController.php +++ b/app/Entities/Controllers/BookController.php @@ -19,6 +19,7 @@ use BookStack\Http\Controller; use BookStack\References\ReferenceFetcher; use BookStack\Util\SimpleListOptions; +use BookStack\Settings\SettingService; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; use Throwable; @@ -31,6 +32,7 @@ public function __construct( protected BookQueries $queries, protected BookshelfQueries $shelfQueries, protected ReferenceFetcher $referenceFetcher, + protected SettingService $settingService, ) { } @@ -115,6 +117,17 @@ public function store(Request $request, string $shelfSlug = null) if ($bookshelf) { $bookshelf->appendBook($book); Activity::add(ActivityType::BOOKSHELF_UPDATE, $bookshelf); + + $should_inherit_perms = $bookshelf->new_books_inherit_perms ?? $this->settingService->get("app-new-books-inherit-perms"); + + if ($should_inherit_perms) { + // Copy permissions from the shelf to the book on creation + // https://github.com/BookStackApp/BookStack/issues/4411#issuecomment-1962302337 + $shelfPermissions = $bookshelf->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray(); + $book->permissions()->delete(); + $book->permissions()->createMany($shelfPermissions); + $book->rebuildPermissions(); + } } return redirect($book->getUrl()); diff --git a/app/Entities/Controllers/BookshelfController.php b/app/Entities/Controllers/BookshelfController.php index 6cedd23e7df..271ce9459d2 100644 --- a/app/Entities/Controllers/BookshelfController.php +++ b/app/Entities/Controllers/BookshelfController.php @@ -16,6 +16,7 @@ use Exception; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; +use Illuminate\Validation\Rule; class BookshelfController extends Controller { @@ -156,6 +157,7 @@ public function edit(string $slug) return view('shelves.edit', [ 'shelf' => $shelf, 'books' => $books, + 'edit_perms' => userCan('restrictions-manage', $shelf), ]); } @@ -171,10 +173,11 @@ public function update(Request $request, string $slug) $shelf = $this->queries->findVisibleBySlugOrFail($slug); $this->checkOwnablePermission('bookshelf-update', $shelf); $validated = $this->validate($request, [ - 'name' => ['required', 'string', 'max:255'], - 'description_html' => ['string', 'max:2000'], - 'image' => array_merge(['nullable'], $this->getImageValidationRules()), - 'tags' => ['array'], + 'name' => ['required', 'string', 'max:255'], + 'description_html' => ['string', 'max:2000'], + 'image' => array_merge(['nullable'], $this->getImageValidationRules()), + 'tags' => ['array'], + 'new_books_inherit_perms' => ['string', Rule::in(['true', 'false', 'null'])], ]); if ($request->has('image_reset')) { @@ -183,6 +186,14 @@ public function update(Request $request, string $slug) unset($validated['image']); } + if ($request->has('new_books_inherit_perms')) { + $this->checkOwnablePermission('restrictions-manage', $shelf); + $p = $validated['new_books_inherit_perms']; + $validated['new_books_inherit_perms'] = $p == "true" ? true : ($p == "false" ? false : null); + } else { + unset($validated['new_books_inherit_perms']); + } + $bookIds = explode(',', $request->get('books', '')); $shelf = $this->shelfRepo->update($shelf, $validated, $bookIds); diff --git a/app/Entities/Models/Bookshelf.php b/app/Entities/Models/Bookshelf.php index 9ffa0ea9cab..82d48583caf 100644 --- a/app/Entities/Models/Bookshelf.php +++ b/app/Entities/Models/Bookshelf.php @@ -17,7 +17,7 @@ class Bookshelf extends Entity implements HasCoverImage public float $searchFactor = 1.2; - protected $fillable = ['name', 'description', 'image_id']; + protected $fillable = ['name', 'description', 'image_id', 'new_books_inherit_perms']; protected $hidden = ['image_id', 'deleted_at', 'description_html']; diff --git a/database/migrations/2024_09_12_204800_add_new_books_inherit_perms_to_bookshelves.php b/database/migrations/2024_09_12_204800_add_new_books_inherit_perms_to_bookshelves.php new file mode 100644 index 00000000000..62d2f3aeae2 --- /dev/null +++ b/database/migrations/2024_09_12_204800_add_new_books_inherit_perms_to_bookshelves.php @@ -0,0 +1,28 @@ +boolean('new_books_inherit_perms')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('bookshelves', function (Blueprint $table) { + $table->dropColumn('new_books_inherit_perms'); + }); + } +}; diff --git a/lang/en/entities.php b/lang/en/entities.php index 9e620b24ed1..e3f6dd2cea4 100644 --- a/lang/en/entities.php +++ b/lang/en/entities.php @@ -97,6 +97,7 @@ 'shelves_books' => 'Books on this shelf', 'shelves_add_books' => 'Add books to this shelf', 'shelves_drag_books' => 'Drag books below to add them to this shelf', + 'shelves_new_books_inherit_perms' => 'Should books created under this shelf inherit it\'s permissions?', 'shelves_empty_contents' => 'This shelf has no books assigned to it', 'shelves_edit_and_assign' => 'Edit shelf to assign books', 'shelves_edit_named' => 'Edit Shelf :name', diff --git a/lang/en/settings.php b/lang/en/settings.php index f4c84092c6f..4bdb1abde39 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -46,6 +46,9 @@ 'app_disable_comments' => 'Disable Comments', 'app_disable_comments_toggle' => 'Disable comments', 'app_disable_comments_desc' => 'Disables comments across all pages in the application.
Existing comments are not shown.', + 'app_new_books_inherit_perms' => 'New Books Inherit Permissions', + 'app_new_books_inherit_perms_toggle' => 'New books inherit permissions', + 'app_new_books_inherit_perms_desc' => 'Enabling this option will cause the bookshelve\'s permissions to be copied to any new book created under the shelf. This fixes the issue where someone creates a book, but cannot access it.
This can be overriden on an individual bookshelf.', // Color settings 'color_scheme' => 'Application Color Scheme', diff --git a/resources/sass/_forms.scss b/resources/sass/_forms.scss index a6f890e3cc4..8fb27e4a0b1 100644 --- a/resources/sass/_forms.scss +++ b/resources/sass/_forms.scss @@ -239,6 +239,10 @@ select { @include rtl { background-position: 20px 70%; } + + &.small { + width: 170px; + } } input[type=date] { diff --git a/resources/views/settings/features.blade.php b/resources/views/settings/features.blade.php index 5935e21f549..f7221f8aef0 100644 --- a/resources/views/settings/features.blade.php +++ b/resources/views/settings/features.blade.php @@ -56,6 +56,20 @@ +
+
+ +

{!! trans('settings.app_new_books_inherit_perms_desc') !!}

+
+
+ @include('form.toggle-switch', [ + 'name' => 'setting-app-new-books-inherit-perms', + 'value' => setting('app-new-books-inherit-perms'), + 'label' => trans('settings.app_new_books_inherit_perms_toggle'), + ]) +
+
+ diff --git a/resources/views/shelves/parts/form.blade.php b/resources/views/shelves/parts/form.blade.php index a75dd6ac1b5..a37fb7e71e6 100644 --- a/resources/views/shelves/parts/form.blade.php +++ b/resources/views/shelves/parts/form.blade.php @@ -84,6 +84,24 @@ class="scroll-box"> +@if($edit_perms) +
+ {{ trans('entities.shelves_new_books_inherit_perms') }} + + @if($errors->has('new_books_inherit_perms')) +
{{ $errors->first('new_books_inherit_perms') }}
+ @endif +
+@endif +
{{ trans('common.cancel') }}