Skip to content

Commit

Permalink
Merge pull request #9 from tomatophp/develop
Browse files Browse the repository at this point in the history
integrate roles inside components
  • Loading branch information
3x1io authored Jan 30, 2024
2 parents 464bd24 + e2a9eff commit 6cad2c9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
26 changes: 13 additions & 13 deletions resources/views/components/button.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
@if($type === 'link' && !isset($icon))
<x-splade-link {{ $attributes->class([
'filament-button inline-flex items-center justify-center py-1 gap-1 font-medium rounded-lg border',
'focus:outline-none focus:ring-offset-2 focus:ring-2 focus:ring-inset dark:focus:ring-offset-0 min-h-[2.25rem] px-4',
'text-sm shadow-sm focus:ring-white filament-page-button-action',
'bg-danger-600 hover:bg-danger-500 focus:bg-danger-700 focus:ring-offset-danger-700 text-white border-transparent' => $danger,
'bg-warning-600 hover:bg-warning-500 focus:bg-warning-700 focus:ring-offset-warning-700 text-white border-transparent' => $warning,
'bg-primary-600 hover:bg-primary-500 focus:bg-primary-700 focus:ring-offset-primary-700 text-white border-transparent' => $primary,
'bg-success-600 hover:bg-success-500 focus:bg-success-700 focus:ring-offset-success-700 text-white border-transparent' => $success,
'bg-white hover:bg-gray-50 focus:bg-gray-100 focus:ring-offset-gray-95 text-gray-950 ring-gray-950/10 dark:bg-gray-800 dark:text-gray-200' => $secondary,
'cursor-pointer transition-colors ease-in-out duration-20'
]) }} :method="$method">
{{$label ?: $slot}}
</x-splade-link>
<x-splade-link {{ $attributes->class([
'filament-button inline-flex items-center justify-center py-1 gap-1 font-medium rounded-lg border',
'focus:outline-none focus:ring-offset-2 focus:ring-2 focus:ring-inset dark:focus:ring-offset-0 min-h-[2.25rem] px-4',
'text-sm shadow-sm focus:ring-white filament-page-button-action',
'bg-danger-600 hover:bg-danger-500 focus:bg-danger-700 focus:ring-offset-danger-700 text-white border-transparent' => $danger,
'bg-warning-600 hover:bg-warning-500 focus:bg-warning-700 focus:ring-offset-warning-700 text-white border-transparent' => $warning,
'bg-primary-600 hover:bg-primary-500 focus:bg-primary-700 focus:ring-offset-primary-700 text-white border-transparent' => $primary,
'bg-success-600 hover:bg-success-500 focus:bg-success-700 focus:ring-offset-success-700 text-white border-transparent' => $success,
'bg-white hover:bg-gray-50 focus:bg-gray-100 focus:ring-offset-gray-95 text-gray-950 ring-gray-950/10 dark:bg-gray-800 dark:text-gray-200' => $secondary,
'cursor-pointer transition-colors ease-in-out duration-20'
]) }} :method="$method">
{{$label ?: $slot}}
</x-splade-link>
@elseif($type === 'icon' || isset($icon))
<x-splade-link {{ $attributes->class([
'px-2 cursor-pointer transition-colors ease-in-out duration-20',
Expand Down
3 changes: 2 additions & 1 deletion resources/views/components/table-action.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

@if($type === 'link')
<x-splade-link {{ $attributes->class([
<x-splade-link {{ $attributes->class([
'text-left w-full px-4 py-2 text-sm font-normal',
'text-danger-700 dark:text-white dark:hover:bg-danger-600 hover:bg-gray-50 hover:text-danger-900' => $danger,
'text-warning-700 dark:text-white dark:hover:bg-warning-600 hover:bg-gray-50 hover:text-warning-900' => $warning,
Expand Down
16 changes: 15 additions & 1 deletion src/Services/TomatoMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;
use TomatoPHP\TomatoAdmin\Services\Contracts\Menu;
use TomatoPHP\TomatoAdmin\Services\Menu\TomatoMenuRegister;
use TomatoPHP\TomatoAdmin\Facade\TomatoMenu as TomatoMenuFacade;
Expand All @@ -25,7 +26,20 @@ public function __construct()

public function loadFromSource(): static
{
$this->menu = TomatoMenuFacade::load();
if(class_exists(Role::class)){
$this->menu = TomatoMenuFacade::load()->where('route', '!=', '')->filter(function ($item) {
$permission = \Spatie\Permission\Models\Permission::where('name', $item->route)->first();
if($permission){
return auth('web')->user()->can($permission->name);
}
else {
return true;
}
});
}
else {
$this->menu = TomatoMenuFacade::load();
}
return $this;
}

Expand Down
28 changes: 27 additions & 1 deletion src/Views/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TomatoPHP\TomatoAdmin\Views;

use Illuminate\Http\Request;
use Illuminate\View\Component;

class Button extends Component
Expand Down Expand Up @@ -29,6 +30,31 @@ public function __construct(
*/
public function render()
{
return view('tomato-admin::components.button');
return function (array $data) {
if(isset($data['attributes']) && isset($data['attributes']['href'])){
$route = \Route::getRoutes()->match(Request::create($this->attributes->getAttributes()['href']))->getName();
if(class_exists(\Spatie\Permission\Models\Role::class)){
$permission = \Spatie\Permission\Models\Permission::where('name', $route)->first();
}
else {
$permission = null;
}

if($permission && auth('web')->user() && auth('web')->user()->can($permission->name)){
return 'tomato-admin::components.button';
}
else if(!$permission){
return 'tomato-admin::components.button';
}
else {
return '';
}
}
else {
return 'tomato-admin::components.button';
}
};


}
}
26 changes: 25 additions & 1 deletion src/Views/TableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TomatoPHP\TomatoAdmin\Views;

use Illuminate\Http\Request;
use Illuminate\View\Component;

class TableAction extends Component
Expand Down Expand Up @@ -29,6 +30,29 @@ public function __construct(
*/
public function render()
{
return view('tomato-admin::components.table-action');
return function (array $data) {
if(isset($data['attributes']) && isset($data['attributes']['href'])){
$route = \Route::getRoutes()->match(Request::create($this->attributes->getAttributes()['href']))->getName();
if(class_exists(\Spatie\Permission\Models\Role::class)){
$permission = \Spatie\Permission\Models\Permission::where('name', $route)->first();
}
else {
$permission = null;
}

if($permission && auth('web')->user() && auth('web')->user()->can($permission->name)){
return 'tomato-admin::components.table-action';
}
else if(!$permission){
return 'tomato-admin::components.table-action';
}
else {
return '';
}
}
else {
return 'tomato-admin::components.table-action';
}
};
}
}

0 comments on commit 6cad2c9

Please sign in to comment.