]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CopyShelfPermissionsCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / CopyShelfPermissionsCommand.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\Queries\BookshelfQueries;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use Illuminate\Console\Command;
8
9 class CopyShelfPermissionsCommand 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      * Execute the console command.
30      */
31     public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries $queries): int
32     {
33         $shelfSlug = $this->option('slug');
34         $cascadeAll = $this->option('all');
35         $shelves = null;
36
37         if (!$cascadeAll && !$shelfSlug) {
38             $this->error('Either a --slug or --all option must be provided.');
39
40             return 1;
41         }
42
43         if ($cascadeAll) {
44             $continue = $this->confirm(
45                 'Permission settings for all shelves will be cascaded. ' .
46                         'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
47                         'Are you sure you want to proceed?'
48             );
49
50             if (!$continue && !$this->hasOption('no-interaction')) {
51                 return 0;
52             }
53
54             $shelves = $queries->start()->get(['id']);
55         }
56
57         if ($shelfSlug) {
58             $shelves = $queries->start()->where('slug', '=', $shelfSlug)->get(['id']);
59             if ($shelves->count() === 0) {
60                 $this->info('No shelves found with the given slug.');
61             }
62         }
63
64         foreach ($shelves as $shelf) {
65             $permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
66             $this->info('Copied permissions for shelf [' . $shelf->id . ']');
67         }
68
69         $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
70         return 0;
71     }
72 }