3 namespace BookStack\Console\Commands;
5 use BookStack\Entities\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.');
61 $continue = $this->confirm(
62 'Permission settings for all shelves will be cascaded. '.
63 'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. '.
64 'Are you sure you want to proceed?'
67 if (!$continue && !$this->hasOption('no-interaction')) {
71 $shelves = Bookshelf::query()->get(['id', 'restricted']);
75 $shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id', 'restricted']);
76 if ($shelves->count() === 0) {
77 $this->info('No shelves found with the given slug.');
81 foreach ($shelves as $shelf) {
82 $this->bookshelfRepo->copyDownPermissions($shelf, false);
83 $this->info('Copied permissions for shelf [' . $shelf->id . ']');
86 $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');