]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/CopyShelfPermissions.php
Allow book, shelf, settings & profile form input validation to skip image
[bookstack] / app / Console / Commands / CopyShelfPermissions.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Entities\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             return;
58         }
59
60         if ($cascadeAll) {
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?'
65             );
66
67             if (!$continue && !$this->hasOption('no-interaction')) {
68                 return;
69             }
70
71             $shelves = Bookshelf::query()->get(['id', 'restricted']);
72         }
73
74         if ($shelfSlug) {
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.');
78             }
79         }
80
81         foreach ($shelves as $shelf) {
82             $this->bookshelfRepo->copyDownPermissions($shelf, false);
83             $this->info('Copied permissions for shelf [' . $shelf->id . ']');
84         }
85
86         $this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
87     }
88 }