Skip to content

Commit

Permalink
authorize news section
Browse files Browse the repository at this point in the history
  • Loading branch information
IsharaEkanayaka committed Jul 15, 2024
1 parent 8bbedc2 commit 63bc7ab
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
10 changes: 8 additions & 2 deletions app/Http/Livewire/Backend/NewsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Livewire\Backend;

use Illuminate\Support\Facades\Gate;
use App\Domains\News\Models\News;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
Expand All @@ -17,7 +18,7 @@ class NewsTable extends DataTableComponent

public function columns(): array
{
return [
$columns = [
Column::make("Title", "title")
->sortable()
->searchable(),
Expand All @@ -33,8 +34,13 @@ public function columns(): array
->sortable(),
Column::make("Updated At", "updated_at")
->sortable(),
Column::make("Actions")
];

if (Gate::allows('edit-or-delete-news')) {
$columns[] = Column::make("Actions");
}

return $columns;
}

public function query(): Builder
Expand Down
10 changes: 10 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,15 @@ public function boot()
// Gate::after(function ($user) {
// return $user->hasAllAccess();
// });

Gate::define('edit-or-delete-news', function ($user) {
// Logic to determine if the user can edit news
return $user->hasAnyPermission(['admin.access.news.edit','admin.access.news.delete']);
});

Gate::define('edit-or-delete-events', function ($user) {
// Logic to determine if the user can edit news
return $user->hasAnyPermission(['admin.access.events.edit','admin.access.events.delete']);
});
}
}
4 changes: 2 additions & 2 deletions resources/views/backend/includes/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class="c-sidebar-nav-dropdown {{ activeClass(Route::is('admin.auth.user.*') || R
</li>
@endif

@if ($logged_in_user->hasPermissionTo('admin.access.news.edit'))
@if ($logged_in_user->hasAnyPermission(['admin.access.news.create', 'admin.access.news.edit', 'admin.access.news.show', 'admin.access.news.delete']))
{{-- News --}}
<li class="c-sidebar-nav-dropdown">
<x-utils.link href="#" icon="c-sidebar-nav-icon cil-newspaper" class="c-sidebar-nav-dropdown-toggle"
Expand All @@ -99,7 +99,7 @@ class="c-sidebar-nav-dropdown {{ activeClass(Route::is('admin.auth.user.*') || R
</li>
@endif

@if ($logged_in_user->hasPermissionTo('admin.access.events.edit'))
@if ($logged_in_user->hasAnyPermission(['admin.access.events.create', 'admin.access.events.edit', 'admin.access.events.show', 'admin.access.events.delete']))
{{-- Events --}}
<li class="c-sidebar-nav-dropdown">
<x-utils.link href="#" icon="c-sidebar-nav-icon cil-browser" class="c-sidebar-nav-dropdown-toggle"
Expand Down
4 changes: 4 additions & 0 deletions resources/views/backend/news/index-table-row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@
<x-livewire-tables::table.cell>
<div class="d-flex px-0 mt-0 mb-0">
<div class="btn-group" role="group" aria-label="">
@if ($logged_in_user->hasPermissionTo('admin.access.news.edit'))
<a href="{{ route('dashboard.news.edit', $row) }}" class="btn btn-info btn-xs"><i class="fa fa-pencil"
title="Edit"></i>
</a>
@endif
@if ($logged_in_user->hasPermissionTo('admin.access.news.delete'))
<a href="{{ route('dashboard.news.delete', $row) }}" class="btn btn-danger btn-xs"><i
class="fa fa-trash" title="Delete"></i>
</a>
@endif
</div>
</div>
</x-livewire-tables::table.cell>
11 changes: 9 additions & 2 deletions routes/backend/news.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
use App\Http\Controllers\Backend\NewsController;
use Illuminate\Support\Facades\Route;

Route::group(['middleware' => ['permission:admin.access.news.edit']], function () {
Route::group(['middleware' => ['permission:admin.access.news.show|admin.access.news.create|admin.access.news.edit|admin.access.news.delete']], function () {

Route::get('/news', function () {
return view('backend.news.index');
})->name('news.index')
})->middleware('permission:admin.access.news.show')
->name('news.index')
->breadcrumbs(function (Trail $trail) {
$trail->push(__('Home'), route('dashboard.home'))
->push(__('News'), route('dashboard.news.index'));
});

// Create
Route::get('news/create', [NewsController::class, 'create'])
->middleware('permission:admin.access.news.create')
->name('news.create')
->breadcrumbs(function (Trail $trail) {
$trail->push(__('Home'), route('dashboard.home'))
Expand All @@ -25,10 +27,12 @@

// Store
Route::post('news/', [NewsController::class, 'store'])
->middleware('permission:admin.access.news.create')
->name('news.store');

// Edit
Route::get('news/edit/{news}', [NewsController::class, 'edit'])
->middleware('permission:admin.access.news.edit')
->name('news.edit')
->breadcrumbs(function (Trail $trail) {
$trail->push(__('Home'), route('dashboard.home'))
Expand All @@ -38,10 +42,12 @@

// Update
Route::put('news/{news}', [NewsController::class, 'update'])
->middleware('permission:admin.access.news.edit')
->name('news.update');

// Delete
Route::get('news/delete/{news}', [NewsController::class, 'delete'])
->middleware('permission:admin.access.news.delete')
->name('news.delete')
->breadcrumbs(function (Trail $trail) {
$trail->push(__('Home'), route('dashboard.home'))
Expand All @@ -51,5 +57,6 @@

// Destroy
Route::delete('news/{news}', [NewsController::class, 'destroy'])
->middleware('permission:admin.access.news.delete')
->name('news.destroy');
});

0 comments on commit 63bc7ab

Please sign in to comment.