PHP code example of ybenhssaien / authorization-bundle
1. Go to this page and download the library: Download ybenhssaien/authorization-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/ */
ybenhssaien / authorization-bundle example snippets
namespace App\Form;
use App\Entity\Person;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
class PersonType extends AbstractType
{
protected Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// "gender_renamed" is the name chosen for this property ("gender" doesn't exist)
if ($this->security->isGranted('write', [Person::class, 'gender_renamed'])) {
$builder->add('gender');
}
if ($this->security->isGranted('write', [Person::class, 'firstName'])) {
$builder->add('firstName');
}
if ($this->security->isGranted('write', [Person::class, 'lastName'])) {
$builder->add('lastName');
}
// "code" is declared an alias of "hrCode" (can use both of them)
if ($this->security->isGranted('write', [Person::class, 'code'])) {
$builder->add('hrCode');
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Person::class,
]);
}
}
namespace App\Entity;
// ....
use Ybenhssaien\AuthorizationBundle\Annotation\HasAuthorizations;
/**
* @Entity()
*
* @HasAuthorizations()
*/
class Entity
{}