2019-12-11 16:21:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2022-10-09 11:36:03 -04:00
|
|
|
use BookStack\Entities\Tools\PermissionsUpdater;
|
2019-12-11 16:21:19 -05:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class CopyShelfPermissions extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'bookstack:copy-shelf-permissions
|
|
|
|
{--a|all : Perform for all shelves in the system}
|
|
|
|
{--s|slug= : The slug for a shelf to target}
|
|
|
|
';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-09 11:58:40 -04:00
|
|
|
protected $description = 'Copy shelf permissions to all child books';
|
2019-12-11 16:21:19 -05:00
|
|
|
|
2022-10-09 11:36:03 -04:00
|
|
|
protected PermissionsUpdater $permissionsUpdater;
|
2019-12-11 16:21:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-10-09 11:36:03 -04:00
|
|
|
public function __construct(PermissionsUpdater $permissionsUpdater)
|
2019-12-11 16:21:19 -05:00
|
|
|
{
|
2022-10-09 11:36:03 -04:00
|
|
|
$this->permissionsUpdater = $permissionsUpdater;
|
2019-12-11 16:21:19 -05:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$shelfSlug = $this->option('slug');
|
|
|
|
$cascadeAll = $this->option('all');
|
|
|
|
$shelves = null;
|
|
|
|
|
|
|
|
if (!$cascadeAll && !$shelfSlug) {
|
|
|
|
$this->error('Either a --slug or --all option must be provided.');
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-12-11 16:21:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cascadeAll) {
|
|
|
|
$continue = $this->confirm(
|
2021-06-26 11:23:15 -04:00
|
|
|
'Permission settings for all shelves will be cascaded. ' .
|
|
|
|
'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
|
2019-12-11 16:21:19 -05:00
|
|
|
'Are you sure you want to proceed?'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$continue && !$this->hasOption('no-interaction')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:58:26 -04:00
|
|
|
$shelves = Bookshelf::query()->get(['id']);
|
2019-12-11 16:21:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($shelfSlug) {
|
2022-10-10 11:58:26 -04:00
|
|
|
$shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id']);
|
2019-12-11 16:21:19 -05:00
|
|
|
if ($shelves->count() === 0) {
|
|
|
|
$this->info('No shelves found with the given slug.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($shelves as $shelf) {
|
2022-10-09 11:36:03 -04:00
|
|
|
$this->permissionsUpdater->updateBookPermissionsFromShelf($shelf, false);
|
2019-12-11 16:21:19 -05:00
|
|
|
$this->info('Copied permissions for shelf [' . $shelf->id . ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Permissions copied for ' . $shelves->count() . ' shelves.');
|
|
|
|
}
|
|
|
|
}
|