3 namespace BookStack\Console\Commands;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use Illuminate\Console\Command;
9 class CopyShelfPermissions extends Command
12 * The name and signature of the console command.
16 protected $signature = 'bookstack:copy-shelf-permissions
17 {--a|all : Perform for all shelves in the system}
18 {--s|slug= : The slug for a shelf to target}
22 * The console command description.
26 protected $description = 'Copy shelf permissions to all child books';
28 protected PermissionsUpdater $permissionsUpdater;
31 * Create a new command instance.
35 public function __construct(PermissionsUpdater $permissionsUpdater)
37 $this->permissionsUpdater = $permissionsUpdater;
38 parent::__construct();
42 * Execute the console command.
46 public function handle()
48 $shelfSlug = $this->option('slug');
49 $cascadeAll = $this->option('all');
52 if (!$cascadeAll && !$shelfSlug) {
53 $this->error('Either a --slug or --all option must be provided.');
59 $continue = $this->confirm(
60 'Permission settings for all shelves will be cascaded. ' .
61 'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
62 'Are you sure you want to proceed?'
65 if (!$continue && !$this->hasOption('no-interaction')) {
69 $shelves = Bookshelf::query()->get(['id']);
73 $shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id']);
74 if ($shelves->count() === 0) {
75 $this->info('No shelves found with the given slug.');
79 foreach ($shelves as $shelf) {
80 $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
81 $this->info('Copied permissions for shelf [' . $shelf->id . ']');
84 $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');