1. Go to this page and download the library: Download fivelab/authorize-action 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/ */
fivelab / authorize-action example snippets
namespace Application\Security;
use FiveLab\Component\AuthorizeAction\Action\AuthorizeActionInterface;
/**
* The authorize action for check grants for edit post
*/
class EditPostAction implements AuthorizeActionInterface
{
/**
* @var int
*/
public $id;
/**
* Constructor.
*
* @param int $postId
*/
public function __construct(int $postId)
{
$this->id = $postId;
}
}
namespace Application\Security\Verifier;
use Application\Security\EditPostAction;
use FiveLab\Component\AuthorizeAction\Action\AuthorizeActionInterface;
use FiveLab\Component\AuthorizeAction\Verifier\AuthorizeActionVerifierInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class EditPostVerifier implements AuthorizeActionVerifierInterface
{
/**
* {@inheritdoc}
*/
public function supports(AuthorizeActionInterface $action, UserInterface $user): bool
{
return $action instanceof EditPostAction;
}
/**
* {@inheritdoc}
*/
public function verify(AuthorizeActionInterface $action, UserInterface $user): void
{
if (!$user->isSuperAdmin() && !$user->isCopywriter()) {
throw new AccessDeniedException();
}
}
}
use Application\Security\Verifier\EditPostVerifier;
use FiveLab\Component\AuthorizeAction\AuthorizationChecker;
use FiveLab\Component\AuthorizeAction\Verifier\AuthorizeActionVerifierChain;
use FiveLab\Component\AuthorizeAction\UserProvider\SymfonyTokenStorageUserProvider;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
$tokenStorage = new TokenStorage();
$userProvider = new SymfonyTokenStorageUserProvider($tokenStorage);
$verifierChain = new AuthorizeActionVerifierChain();
$verifierChain->add(new EditPostVerifier());
$authorizationChecker = new AuthorizationChecker($verifierChain, $userProvider);