symfony - Symfony2 - User specific form based on role access -
I have a form that has a category unit dropdown that has a Post Unit (OneToMany / ManyToOne).
Status: Now the customer has to select the category in the drop down and they can make the mistake of choosing the wrong category and they will go to any other blog (if they choose the wrong ) And they will not be allowed to change it back to the appropriate category.
To minimize this potential problem, I want to do one of the two solutions:
1) Set the Auto Category by Category
2) Or restrict the user to select only the category that has access to them (e.g., if the user has a specific role, they only Get this category for drop down)
The user has a There is a ROLE_USER restriction that allows them only the CRUD, which they have access to.
For example,
-
ROLEUSER1 has only access to / class 1 (and can use CRUD on it)
-
Only ROLEUSER2 / category 2 has access (and can use CRUD)
-
ROLEUSER3 has access to / c only Ategory3 (And can use CRUD on it)
How can I set it so that the customer can not make the mistake of choosing the wrong class?
('Title') - & gt; Joint ('body') - & gt; Addition ('Author') - & gt; Addition ('Category') - & gt; Add ('file', 'file', array ('label' = & gt; 'image', 'required' = & gt; false)) - & gt; Addition ('created'); }
Controller
public function job1CreateAction (Request $ request) {$ entity = new post (); $ Form = $ this- & gt; CreateCreateForm ($ unit); $ Form & gt; HandleRequest ($ request); If ($ form-> isValid ()) {$ em = $ this- & gt; GetDoctrine () - & gt; GetManager (); $ Em- & gt; Continues ($ unit); $ Em- & gt; Flush (); $ $ - ($ -> - URURL ('Job1_Show', Array ('id' => entity entity-> ID (ID)) ;; } Returns array ('unit' => gtc: $ unit, 'form' = & gt; $ form-> createView (),); } Private Function createCreateForm (post $ unit) {$ form = $ this- & gt; CreateForm (new post type), $ unit, array ('action' => = this- & gt; generated url ('job1_create'), 'method' = & gt; 'POST',)); $ Form-> Add ('submit', 'submit', array ('label' = & gt; 'create')); $ Form return; }
How about this? : Remove the 'Category' field from the form builder and set it manually in the Administrator action:
Get ($ -> this-> Get ('security.context') - & Gt; isGranted ('ROLEUSER1') {$ Entity-> Set category (CATEGORY1);}
Edit:
Administrator action:
(get 'security.context') -> Egrend (' ROLEUSER1 ') {$ category1 = $ this- & gt; getDoctrine () - & gt; getManager () - & gt; getRepository (' music: post ') - & gt; search (1); // We are getting the category object. This is just an example, it will be different in your case $ unit -> Set category ($ category1);} $ form = $ this-> createCreateForm ($ Unit); ....}and form:
public function buildForm (FormBuilderInterface $ builder, array $ option) {$ manufacturer - & Gt; addition ('title') - & gt; joint ('body') - & gt; Add ('Author') // - & gt; Adding range ') - & gt; Add ('file', 'file', array ('label' = & gt; 'image', 'required' => false)) - & gt; Addition ('created'); }
Comments
Post a Comment