symfony - sfDoctrineGuard - 根据组凭据限制用户创建

时间:2011-04-20 08:18:45

标签: symfony1 symfony-1.4

我目前正在使用sfDoctrineGuard开发一个相当庞大且复杂的用户管理系统

我创建了4个群组,editorsmoderatorsadminssuperadmins

我想要做的是限制管理员中的某些用户能够在sfGuardUser管理模块中创建/查看/编辑其他用户。

例如,superadmins用户可以创建editorsmoderatorsadmins和其他superadmins,但moderator只能创建editors {{1}}。

这是否可以在sfDoctrineGuard中使用,如果是这样,有人可以让我了解我是如何实现这一目标的吗?

由于

1 个答案:

答案 0 :(得分:0)

首先,您可以在generator.yml中设置凭据,以显示/隐藏基于凭据的操作和对象操作的链接。例如:

config:
  list:
    object_actions:
      _delete:
        confirm: Вы уверены, что хотите удалить пользователя?
        credentials: superuser
    actions:
      _new:
        credentails: moderator

接下来,使用针对组的学说选择小部件的自定义表方法配置表单:

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());
  }
}

class sfGuardGroupTable extends PluginsfGuardGroupTable
{
  /**
   * Builds list query based on credentials
   *
   */
  public function getListForAdmin()
  {
    $user = sfContext::getInstance()->getUser();

    $q = $this->createQuery('g');

    if (!$user->isSuperAdmin() && $user->hasCredential('moderator'))
    {
      $q->addWhere('g.name IN (?)', array('editor'));
    }
    else if ($user->hasCredential('editor'))
    {
      $q->addWhere('g.name IN (?)', array('editor'));
    }        
    return $q;
  }
}

一些增强功能:通过从操作中传递用户实例(在preExecute中)来删除单线调用,并使用sfConfig :: get从app.yml加载组名,而不是在代码中使用硬编码。