管理者昇格/降格 CLIコマンド
This commit is contained in:
parent
e98ed0c3ca
commit
82ccd623a6
59
app/Console/Commands/DemoteUser.php
Normal file
59
app/Console/Commands/DemoteUser.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class DemoteUser extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'tissue:user:demote {username}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Demote admin to user';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$user = User::where('name', $this->argument('username'))->first();
|
||||||
|
if ($user === null) {
|
||||||
|
$this->error('No user with such username');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$user->is_admin) {
|
||||||
|
$this->info('@' . $user->name . ' is already an user.');
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->is_admin = false;
|
||||||
|
if ($user->save()) {
|
||||||
|
$this->info('@' . $user->name . ' is an user now.');
|
||||||
|
} else {
|
||||||
|
$this->error('Something happened.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
59
app/Console/Commands/PromoteUser.php
Normal file
59
app/Console/Commands/PromoteUser.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class PromoteUser extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'tissue:user:promote {username}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Promote user to admin';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$user = User::where('name', $this->argument('username'))->first();
|
||||||
|
if ($user === null) {
|
||||||
|
$this->error('No user with such username');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user->is_admin) {
|
||||||
|
$this->info('@' . $user->name . ' is already an administrator.');
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->is_admin = true;
|
||||||
|
if ($user->save()) {
|
||||||
|
$this->info('@' . $user->name . ' is an administrator now.');
|
||||||
|
} else {
|
||||||
|
$this->error('Something happened.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Console;
|
namespace App\Console;
|
||||||
|
|
||||||
|
use App\Console\Commands\DemoteUser;
|
||||||
|
use App\Console\Commands\PromoteUser;
|
||||||
use Illuminate\Console\Scheduling\Schedule;
|
use Illuminate\Console\Scheduling\Schedule;
|
||||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||||
|
|
||||||
@ -35,6 +37,8 @@ class Kernel extends ConsoleKernel
|
|||||||
*/
|
*/
|
||||||
protected function commands()
|
protected function commands()
|
||||||
{
|
{
|
||||||
|
$this->load(__DIR__.'/Commands');
|
||||||
|
|
||||||
require base_path('routes/console.php');
|
require base_path('routes/console.php');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user