tissue/app/Console/Commands/PromoteUser.php

62 lines
1.2 KiB
PHP
Raw Normal View History

2019-02-11 14:46:43 +09:00
<?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');
2019-03-14 00:05:11 +09:00
2019-02-11 14:46:43 +09:00
return 1;
}
if ($user->is_admin) {
$this->info('@' . $user->name . ' is already an administrator.');
2019-03-14 00:05:11 +09:00
2019-02-11 14:46:43 +09:00
return 0;
}
$user->is_admin = true;
if ($user->save()) {
$this->info('@' . $user->name . ' is an administrator now.');
} else {
$this->error('Something happened.');
}
}
}