Merge pull request #156 from shikorism/feature/admin

お知らせ管理機能 (+ 管理者用画面)
This commit is contained in:
shibafu 2019-03-17 20:14:41 +09:00 committed by GitHub
commit b8ceac51f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 509 additions and 1 deletions

View 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.');
}
}
}

View 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.');
}
}
}

View File

@ -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');
} }
} }

View 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');
}
}

View 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', 'お知らせを削除しました。');
}
}

View 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'
];
}
}

View File

@ -16,5 +16,9 @@ class Information extends Model
3 => ['label' => 'メンテナンス', 'class' => 'badge-warning'] 3 => ['label' => 'メンテナンス', 'class' => 'badge-warning']
]; ];
protected $fillable = [
'category', 'pinned', 'title', 'content'
];
protected $dates = ['deleted_at']; protected $dates = ['deleted_at'];
} }

View File

@ -25,6 +25,8 @@ class AuthServiceProvider extends ServiceProvider
{ {
$this->registerPolicies(); $this->registerPolicies();
// Gate::define('admin', function ($user) {
return $user->is_admin;
});
} }
} }

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIsAdminToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_admin');
});
}
}

BIN
public/dashboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

View File

@ -119,6 +119,8 @@ return [
'attributes' => [ 'attributes' => [
'email' => 'メールアドレス', 'email' => 'メールアドレス',
'password' => 'パスワード', 'password' => 'パスワード',
'title' => 'タイトル',
'content' => '本文',
], ],
]; ];

View File

@ -0,0 +1,10 @@
@extends('layouts.admin')
@section('title', 'ダッシュボード')
@section('tab-content')
<div class="container d-flex flex-column align-items-center">
<img src="{{ asset('dashboard.png') }}" class="w-50"/>
<p class="text-muted">TODO: 役に立つ情報を表示する</p>
</div>
@endsection

View File

@ -0,0 +1,54 @@
@extends('layouts.admin')
@section('title', 'お知らせ')
@section('tab-content')
<div class="container">
<h2>お知らせの作成</h2>
<hr>
<form action="{{ route('admin.info.store') }}" method="post">
{{ csrf_field() }}
<div class="row">
<div class="form-group col-12 col-lg-6">
<label for="category">カテゴリ</label>
<select id="category" name="category" class="form-control">
@foreach($categories as $id => $category)
<option value="{{ $id }}" {{ old('category') == $id ? 'selected' : '' }}>{{ $category['label'] }}</option>
@endforeach
</select>
</div>
<div class="form-group col-12 col-lg-6 d-flex flex-column justify-content-center">
<div class="custom-control custom-checkbox">
<input id="pinned" name="pinned" type="checkbox" class="custom-control-input" value="1"
{{ old('pinned') ? 'checked' : ''}}>
<label for="pinned" class="custom-control-label">ピン留め (常に優先表示) する</label>
</div>
</div>
</div>
<div class="form-group">
<label for="title">タイトル</label>
<input id="title" name="title" type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}">
@if ($errors->has('title'))
<div class="invalid-feedback">{{ $errors->first('title') }}</div>
@endif
</div>
<div class="form-group mt-3">
<label for="content">本文</label>
<textarea id="content" name="content" rows="15" class="form-control {{ $errors->has('content') ? ' is-invalid' : '' }}" maxlength="10000">{{ old('content') }}</textarea>
<small class="form-text text-muted">
最大 10000 文字、Markdown 形式
</small>
@if ($errors->has('content'))
<div class="invalid-feedback">{{ $errors->first('content') }}</div>
@endif
</div>
<div class="d-flex justify-content-between">
<button type="submit" class="btn btn-primary">登録</button>
</div>
</form>
</div>
@endsection

View File

@ -0,0 +1,60 @@
@extends('layouts.admin')
@section('title', 'お知らせ')
@section('tab-content')
<div class="container">
<h2>お知らせの編集</h2>
<hr>
<form action="{{ route('admin.info.update', ['info' => $info]) }}" method="post">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div class="row">
<div class="form-group col-12 col-lg-6">
<label for="category">カテゴリ</label>
<select id="category" name="category" class="form-control">
@foreach($categories as $id => $category)
<option value="{{ $id }}" {{ (old('category') ?? $info->category) == $id ? 'selected' : '' }}>{{ $category['label'] }}</option>
@endforeach
</select>
</div>
<div class="form-group col-12 col-lg-6 d-flex flex-column justify-content-center">
<div class="custom-control custom-checkbox">
<input id="pinned" name="pinned" type="checkbox" class="custom-control-input" value="1"
{{ (is_bool(old('pinned')) ? old('pinned') : $info->pinned) ? 'checked' : ''}}>
<label for="pinned" class="custom-control-label">ピン留め (常に優先表示) する</label>
</div>
</div>
</div>
<div class="form-group">
<label for="title">タイトル</label>
<input id="title" name="title" type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') ?? $info->title }}">
@if ($errors->has('title'))
<div class="invalid-feedback">{{ $errors->first('title') }}</div>
@endif
</div>
<div class="form-group mt-3">
<label for="content">本文</label>
<textarea id="content" name="content" rows="15" class="form-control {{ $errors->has('content') ? ' is-invalid' : '' }}" maxlength="10000">{{ old('content') ?? $info->content }}</textarea>
<small class="form-text text-muted">
最大 10000 文字、Markdown 形式
</small>
@if ($errors->has('content'))
<div class="invalid-feedback">{{ $errors->first('content') }}</div>
@endif
</div>
<div class="d-flex justify-content-between">
<button type="submit" class="btn btn-primary">更新</button>
<button type="submit" class="btn btn-danger" form="delete-form">削除</button>
</div>
</form>
<form id="delete-form" action="{{ route('admin.info.destroy', ['info' => $info]) }}" method="post">
{{ method_field('DELETE') }}
{{ csrf_field() }}
</form>
</div>
@endsection

