Skip to content
Permalink
Browse files

Add page policy

- Fix slug validation bug
- Some small fixes
  • Loading branch information...
dignityinside committed Jun 11, 2019
1 parent 77ee05a commit 04d62c676491745155654144a0c464b0d005e960
@@ -16,17 +16,28 @@
*/
class PagesController extends Controller
{
/**
* PagesController constructor.
*/
public function __construct()
{
$this->authorizeResource(Page::class);
}
/**
* Display a listing of the resource.
*
* @return View
*/
public function index(): View
{
$pages = Page::all()
$pages = Page::where('material_id', 'blog')
->where('mainstream', '1')
->where('status_id', 'public')
->where('mainstream', true)
->where('material_id', 'blog');
->orderBy('created_at', 'desc')
->paginate(config('pages.posts_per_page'));
return view('pages.index')->with('pages', $pages);
}
@@ -30,17 +30,26 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
$rules = [
'category_id' => ['required', 'integer'],
'title' => ['required', 'min:3', 'max:255', 'string'],
'title' => ['required', 'min:3', 'max:255'],
'content' => ['required', 'string'],
'slug' => ['nullable', 'string', 'min:3', 'max:255'],
'status_id' => ['string'],
'slug' => ['nullable', 'alpha_dash', 'min:3', 'max:255'],
'status_id' => ['alpha'],
'owner_id' => ['integer'],
'hits' => ['integer'],
'allow_comments' => ['integer', 'between:0,1'],
'mainstream' => ['integer', 'between:0,1'],
'material_id' => ['string'],
'material_id' => ['alpha'],
];
if ($this->method() === 'PUT' || $this->method() === 'PATCH') {
array_push($rules['slug'], 'unique:pages,slug,' . $this->page->id);
} else {
array_push($rules['slug'], 'unique:pages,slug');
}
return $rules;
}
}
@@ -23,7 +23,7 @@ public function creating(Page $page)
{
if (empty($page->slug)) {
$page->slug = Str::slug($page->title);
$page->slug = $this->generateUniqueSlug($page);
}
$page->owner_id = Auth::id();
@@ -36,7 +36,24 @@ public function updating(Page $page)
{
if (empty($page->slug)) {
$page->slug = Str::slug($page->title);
$page->slug = $this->generateUniqueSlug($page);
}
}
/**
* Generate unique slug
*
* @param Page $page
*
* @return string
*/
private function generateUniqueSlug(Page $page): string
{
$tempSlug = Str::slug($page->title);
$slug = Page::where('slug', $tempSlug)->first();
return !empty($slug) ? $tempSlug . '-' . date('d-m-Y-H-i-s') : $tempSlug;
}
}
@@ -24,7 +24,6 @@ class Page extends Model
'slug',
'content',
'status_id',
'owner_id',
'hits',
'allow_comments',
'mainstream',
@@ -0,0 +1,77 @@
<?php
namespace App\Policies;
use App\User;
use App\Page;
use Illuminate\Auth\Access\HandlesAuthorization;
/**
* Class PagePolicy
*
* @package App\Policies
*
* @author Alexander Schilling <dignityinside@protonmail.com>
*/
class PagePolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the page.
*
* @param User $user
* @param Page $page
*
* @return bool
*/
public function view(?User $user, Page $page): bool
{
if ($page->status_id == 'public') {
return true;
}
if ($user !== null && $page->owner_id === $user->id) {
return true;
}
return false;
}
/**
* Determine whether the user can create pages.
*
* @return bool
*/
public function create(): bool
{
return auth()->check();
}
/**
* Determine whether the user can update the page.
*
* @param User $user
* @param Page $page
*
* @return bool
*/
public function update(User $user, Page $page): bool
{
return $page->owner_id == $user->id;
}
/**
* Determine whether the user can delete the page.
*
* @param User $user
* @param Page $page
*
* @return bool
*/
public function delete(User $user, Page $page): bool
{
return $page->owner_id == $user->id;
}
}
@@ -4,6 +4,7 @@
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\User;
class AuthServiceProvider extends ServiceProvider
{
@@ -13,18 +14,23 @@ class AuthServiceProvider extends ServiceProvider
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
'App\Page' => 'App\Policies\PagePolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
public function boot(Gate $gate)
{
$this->registerPolicies();
//
$gate::before(function (User $user) {
if ($user->isAdmin()) {
return true;
}
});
}
}
@@ -36,4 +36,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* @return bool
*/
public function isAdmin(): bool
{
return (int) $this->is_admin;
}
}
@@ -0,0 +1,17 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Posts per page
|--------------------------------------------------------------------------
|
| How many posts should be shown on PagesController@index.
| Only pages with material id "blog" are relevant.
|
*/
'posts_per_page' => env('POSTS_PER_PAGE', 10),
];
@@ -20,6 +20,10 @@ public function up()
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->enum('is_admin', [0, 1])->default(0);
$table->enum('is_premium', [0, 1])->default(0);
$table->string('github')->nullable();
$table->enum('status', ['active', 'inactive'])->default('inactive');
$table->timestamps();
});
}
@@ -23,7 +23,7 @@ public function up()
$table->bigIncrements('id');
$table->string('title', 255);
$table->string('slug', 255);
$table->string('slug')->unique();
$table->text('content');
$table->enum('status_id', ['draft', 'public'])->default('draft');
$table->unsignedInteger('category_id')->default(0);
@@ -2,13 +2,15 @@

@section('content')

<div class="row">
@foreach($pages as $page)
<div class="post-view">
@foreach($pages as $page)
<div class="row">
<div>
<h3><a href="/page/{{$page->slug}}">{{ $page->title }}</a></h3>
<div>{{ $page->content }}</div>
</div>
@endforeach
</div>
</div>
@endforeach

{{ $pages->links() }}

@endsection
@@ -8,6 +8,12 @@
(Черновик)
@endif

@can('update', $page)
<div>
<a href="/page/{{$page->slug}}/edit">Редактировать</a>
</div>
@endauth

<div>
{{$page->content}}
</div>

0 comments on commit 04d62c6

Please sign in to comment.
You can’t perform that action at this time.