1. Go to this page and download the library: Download sybio/permission-bundle 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/ */
declare(strict_types=1);
namespace App\Security\Permission;
use Sybio\PermissionBundle\PermissionInterface;
final readonly class EditArticlePermission implements PermissionInterface
{
public function __construct(
private int $articleId,
private int $userId,
) {
}
}
declare(strict_types=1);
namespace App\Security\Permission;
use App\Repository\ArticleRepository;
use Sybio\PermissionBundle\PermissionCheckerInterface;
use Sybio\PermissionBundle\PermissionInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @implements PermissionCheckerInterface<EditArticlePermission>
*/
final class EditArticlePermissionChecker implements PermissionCheckerInterface
{
public function __construct(
private readonly ArticleRepository $articleRepository,
) {
}
public function check(
PermissionInterface $permission,
ExecutionContextInterface $context,
): void {
$article = $this->articleRepository->find($permission->articleId);
if ($article === null) {
$context->buildViolation('The article does not exist.')
->addViolation();
return;
}
if ($article->getAuthorId() !== $permission->userId) {
$context->buildViolation('You are not authorized to edit this article.')
->addViolation();
}
}
public static function permissionClass(): string
{
return EditArticlePermission::class;
}
}
//...
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
$this->authorizationChecker->isGranted(
'PERMISSION',
new EditArticlePermission($articleId, $userId),
);
//...
use Sybio\PermissionBundle\Validation\PermissionValidatorInterface;
dd(
$this->permissionValidator->getLastViolations(),
);
//...
use App\Security\Permission\EditArticlePermission;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ArticleController extends AbstractController
{
#[Route('/article/{id}/edit', name: 'article_edit')]
public function edit(int $id): Response
{
$this->denyAccessUnlessGranted('PERMISSION', new EditArticlePermission($id, $this->getUser()->getId()));
// Do whatever you want here
return $this->render('article/edit.html.twig');
}
}
//...
use Sybio\PermissionBundle\Validation\PermissionValidatorInterface;
$violations = $this->permissionValidator->validate($permission);
if ($violations->count() > 0) {
// Do something with the violations
}
declare(strict_types=1);
namespace App\Security\Permission;
use Sybio\PermissionBundle\PermissionInterface;
final readonly class DeleteCommentPermission implements PermissionInterface
{
public function __construct(
public Comment $comment,
public ?User $user,
public bool $isAdmin,
) {
}
}
declare(strict_types=1);
namespace App\Security\Permission;
use App\Repository\CommentRepository;
use Sybio\PermissionBundle\PermissionCheckerInterface;
use Sybio\PermissionBundle\PermissionInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @implements PermissionCheckerInterface<DeleteCommentPermission>
*/
final class DeleteCommentPermissionChecker implements PermissionCheckerInterface
{
public function check(
PermissionInterface $permission,
ExecutionContextInterface $context,
): void {
$comment = $permission->comment;
// Check that the comment is not soft deleted
if ($comment->isDeleted()) {
$context->buildViolation('The comment is already deleted.')
->addViolation();
return;
}
// Admins can always delete
if ($permission->isAdmin) {
return;
}
// Check that the user is the author of the comment
if ($comment->getAuthorId() !== $permission->user->getId()) {
$context->buildViolation('You are not authorized to delete this comment.')
->addViolation();
}
}
public static function permissionClass(): string
{
return DeleteCommentPermission::class;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.