【发布时间】:2013-06-02 17:27:53
【问题描述】:
我正在使用 sfDoctrineGuardPlugin 和 Symfony 1.4.20 开发一个应用程序,然后我有三个用户和三个用户组以及以下一些权限:
user_name user_group permissions
u1 Group1 can_admin_full, can_admin
u2 Group2 can_admin
u3 Group3 no_admin
所以 u1 应该能够将用户添加到应用程序,但只能在 Groups Options 下看到 Group2 和 Group3,u2 应该能够将用户添加到应用程序但只能在 Groups Options 下看到 Group3,所以 u1 和 u2 不应该添加属于 Group1 的用户,我如何使用 sfDoctrineGuard 来获得这个?有可能吗?
注意:我以 GroupN 为例,但下面的代码示例是组和权限的正确名称!
编辑:改进问题
因此,在仔细查看this 之后,我尝试相同但适应了我的代码,所以在lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php 中我改变了这个:
class sfGuardUserForm extends PluginsfGuardUserForm {
public function configure() {
//groups_list
$this->getWidget('groups_list')->setOption('expanded', true);
$this->getWidget('groups_list')->setOption('table_method', 'getListForAdmin');
$this->getValidator('groups_list')->setOption('query', Doctrine::getTable('sfGuardGroup')->getListForAdmin());
}
}
我也在lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardGroupTable.class.php做了这个更改
class sfGuardGroupTable extends PluginsfGuardGroupTable {
/**
* Returns an instance of this class.
*
* @return object sfGuardGroupTable
*/
public static function getInstance() {
return Doctrine_Core::getTable('sfGuardGroup');
}
/**
* Builds list query based on credentials
*
*/
public function getListForAdmin() {
$user = sfContext::getInstance()->getUser();
$q = $this->createQuery('g');
if ($user->hasPermissions('can_admin_full')) {
$q->addWhere('g.name IN (?)', array('Administradores Monitor', 'Monitor'));
} else if ($user->hasPermissions('can_admin')) {
$q->addWhere('g.name IN (?)', array('Monitor'));
}
return $q;
}
}
但不起作用,因为使用属于组“Administrador de Servicios”并具有权限“can_admin”和“can_admin_full”的用户登录,我可以看到小部件中的所有组,我只是在寻找在这种情况下,'Administradores Monitor' 和 'Monitor'
编辑 2 也试试这个其他代码:
$this->widgetSchema['groups_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'table_method' => 'getListForAdmin', 'query' => Doctrine::getTable('sfGuardGroup')->getListForAdmin()));
仍然无法正常工作,我将 'table_method' => 'getListForAdmin' 更改为 'table_method' => 'getListForAdmin1' 并没有任何反应,所以我怀疑该方法从未被调用
编辑 3 现在它正在工作,但看到这个: 如果我使用这种方法:
$this->getWidget('groups_list')->setOption('expanded', true);
$this->getWidget('groups_list')->setOption('table_method', 'getListForAdmin');
$this->getValidator('groups_list')->setOption('query', Doctrine::getTable('sfGuardGroup')->getListForAdmin());
然后我得到这个错误:
SQLSTATE[HY093]:无效的参数号:绑定变量的数量 令牌数不匹配
如果我使用其他方法:
$this->widgetSchema['groups_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'table_method' => 'getListForAdmin', 'query' => Doctrine::getTable('sfGuardGroup')->getListForAdmin()));
我收到另一个错误:
sfWidgetFormDoctrineChoice 需要以下选项:'model'。
然后我添加了参数model:
$this->widgetSchema['groups_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'table_method' => 'getListForAdmin', 'query' => Doctrine::getTable('sfGuardGroup')->getListForAdmin()));
但得到与第一种方法相同的错误,我怀疑问题出在 getListForAdmin() 函数中但真的不知道到底在哪里
此时有什么问题?
【问题讨论】:
-
是的,有可能。但请记住,SO 不是有人为您编写代码的地方,而是当您遇到困难时可以寻求帮助的地方。向我们展示您为实现您的需要而尝试了什么,以及您是否遇到了问题。届时我们将能够为您提供帮助。
-
@MichalTrojanowski 嗨,感谢您的回复,我不是要求有人为我完成这项工作,只是想找到正确的路径来完成这项工作我的意思是一些建议在哪些文件中进行编辑,哪些方法,关于这个的任何建议只是为了了解整个想法,你能帮忙吗?
-
现在好多了:) 请参阅下面的答案。不确定这是否是问题所在,但让我们从一些事情开始,我们最终会得到结果;)
标签: permissions symfony1 symfony-1.4 sfguard sfdoctrineguard