symfony - Symfony2: Something like a Role Provider? -


in web application, want user's able create roles , add users them dynamically. thing imagine, edit security.yml every time, can't best solution, can it? nice, if there user provider roles, can define 1 loads roles database (doctrine).

thanks help, hice3000.

then, should want add role entity model hice.

you have know symfony2 provides support dynamic roles too. have getroles() method in symfony2 user spec in api doc, user entity should implement, forces him return roles. these roles must either implement role interface specifies getrole() method returns, usually, role name itself.

you can add newly created role directly user role list getroles() user method return.

here example using annotations : first role class

/**  * role class  *  * @orm\entity()  */ class role implements roleinterface, \serializable {     /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="name", type="string", length=255)      */     private $name;      /**      * @orm\manytomany(targetentity="user", mappedby="userroles")      */     private $users;      public function __construct()     {         $this->users = new \doctrine\common\collections\arraycollection();     }      public function getrole()     {         return $this->name;     }  } 

and user class

/**  * user  *  * @orm\entity()  */ class user implements userinterface, \serializable {     /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="username", type="string", length=255)      */     private $username;      /**      * @orm\manytomany(targetentity="role", inversedby="users")      * @orm\jointable(name="user_roles")      */     private $userroles;      public function __construct()     {         $this->userroles = new \doctrine\common\collections\arraycollection();     }      public function getroles()     {         return $this->userroles->toarray();     } 

i've skipped imports , methods simplify approach.

edit : there know serialization too. sharom commented on github, musn't serialize users in roles neither roles in users. read post , think you'll understand :)


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -