PHP code example of spotonlive / sl-assertions
1. Go to this page and download the library: Download spotonlive/sl-assertions library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spotonlive / sl-assertions example snippets
'providers' => [
\SpotOnLive\Assertions\Providers\Services\AssertionServiceProvider::class,
\SpotOnLive\Assertions\Providers\Helpers\AssertionHelperProvider::class,
]
'aliases' => [
'AssertionHelper' => \SpotOnLive\Assertions\Facades\Helpers\AssertionHelperFacade::class,
]
namespace App\Assertions\Users;
use SpotOnLive\Assertions\AssertionInterface;
use App\Entities\User;
class EditAssertion implements AssertionInterface
{
/**
* @param User $user
* @param array $data
* @return bool
*/
public function assert($user, array $data = [])
{
/** @var User $userToEdit */
$userToEdit = $data['user'];
return $user == $userToEdit || $user->hasRole(['superadmin', 'admin']);
}
}
return [
'users.edit' => \App\Assertions\Users\EditAssertion::class,
];
namespace App\Controllers;
use \SpotOnLive\Assertions\Services\AssertionServiceInterface;
class Controller
{
/** @var AssertionServiceInterface **/
protected $assertionService;
public function __construct(AssertionServiceInterface $assertionService)
{
$this->assertionService = $assertionService;
}
public function admin()
{
if (!$this->assertionService->isGranted('admin.page', Auth::user())) {
return redirect()->route('not-granted');
}
return view('admin.page');
}
}
@if(AssertionHelper::isGranted('user.edit', Auth::user(), ['user' => $user]))
<a href="{{URL::route('user.edit')}}">{{_('Edit user')}}</a>
@endif