Merge pull request #156 from shikorism/feature/admin
お知らせ管理機能 (+ 管理者用画面)
This commit is contained in:
61
app/Console/Commands/DemoteUser.php
Normal file
61
app/Console/Commands/DemoteUser.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
}
|
61
app/Console/Commands/PromoteUser.php
Normal file
61
app/Console/Commands/PromoteUser.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?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;
|
||||
|
||||
use App\Console\Commands\DemoteUser;
|
||||
use App\Console\Commands\PromoteUser;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@@ -35,6 +37,8 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function commands()
|
||||
{
|
||||
$this->load(__DIR__.'/Commands');
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
}
|
||||
|
14
app/Http/Controllers/Admin/DashboardController.php
Normal file
14
app/Http/Controllers/Admin/DashboardController.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('admin.dashboard');
|
||||
}
|
||||
}
|
75
app/Http/Controllers/Admin/InfoController.php
Normal file
75
app/Http/Controllers/Admin/InfoController.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AdminInfoStoreRequest;
|
||||
use App\Information;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class InfoController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$informations = Information::query()
|
||||
->select('id', 'category', 'pinned', 'title', 'created_at')
|
||||
->orderByDesc('pinned')
|
||||
->orderByDesc('created_at')
|
||||
->paginate(20);
|
||||
|
||||
return view('admin.info.index')->with([
|
||||
'informations' => $informations,
|
||||
'categories' => Information::CATEGORIES
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('admin.info.create')->with([
|
||||
'categories' => Information::CATEGORIES
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(AdminInfoStoreRequest $request)
|
||||
{
|
||||
$inputs = $request->all();
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
$info = Information::create($inputs);
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$information = Information::findOrFail($id);
|
||||
|
||||
return view('admin.info.edit')->with([
|
||||
'info' => $information,
|
||||
'categories' => Information::CATEGORIES
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(AdminInfoStoreRequest $request, Information $info)
|
||||
{
|
||||
$inputs = $request->all();
|
||||
if (!$request->has('pinned')) {
|
||||
$inputs['pinned'] = false;
|
||||
}
|
||||
|
||||
$info->fill($inputs)->save();
|
||||
|
||||
return redirect()->route('admin.info.edit', ['info' => $info])->with('status', 'お知らせを更新しました。');
|
||||
}
|
||||
|
||||
public function destroy(Information $info)
|
||||
{
|
||||
$info->delete();
|
||||
|
||||
return redirect()->route('admin.info')->with('status', 'お知らせを削除しました。');
|
||||
}
|
||||
}
|
35
app/Http/Requests/AdminInfoStoreRequest.php
Normal file
35
app/Http/Requests/AdminInfoStoreRequest.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Information;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AdminInfoStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'category' => ['required', Rule::in(array_keys(Information::CATEGORIES))],
|
||||
'pinned' => 'nullable|boolean',
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string|max:10000'
|
||||
];
|
||||
}
|
||||
}
|
@@ -16,5 +16,9 @@ class Information extends Model
|
||||
3 => ['label' => 'メンテナンス', 'class' => 'badge-warning']
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'category', 'pinned', 'title', 'content'
|
||||
];
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
}
|
||||
|
@@ -25,6 +25,8 @@ class AuthServiceProvider extends ServiceProvider
|
||||
{
|
||||
$this->registerPolicies();
|
||||
|
||||
//
|
||||
Gate::define('admin', function ($user) {
|
||||
return $user->is_admin;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user