Skip to content

Commit

Permalink
feat: 23. Authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
mfl4 committed Dec 13, 2023
1 parent 204bb9b commit 18f34c3
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 31 deletions.
68 changes: 68 additions & 0 deletions app/Http/Controllers/AdminCategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;

class AdminCategoryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$this->authorize('admin');
return view('dashboard.categories.index', [
'categories' => Category::all(),
]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{

}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(Category $category)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Category $category)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Category $category)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Category $category)
{
//
}
}
3 changes: 2 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Kernel extends HttpKernel

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Expand All @@ -64,5 +64,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'admin' => \App\Http\Middleware\IsAdmin::class,
];
}
23 changes: 23 additions & 0 deletions app/Http/Middleware/IsAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (!auth()->check() || !auth()->user()->is_admin) {
abort(403);
}
return $next($request);
}
}
5 changes: 5 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -21,5 +22,9 @@ public function register(): void
public function boot(): void
{
Paginator::useBootstrapFive();

Gate::define('admin', function ($user) {
return $user->is_admin;
});
}
}
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('is_admin');
});
}
};
4 changes: 2 additions & 2 deletions resources/views/categories.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<div class="container">
<div class="row">
@foreach ($categories as $category)
<div class="col-md-4">
<div class="col-md-4 g-4">
<a href="/posts?category={{ $category->slug }}">
<div class="card text-bg-dark border-2 border-primary">
<img src="https://source.unsplash.com/500x500?{{ $category->name }}" class="card-img"
alt="...">
<div class="card-img-overlay d-flex align-items-center p-0">
<h5 class="card-title text-center flex-fill p-5 fs-3"
style="background-color: rgba(13, 110, 253, 0.7) ">
style="background-color: rgba(13, 110, 253, 0.6)">
{{ $category->name }}</h5>
</div>
</div>
Expand Down
47 changes: 47 additions & 0 deletions resources/views/dashboard/categories/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@extends('dashboard.layouts.main')

@section('container')
<h1>Categories Management</h1>
<hr class="my-3 border-5 border-black">

@if (session()->has('success'))
<div class="alert alert-success alert-dismissible fade show col-lg-8" role="alert">
{{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif

<div class="table-responsive col-lg-6">
<a href="/dashboard/categories/create" class="btn btn-outline-primary mb-3"><i class="bi bi-plus"></i> Create New
Category</a>
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Category Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $category->name }}</td>
<td>
<a href="/dashboard/categories/{{ $category->slug }}" class="btn btn-outline-info"><i
class="bi bi-eye"></i></a>
<a href="/dashboard/categories/{{ $category->slug }}/edit" class="btn btn-outline-warning"><i
class="bi bi-pencil-square"></i></a>
<form action="/dashboard/categories/{{ $category->slug }}" method="post" class="d-inline">
@method('delete')
@csrf
<button class="btn btn-outline-danger" onclick="return confirm('Are you sure?')"><i
class="bi bi-trash"></i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
11 changes: 6 additions & 5 deletions resources/views/partials/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link {{ $active === 'home' ? 'active' : '' }}" aria-current="page"
<a class="nav-link {{ request()->is('/') ? 'active' : '' }}" aria-current="page"
href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link {{ $active === 'about' ? 'active' : '' }}" href="/about">About</a>
<a class="nav-link {{ request()->is('about') ? 'active' : '' }}" href="/about">About</a>
</li>
<li class="nav-item">
<a class="nav-link {{ $active === 'posts' ? 'active' : '' }}" href="/posts">Blog</a>
<a class="nav-link {{ request()->is('posts*') ? 'active' : '' }}" href="/posts">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link {{ $active === 'categories' ? 'active' : '' }}" href="/categories">Categories</a>
<a class="nav-link {{ request()->is('categories*') ? 'active' : '' }}"
href="/categories">Categories</a>
</li>
</ul>

Expand Down Expand Up @@ -46,7 +47,7 @@
</li>
@else
<li class="nav-item">
<a class="nav-link {{ $active === 'login' ? 'active' : '' }}" href="/login"><i
<a class="nav-link {{ request()->is('login') ? 'active' : '' }}" href="/login"><i
class="bi bi-box-arrow-in-right"></i> Log In</a>
</li>
@endauth
Expand Down
21 changes: 19 additions & 2 deletions resources/views/partials/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<hr class="text-light my-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link text-light {{ request()->is('dashboard') ? 'active' : '' }}" href="/dashboard">
<a class="nav-link text-light {{ Request::is('dashboard') ? 'active' : '' }}" href="/dashboard">
<i class="bi bi-house-door"></i> Home
</a>
</li>
<li class="nav-item">
<a class="nav-link text-light {{ request()->is('dashboard/posts*') ? 'active' : '' }}"
<a class="nav-link text-light {{ Request::is('dashboard/posts*') ? 'active' : '' }}"
href="/dashboard/posts">
<i class="bi bi-file-earmark-text"></i> Post Management
</a>
Expand All @@ -30,5 +30,22 @@
</form>
</li>
</ul>

@can('admin')
<hr class="text-light my-3">
<h6
class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-light text-uppercase">
<span>Admnistration</span>
</h6>
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link text-light {{ Request::is('dashboard/categories*') ? 'active' : '' }}"
href="/dashboard/categories">
<i class="bi bi-tag"></i> Category Management
</a>
</li>
</ul>
@endcan

</div>
</nav>
Loading

0 comments on commit 18f34c3

Please sign in to comment.