Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option for new books to inherit permissions when created #5205

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Entities/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@ public function __construct(
protected BookQueries $queries,
protected BookshelfQueries $shelfQueries,
protected ReferenceFetcher $referenceFetcher,
protected SettingService $settingService,
) {
}

Expand Down Expand Up @@ -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());
Expand Down
19 changes: 15 additions & 4 deletions app/Entities/Controllers/BookshelfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Exception;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Rule;

class BookshelfController extends Controller
{
Expand Down Expand Up @@ -156,6 +157,7 @@ public function edit(string $slug)
return view('shelves.edit', [
'shelf' => $shelf,
'books' => $books,
'edit_perms' => userCan('restrictions-manage', $shelf),
]);
}

Expand All @@ -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')) {
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion app/Entities/Models/Bookshelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('bookshelves', function (Blueprint $table) {
$table->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');
});
}
};
1 change: 1 addition & 0 deletions lang/en/entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br> 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.<br>This can be overriden on an individual bookshelf.',

// Color settings
'color_scheme' => 'Application Color Scheme',
Expand Down
4 changes: 4 additions & 0 deletions resources/sass/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ select {
@include rtl {
background-position: 20px 70%;
}

&.small {
width: 170px;
}
}

input[type=date] {
Expand Down
14 changes: 14 additions & 0 deletions resources/views/settings/features.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@
</div>
</div>

<div class="grid half gap-xl">
<div>
<label class="setting-list-label">{{ trans('settings.app_new_books_inherit_perms') }}</label>
<p class="small">{!! trans('settings.app_new_books_inherit_perms_desc') !!}</p>
</div>
<div>
@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'),
])
</div>
</div>


</div>

Expand Down
18 changes: 18 additions & 0 deletions resources/views/shelves/parts/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ class="scroll-box">
</div>
</div>

@if($edit_perms)
<div class="form-group">
{{ trans('entities.shelves_new_books_inherit_perms') }}
<select
name="new_books_inherit_perms"
id="new_books_inherit_perms"
class="small"
>
<option value="null" @if(old("new_books_inherit_perms") === null || $shelf->new_books_inherit_perms === null) selected @endif> Use global setting </option>
<option value="true" @if(old("new_books_inherit_perms") || $shelf->new_books_inherit_perms) selected @endif> Yes </option>
<option value="false" @if(old("new_books_inherit_perms") === 0 || $shelf->new_books_inherit_perms === 0) selected @endif> No </option>
</select>
@if($errors->has('new_books_inherit_perms'))
<div class="text-neg text-small">{{ $errors->first('new_books_inherit_perms') }}</div>
@endif
</div>
@endif

<div class="form-group text-right">
<a href="{{ isset($shelf) ? $shelf->getUrl() : url('/shelves') }}" class="button outline">{{ trans('common.cancel') }}</a>
<button type="submit" class="button">{{ trans('entities.shelves_save') }}</button>
Expand Down