]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CopyShelfPermissions.php
Replaced embeds with images in exports
[bookstack] / app / Console / Commands / CopyShelfPermissions.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use Illuminate\Console\Command;
8
9 class CopyShelfPermissions extends Command
10 {
11     /**
12      * The name and signature of the console command.
13      *
14      * @var string
15      */
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}
19                             ';
20
21     /**
22      * The console command description.
23      *
24      * @var string
25      */
26     protected $description = 'Copy shelf permissions to all child books';
27
28     /**
29      * @var BookshelfRepo
30      */
31     protected $bookshelfRepo;
32
33     /**
34      * Create a new command instance.
35      *
36      * @return void
37      */
38     public function __construct(BookshelfRepo $repo)
39     {
40         $this->bookshelfRepo = $repo;
41         parent::__construct();
42     }
43
44     /**
45      * Execute the console command.
46      *
47      * @return mixed
48      */
49     public function handle()
50     {
51         $shelfSlug = $this->option('slug');
52         $cascadeAll = $this->option('all');
53         $shelves = null;
54
55         if (!$cascadeAll && !$shelfSlug) {
56             $this->error('Either a --slug or --all option must be provided.');
57
58             return;
59         }
60
61         if ($cascadeAll) {
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?'
66             );
67
68             if (!$continue && !$this->hasOption('no-interaction')) {
69                 return;
70             }
71
72             $shelves = Bookshelf::query()->get(['id', 'restricted']);
73         }
74
75         if ($shelfSlug) {
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.');
79             }
80         }
81
82         foreach ($shelves as $shelf) {
83             $this->bookshelfRepo->copyDownPermissions($shelf, false);
84             $this->info('Copied permissions for shelf [' . $shelf->id . ']');
85         }
86
87         $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
88     }
89 }