]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CopyShelfPermissionsCommand.php
Queries: Extracted static page,chapter,shelf queries to classes
[bookstack] / app / Console / Commands / CopyShelfPermissionsCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Queries\BookshelfQueries;
7 use BookStack\Entities\Tools\PermissionsUpdater;
8 use Illuminate\Console\Command;
9
10 class CopyShelfPermissionsCommand extends Command
11 {
12     /**
13      * The name and signature of the console command.
14      *
15      * @var string
16      */
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}
20                             ';
21
22     /**
23      * The console command description.
24      *
25      * @var string
26      */
27     protected $description = 'Copy shelf permissions to all child books';
28
29     /**
30      * Execute the console command.
31      */
32     public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries $queries): int
33     {
34         $shelfSlug = $this->option('slug');
35         $cascadeAll = $this->option('all');
36         $shelves = null;
37
38         if (!$cascadeAll && !$shelfSlug) {
39             $this->error('Either a --slug or --all option must be provided.');
40
41             return 1;
42         }
43
44         if ($cascadeAll) {
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?'
49             );
50
51             if (!$continue && !$this->hasOption('no-interaction')) {
52                 return 0;
53             }
54
55             $shelves = $queries->start()->get(['id']);
56         }
57
58         if ($shelfSlug) {
59             $shelves = $queries->start()->where('slug', '=', $shelfSlug)->get(['id']);
60             if ($shelves->count() === 0) {
61                 $this->info('No shelves found with the given slug.');
62             }
63         }
64
65         foreach ($shelves as $shelf) {
66             $permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
67             $this->info('Copied permissions for shelf [' . $shelf->id . ']');
68         }
69
70         $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
71         return 0;
72     }
73 }