]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CopyShelfPermissions.php
Fix timestamp in API docs example response
[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\Tools\PermissionsUpdater;
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     protected PermissionsUpdater $permissionsUpdater;
29
30     /**
31      * Create a new command instance.
32      *
33      * @return void
34      */
35     public function __construct(PermissionsUpdater $permissionsUpdater)
36     {
37         $this->permissionsUpdater = $permissionsUpdater;
38         parent::__construct();
39     }
40
41     /**
42      * Execute the console command.
43      *
44      * @return mixed
45      */
46     public function handle()
47     {
48         $shelfSlug = $this->option('slug');
49         $cascadeAll = $this->option('all');
50         $shelves = null;
51
52         if (!$cascadeAll && !$shelfSlug) {
53             $this->error('Either a --slug or --all option must be provided.');
54
55             return;
56         }
57
58         if ($cascadeAll) {
59             $continue = $this->confirm(
60                 'Permission settings for all shelves will be cascaded. ' .
61                         'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
62                         'Are you sure you want to proceed?'
63             );
64
65             if (!$continue && !$this->hasOption('no-interaction')) {
66                 return;
67             }
68
69             $shelves = Bookshelf::query()->get(['id']);
70         }
71
72         if ($shelfSlug) {
73             $shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id']);
74             if ($shelves->count() === 0) {
75                 $this->info('No shelves found with the given slug.');
76             }
77         }
78
79         foreach ($shelves as $shelf) {
80             $this->permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
81             $this->info('Copied permissions for shelf [' . $shelf->id . ']');
82         }
83
84         $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
85     }
86 }