1. Go to this page and download the library: Download epubli4/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/ */
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManagerInterface;
use Epubli\PermissionBundle\Interfaces\SelfPermissionInterface;
class ExampleEntity implements SelfPermissionInterface
{
/**
* @ORM\Column(type="integer")
*/
private $user_id;
public function getUserId(): ?int
{
return $this->user_id;
}
/**
* @inheritDoc
*/
public function getUserIdForPermissionBundle(): ?int
{
return $this->getUserId();
}
/**
* @inheritDoc
*/
public function getFieldNameOfUserIdForPermissionBundle(): string
{
return 'user_id';
}
/**
* @inheritDoc
*/
public function hasUserIdProperty(): bool
{
return true;
}
/**
* @inheritDoc
*/
public function getPrimaryIdsWhichBelongToUser(EntityManagerInterface $entityManager, int $userId): array
{
return [];
}
}
use Doctrine\ORM\Mapping as ORM;
use Epubli\PermissionBundle\Interfaces\SelfPermissionInterface;
use Epubli\PermissionBundle\Traits\SelfPermissionTrait;
class ExampleEntity implements SelfPermissionInterface
{
use SelfPermissionTrait;
/**
* @ORM\Column(type="integer")
*/
private $user_id;
public function getUserId(): ?int
{
return $this->user_id;
}
}
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Epubli\PermissionBundle\Interfaces\SelfPermissionInterface;
class ExampleEntity implements SelfPermissionInterface
{
/**
* @ORM\OneToOne(targetEntity=OtherEntity::class, inversedBy="exampleEntity", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $otherEntity;
public function getOtherEntity(): ?OtherEntity
{
return $this->otherEntity;
}
public function getPrimaryIdsWhichBelongToUser(EntityManagerInterface $entityManager, int $userId): array
{
/** @var Query $query */
$query = $entityManager->getRepository(__CLASS__)
->createQueryBuilder('c')
->select('c.id')
->join('c.otherEntity', 'u')
->where('u.userId = :userId')
->setParameter('userId', $userId)
->getQuery();
return array_column($query->getArrayResult(), 'id');
}
public function getUserIdForPermissionBundle(): ?int
{
return $this->getOtherEntity()->getUserId();
}
public function getFieldNameOfUserIdForPermissionBundle(): string
{
return '';
}
public function hasUserIdProperty(): bool
{
return false;
}
}
namespace App\Controller;
use Epubli\PermissionBundle\Service\AccessToken;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestAction extends AbstractController
{
public function __invoke(AccessToken $accessToken)
{
var_dump('Is the token present and valid: ' . $accessToken->exists());
var_dump('This is the unique json token identifier: ' . $accessToken->getJTI());
var_dump('The id of the user: ' . $accessToken->getUserId());
var_dump('Checking for permissions: ' . $accessToken->hasPermissionKey('user.user.delete'));
}
}
namespace App\Controller;
use Epubli\PermissionBundle\Annotation\Permission;
use Epubli\PermissionBundle\Service\AccessToken;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class TestController extends AbstractController
{
/**
* @Permission(
* key="customPermission1",
* description="This is a description"
* )
* @Permission(
* key="customPermission2",
* description="This is a description"
* )
*/
public function postTest(AccessToken $accessToken)
{
if (!$accessToken->exists()){
throw new UnauthorizedHttpException('Bearer', 'Access-Token is invalid.');
}
if (!$accessToken->hasPermissionKey('test.customPermission1')){
throw new AccessDeniedHttpException('Missing permission key: test.customPermission1');
}
//User is now authenticated and authorized for customPermission1
if (!$accessToken->hasPermissionKey('test.customPermission2')){
throw new AccessDeniedHttpException('Missing permission key: test.customPermission2');
}
//User is now authenticated and authorized for customPermission2
}
}
use Epubli\ApiPlatform\TestBundle\OrmApiPlatformTestCase;
use Epubli\PermissionBundle\Traits\JWTMockTrait;
class JsonWebTokenTest extends OrmApiPlatformTestCase
{
use JWTMockTrait;
public static function setUpBeforeClass(): void
{
self::setUpJsonWebTokenMockCreator();
}
public function setUp(): void
{
parent::setUp();
self::$kernelBrowser->getCookieJar()->set(self::$cachedCookie);
}
}
use Epubli\ApiPlatform\TestBundle\OrmApiPlatformTestCase;
use Epubli\PermissionBundle\Traits\JWTMockTrait;
class JsonWebTokenTest extends OrmApiPlatformTestCase
{
use JWTMockTrait;
public static function setUpBeforeClass(): void
{
self::setUpJsonWebTokenMockCreator();
}
public function testRetrieveTheResourceList(): void
{
self::$kernelBrowser->getCookieJar()->set(self::$cachedCookie);
$this->request(
'/api/json_web_tokens',
'GET'
);
}
}
use Epubli\ApiPlatform\TestBundle\OrmApiPlatformTestCase;
use Epubli\PermissionBundle\Traits\JWTMockTrait;
use Epubli\PermissionBundle\Traits\UnitTestTrait;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestConfig;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestDeleteData;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestGetCollectionData;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestGetItemData;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestPostData;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestUpdateData;
class CompanyDataTest extends OrmApiPlatformTestCase
{
use JWTMockTrait;
use UnitTestTrait;
public const RESOURCE_URI = '/api/company_datas/';
public static function setUpBeforeClass(): void
{
self::setUpJsonWebTokenMockCreator();
self::$unitTestConfig = new UnitTestConfig();
}
public function setUp(): void
{
parent::setUp();
self::$kernelBrowser->getCookieJar()->set(self::$cachedCookie);
}
protected function getDemoEntity(): CompanyData
{
$userProfileTestDummy = (new UserProfileTest())->getDemoEntity();
$this->persistAndFlush($userProfileTestDummy);
$companyData = new CompanyData();
$companyData->setCompanyName(self::$faker->company);
$companyData->setValueAddedTaxNumber((string)self::$faker->randomNumber());
$companyData->setUserProfile($userProfileTestDummy);
$companyData->setCreatedAt(self::$faker->dateTimeBetween('-200 days', 'now'));
$companyData->setUpdatedAt(self::$faker->dateTimeBetween($companyData->getCreatedAt(), 'now'));
return $companyData;
}
public function getDeleteDataForPermissionBundle(): ?UnitTestDeleteData
{
/** @var CompanyData $companyData */
$companyData = $this->findOne(CompanyData::class);
$userId = $companyData->getUserProfile()->getUserId();
return new UnitTestDeleteData(
self::RESOURCE_URI . $companyData->getId(),
'user-profile.company_data.delete',
$userId
);
}
public function getUpdateDataForPermissionBundle(): ?UnitTestUpdateData
{
/** @var CompanyData $companyData */
$companyData = $this->findOne(CompanyData::class);
$userId = $companyData->getUserProfile()->getUserId();
return new UnitTestUpdateData(
self::RESOURCE_URI . $companyData->getId(),
'user-profile.company_data.update.companyName',
$userId,
json_encode(
[
'companyName' => 'new Company Name',
]
),
'companyName',
'new Company Name'
);
}
public function getPostDataForPermissionBundle(): ?UnitTestPostData
{
$companyData = $this->getDemoEntity();
$userId = $companyData->getUserProfile()->getUserId();
return new UnitTestPostData(
self::RESOURCE_URI,
'user-profile.company_data.create',
$userId,
json_encode(
[
'companyName' => $companyData->getCompanyName(),
'valueAddedTaxNumber' => $companyData->getValueAddedTaxNumber(),
'userProfile' => '/api/user_profiles/' . $companyData->getUserProfile()->getId(),
]
)
);
}
public function getGetItemDataForPermissionBundle(): ?UnitTestGetItemData
{
/** @var CompanyData $companyData */
$companyData = $this->findOne(CompanyData::class);
$userId = $companyData->getUserProfile()->getUserId();
return new UnitTestGetItemData(
self::RESOURCE_URI . $companyData->getId(),
'user-profile.company_data.read',
$userId
);
}
public function getGetCollectionDataForPermissionBundle(): ?UnitTestGetCollectionData
{
/** @var CompanyData $companyData */
$companyData = $this->findOne(CompanyData::class);
$userId = $companyData->getUserProfile()->getUserId();
return new UnitTestGetCollectionData(
self::RESOURCE_URI,
'user-profile.company_data.read',
$userId,
1
);
}
}
use Epubli\ApiPlatform\TestBundle\OrmApiPlatformTestCase;
use Epubli\PermissionBundle\Traits\UnitTestTrait;
use Epubli\PermissionBundle\UnitTestHelpers\UnitTestConfig;
class ExampleTest extends OrmApiPlatformTestCase
{
use UnitTestTrait;
public static function setUpBeforeClass(): void
{
self::$unitTestConfig = new UnitTestConfig();
// If you implemented the SelfPermissionInterface in your entity
// then set this to true (defaults to true):
self::$unitTestConfig->implementsSelfPermissionInterface = true;
// If you do not have a DELETE route for your entity
// then set this to false (defaults to true):
self::$unitTestConfig->hasDeleteRoute = true;
// If your DELETE route
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.