Symfony sfDoctrineGuard 插件 sfGuardUser 模块
使用 sfDoctrineGuard 插件时,它会自动生成后端管理功能,我可以在其中编辑系统用户并为其分配权限。
因此,我访问 http://.../backend_dev.php/sf_guard_user/:id/edit
,在那里我会看到用户的信息,包括可供选择的权限。
默认情况下,权限显示为多选框,HTML 如下:
<select name="sf_guard_user[permissions_list][]" multiple="multiple" id="sf_guard_user_permissions_list">
<option value="1">Permission1</option>
<option value="2">Permission2</option>
<option value="3">Permission3</option>
<option value="4">Permission4</option>
</select>
我更喜欢的是复选框列表。所以我四处搜索,发现如果我将选项“expanded”设置为 true 添加到以下代码:
'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'expanded' => true,)),
该代码是此文件的一部分:lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php。我认为我不应该编辑这个文件(如果重新安装 sfDoctrineGuard,更改可能会被覆盖),但想不出另一种方法来使其工作。
生成的 HTML 如下:
<ul class="checkbox_list">
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="1" id="sf_guard_user_permissions_list_1" /> <label for="sf_guard_user_permissions_list_1">Permission1</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="2" id="sf_guard_user_permissions_list_2" /> <label for="sf_guard_user_permissions_list_2">Permission2</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="3" id="sf_guard_user_permissions_list_3" /> <label for="sf_guard_user_permissions_list_3">Permission3</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="4" id="sf_guard_user_permissions_list_4" /> <label for="sf_guard_user_permissions_list_4">Permission4</label></li>
</ul>
我现在需要做的是根据前缀拆分权限。例如,如果我拥有名为 user_action1、user_action2、file_action1、file_action2 的权限,它们将显示为:
User
checkbox (custom label) Action One
checkbox Action Two
File
checkbox (custom label) Action One
checkbox Action Two
但不知道从哪里开始。如果有一个要编辑的模板,那就很容易了,但由于我正在处理表单框架,所以我的理解是模板是动态生成的 - 我可以在我的 symonfy 缓存文件夹中看到它们。
我该怎么办呢?
我开始编写自己的 sfWidgetFormDoctrineChoicePermission 类,该类扩展了与 sfWidgetFormDoctrineChoice 相同的类,但我正在努力正确编辑渲染函数以获得所需的输出。这是开展这项工作的正确方法吗?
我还需要将我的 sfGuardUserProfile 模型集成到编辑用户页面中(与上面相同),我读到过编辑 sfGuardUser 插件模块的generator.yml 文件并简单地添加 sfGuardUserProfile 表中的字段名称即可使其工作,但是遗憾的是事实并非如此。
When using sfDoctrineGuard plugin, it automatically generates the backend administration functionality where I can edit users of the system and assign them permissions.
So I visit http://.../backend_dev.php/sf_guard_user/:id/edit
where I am presented with the user's information including the available permissions to select.
By default the permissions are shown as a multiple select box, HTML follows:
<select name="sf_guard_user[permissions_list][]" multiple="multiple" id="sf_guard_user_permissions_list">
<option value="1">Permission1</option>
<option value="2">Permission2</option>
<option value="3">Permission3</option>
<option value="4">Permission4</option>
</select>
What I would prefer is a list of checkboxes. So I searched around and found that if I add the option "expanded" set to true to the following code:
'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'expanded' => true,)),
The code is part of this file: lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php. I don't think I should've edited this file (potential for changes to be overwritten should sfDoctrineGuard ever be re-installed) but couldn't think of another way to make it work.
The generated HTML is as follows:
<ul class="checkbox_list">
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="1" id="sf_guard_user_permissions_list_1" /> <label for="sf_guard_user_permissions_list_1">Permission1</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="2" id="sf_guard_user_permissions_list_2" /> <label for="sf_guard_user_permissions_list_2">Permission2</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="3" id="sf_guard_user_permissions_list_3" /> <label for="sf_guard_user_permissions_list_3">Permission3</label></li>
<li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="4" id="sf_guard_user_permissions_list_4" /> <label for="sf_guard_user_permissions_list_4">Permission4</label></li>
</ul>
What I need to do now is split up the permissions based on their prefix. For example if I had permissions named user_action1, user_action2, file_action1, file_action2, they would display like:
User
checkbox (custom label) Action One
checkbox Action Two
File
checkbox (custom label) Action One
checkbox Action Two
but have no idea where to start with this. It would be easy if there was a template to edit but since I'm dealing with the Forms framework it is my understanding that the templates are generated on the fly - I can see them in my symonfy cache folder.
How would I go about this?
I started writing my own sfWidgetFormDoctrineChoicePermission class that extends the same class as sfWidgetFormDoctrineChoice but am struggling to edit the rendering functions correctly for the desired output. Is this the correct way to go about this work?
I also need to integrate my sfGuardUserProfile model into the edit user page (same as above), I read somwhere that editing the generator.yml file for the sfGuardUser plugin module and simply adding the field names from the sfGuardUserProfile table would make it work, but sadly it doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,永远不要编辑基类。您要编辑的内容是:
从这里您可以通过调用覆盖默认小部件:
您应该创建自己的小部件并从 sfWidgetFormDoctrineChoice 扩展它是最好的开始。基本上它会返回 html 作为 php 字符串,并会得到回显。这很简单,您最终会明白。
要在编辑主用户表单时包含您的个人资料表单,非常简单,在同一个 sfGuardUserForm 类中:
最后一个调用超出了我的想象,因此语法可能是错误的,因此请检查一下,并将关系替换为名称当然是你的个人资料关系。
First of all don't ever edit base classes. The one you are wanting to edit is:
From here you can override the default widget by calling:
You should be creating your own widget and exdending it from sfWidgetFormDoctrineChoice is the best start. Basically it will return html as php strings which will get echoed. It is simple enough to get your head around eventually.
To include your profile form when editing your main user form it is very simple, in the same sfGuardUserForm class:
That last call is off the top of my head so the syntax might be wrong so check it out, also replace the relationship with the name of your profile relationship of course.
首先,您必须将新的
configure()
放入UserAdmin
表单(后端)中,而不是来自(前端)的常规用户。然后,您的
configure()
方法将具有新的小部件声明:祝您好运:)
First off, you have to put the new
configure()
inUserAdmin
form (backend), not the regular User from (frontend).Then, your
configure()
method will have the new widget declaration:Good luck:)