3 namespace BookStack\Console\Commands;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
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';
31 protected $bookshelfRepo;
34 * Create a new command instance.
38 public function __construct(BookshelfRepo $repo)
40 $this->bookshelfRepo = $repo;
41 parent::__construct();
45 * Execute the console command.
49 public function handle()
51 $shelfSlug = $this->option('slug');
52 $cascadeAll = $this->option('all');
55 if (!$cascadeAll && !$shelfSlug) {
56 $this->error('Either a --slug or --all option must be provided.');
62 $continue = $this->confirm(
63 'Permission settings for all shelves will be cascaded. ' .
64 'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
65 'Are you sure you want to proceed?'
68 if (!$continue && !$this->hasOption('no-interaction')) {
72 $shelves = Bookshelf::query()->get(['id', 'restricted']);
76 $shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id', 'restricted']);
77 if ($shelves->count() === 0) {
78 $this->info('No shelves found with the given slug.');
82 foreach ($shelves as $shelf) {
83 $this->bookshelfRepo->copyDownPermissions($shelf, false);
84 $this->info('Copied permissions for shelf [' . $shelf->id . ']');
87 $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');