View File

@ -0,0 +1,57 @@
@extends('layouts.admin')
@section('title', 'お知らせ')
@section('tab-content')
<div class="container">
<h2>お知らせ</h2>
<hr>
<div class="d-flex mb-3">
<a href="{{ route('admin.info.create') }}" class="btn btn-primary">新規作成</a>
</div>
<table class="table table-sm">
<thead>
<tr>
<th>カテゴリ</th>
<th>タイトル</th>
<th>作成日</th>
</tr>
</thead>
<tbody>
@foreach($informations as $info)
<tr>
<td>
@if ($info->pinned)
<span class="badge badge-secondary"><span class="oi oi-pin"></span>ピン留め</span>
@endif
<span class="badge {{ $categories[$info->category]['class'] }}">{{ $categories[$info->category]['label'] }}</span>
</td>
<td>
<a href="{{ route('admin.info.edit', ['id' => $info->id]) }}">{{ $info->title }}</a>
</td>
<td>
{{ $info->created_at->format('Y年n月j日') }}
</td>
</tr>
@endforeach
</tbody>
</table>
<ul class="pagination mt-4 justify-content-center">
<li class="page-item {{ $informations->currentPage() === 1 ? 'disabled' : '' }}">
<a class="page-link" href="{{ $informations->previousPageUrl() }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
@for ($i = 1; $i <= $informations->lastPage(); $i++)
<li class="page-item {{ $i === $informations->currentPage() ? 'active' : '' }}"><a href="{{ $informations->url($i) }}" class="page-link">{{ $i }}</a></li>
@endfor
<li class="page-item {{ $informations->currentPage() === $informations->lastPage() ? 'disabled' : '' }}">
<a class="page-link" href="{{ $informations->nextPageUrl() }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</div>
@endsection

View File

@ -0,0 +1,20 @@
@extends('layouts.base')
@section('content')
<div class="container">
<div class="row">
<div class="col-lg-3">
<div class="list-group mb-4">
<div class="list-group-item disabled font-weight-bold">管理</div>
<a class="list-group-item list-group-item-action {{ Route::is('admin.dashboard') ? 'active' : '' }}"
href="{{ route('admin.dashboard') }}"><span class="oi oi-dashboard mr-1"></span> ダッシュボード</a>
<a class="list-group-item list-group-item-action {{ Route::is('admin.info*') ? 'active' : '' }}"
href="{{ route('admin.info') }}"><span class="oi oi-bullhorn mr-1"></span> お知らせ</a>
</div>
</div>
<div class="tab-content col-lg-9">
@yield('tab-content')
</div>
</div>
</div>
@endsection

View File

@ -105,6 +105,9 @@
</a> </a>
<div class="dropdown-divider"></div> <div class="dropdown-divider"></div>
<a href="{{ route('setting') }}" class="dropdown-item">設定</a> <a href="{{ route('setting') }}" class="dropdown-item">設定</a>
@can ('admin')
<a href="{{ route('admin.dashboard') }}" class="dropdown-item">管理</a>
@endcan
<a href="{{ route('logout') }}" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">ログアウト</a> <a href="{{ route('logout') }}" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">ログアウト</a>
</div> </div>
</li> </li>

View File

@ -44,3 +44,17 @@ Route::get('/info/{id}', 'InfoController@show')->where('id', '[0-9]+')->name('in
Route::redirect('/search', '/search/checkin', 301); Route::redirect('/search', '/search/checkin', 301);
Route::get('/search/checkin', 'SearchController@index')->name('search'); Route::get('/search/checkin', 'SearchController@index')->name('search');
Route::get('/search/related-tag', 'SearchController@relatedTag')->name('search.related-tag'); Route::get('/search/related-tag', 'SearchController@relatedTag')->name('search.related-tag');
Route::middleware('can:admin')
->namespace('Admin')
->prefix('admin')
->name('admin.')
->group(function () {
Route::get('/', 'DashboardController@index')->name('dashboard');
Route::get('/info', 'InfoController@index')->name('info');
Route::get('/info/create', 'InfoController@create')->name('info.create');
Route::post('/info', 'InfoController@store')->name('info.store');
Route::get('/info/{info}', 'InfoController@edit')->name('info.edit');
Route::put('/info/{info}', 'InfoController@update')->name('info.update');
Route::delete('/info/{info}', 'InfoController@destroy')->name('info.destroy');
});