【问题标题】:symfony - sfDoctrineGuard - restricting user creation based on group credentialssymfony - sfDoctrineGuard - 根据组凭据限制用户创建
【发布时间】:2011-04-20 08:18:44
【问题描述】:

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

我创建了 4 个组,editorsmoderatorsadminssuperadmins

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

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

这在 sfDoctrineGuard 中是否可行,如果可以,有人可以告诉我如何实现这一目标吗?

谢谢

【问题讨论】:

    标签: symfony1 symfony-1.4


    【解决方案1】:

    首先,您可以在 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 中加载组名称,而不是在代码中硬编码。

    【讨论】:

    • 如果我以版主身份登录,我仍然会看到所有用户,还是只看到列表中的版主和编辑用户?
    • 您可以使用相同的方法根据凭据创建自定义用户列表。创建一个 sfGuardUserTable::getForList 方法,并在 generator.yml > list > table_method 中指定。
    • 好的,但是无论我以什么用户身份登录,都会调用它。我有 4 个用户组,假设每个组中有 2 个用户。超级用户可以查看所有用户。但是版主应该只能看到版主和编辑,而编辑应该只能看到其他编辑。
    • 不会,这取决于登录的用户,请放心。看这行:“$user = sfContext::getInstance()->getUser();”。在这里,您将获得登录用户的实例。虽然这不是最佳做法。
    • @Dziamid 我已经尝试了上面的代码,但我无法让它工作。我使用基本帐户登录,即具有编辑权限,但我仍然可以看到所有用户和所有组。它似乎没有调用getListForAdmin 方法。有什么想法/帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2013-06-02
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多