3 namespace BookStack\Console\Commands;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Queries\BookshelfQueries;
7 use BookStack\Entities\Tools\PermissionsUpdater;
8 use Illuminate\Console\Command;
10 class CopyShelfPermissionsCommand extends Command
13 * The name and signature of the console command.
17 protected $signature = 'bookstack:copy-shelf-permissions
18 {--a|all : Perform for all shelves in the system}
19 {--s|slug= : The slug for a shelf to target}
23 * The console command description.
27 protected $description = 'Copy shelf permissions to all child books';
30 * Execute the console command.
32 public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries $queries): int
34 $shelfSlug = $this->option('slug');
35 $cascadeAll = $this->option('all');
38 if (!$cascadeAll && !$shelfSlug) {
39 $this->error('Either a --slug or --all option must be provided.');
45 $continue = $this->confirm(
46 'Permission settings for all shelves will be cascaded. ' .
47 'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
48 'Are you sure you want to proceed?'
51 if (!$continue && !$this->hasOption('no-interaction')) {
55 $shelves = $queries->start()->get(['id']);
59 $shelves = $queries->start()->where('slug', '=', $shelfSlug)->get(['id']);
60 if ($shelves->count() === 0) {
61 $this->info('No shelves found with the given slug.');
65 foreach ($shelves as $shelf) {
66 $permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
67 $this->info('Copied permissions for shelf [' . $shelf->id . ']');
70 $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');