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


   return [
       ......
       Ybenhssaien\AuthorizationBundle\AuthorizationBundle::class => ['all' => true],
   ];
    



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Ybenhssaien\AuthorizationBundle\Annotation\Authorization;
use Ybenhssaien\AuthorizationBundle\Annotation\HasAuthorizations;

/**
 * @ORM\Entity()
 *
 * @HasAuthorizations()
 */
class Person
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=10)
     *
     * @Authorization(
     *      "name" : "gender_renamed",
     *      {
     *          "ROLE_HR" : {"read": true, "write": true},
     *          "ROLE_USER" : {"read": true, "write": false},
     *          "ROLE_OTHERS" : {"read": true, "write": false},
     *      }
     *  )
     */
    private $gender;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Authorization({
     *     "ROLE_HR",
     *     "ROLE_USER" : {"write": false},
     *     "ROLE_OTHERS" : {"write": false},
     * })
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Authorization({
     *     "ROLE_HR",
     *     "ROLE_USER" : {"write": false},
     *     "ROLE_OTHERS" : {"write": false},
     * })
     */
    private $lastName;

    /**
     * @ORM\Column(type="string", length=10)
     *
     * @Authorization(
     *     {"ROLE_HR"},
     *     "aliases" : {"code"},
     * )
     */
    private $hrCode;
}



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
   {}       
   

           /**
             * @ORM\Column(type="string")
             *
             * @Authorization(
             *      "name" : "custom_name",
             *      {/* Roles */}
             *  )
             */
          private $property;
        

           /**
             * @ORM\Column(type="string")
             *
             * @Authorization(
             *      "aliases" : {"alias1", "alias2", ....},
             *      {/* Roles */}
             *  )
             */
          private $property;
        

           /**
             * @ORM\Column(type="string")
             *
             * @Authorization(
             *      "roles" : {
             *        "ROLE_USER",
             *        "ROLE_GUEST" : {"write" : false},
             *        "ROLE_OTHER" : {"read" : false, "write" : false},
             *      }
             *  )
             */
          private $property;

           /**
             * @ORM\Column(type="string")
             *
             * @Authorization(
             *      "name" : "custom_name",
             *      "aliases" : {"alias"},
             *      {
             *        "ROLE_USER",
             *        "ROLE_GUEST" : {"write" : false},
             *        "ROLE_OTHER" : {"read" : false, "write" : false},
             *      }
             *  )
             */
          private $property2;