0

我正在尝试使用我自己的注册表单将新用户添加到 sfDoctrineGuard 表。这是我做的配置功能:

public function configure() {
    // Remove all widgets we don't want to show
    unset(
            $this['is_super_admin'], $this['updated_at'], $this['groups_list'], $this['permissions_list'], $this['last_login'], $this['created_at'], $this['salt'], $this['algorithm']
    );

    $id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
    $this->setDefault('idempresa', $id_empresa);

    $this->validatorSchema['idempresa'] = new sfValidatorPass();

    $this->widgetSchema['first_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['last_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['username'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['email_address'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));

    // Setup proper password validation with confirmation
    $this->validatorSchema['password']->setOption('required', true);
    $this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];

    $this->widgetSchema->moveField('password_confirmation', 'after', 'password');

    $this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirmation', array(), array('invalid' => 'The two passwords must be the same.')));
}

现在我需要使用用户登录的相同组创建这些新用户,但我不知道如何。我读了这篇文章,但不知道使用getGroups()是否可以完成我的意思是设置groups_list默认值,有什么建议吗?最好的方法是什么?

4

1 回答 1

2

有几种方法可以做到这一点...我建议在对其他字段进行验证并保存用户对象后添加组;因此您可以覆盖save()表单的功能并将它们添加到那里:

<?php

class YourUserForm extends PluginsfGuardUserForm
{
  /**
   * A class variable to store the current user
   * @var sfGuardUser
   */
  protected $current_user;

  public function configure()
  {
    // Remove all widgets we don't want to show
    unset(
      $this['is_super_admin'],
      $this['updated_at'],
      $this['groups_list'],
      $this['permissions_list'],
      $this['last_login'],
      $this['created_at'],
      $this['salt'],
      $this['algorithm']
    );

    // save the currrent user for use later in the save function
    $this->current_user = sfContext::getInstance()->getUser()->getGuardUser();

    $id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();;
    $this->setDefault('idempresa', $id_empresa);

    $this->validatorSchema['idempresa'] = new sfValidatorPass();

    $this->widgetSchema['first_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['last_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['username'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['email_address'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
    $this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));

    // Setup proper password validation with confirmation
    $this->validatorSchema['password']->setOption('required', true);
    $this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];

    $this->widgetSchema->moveField('password_confirmation', 'after', 'password');

    $this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirmation', array(), array('invalid' => 'The two passwords must be the same.')));
  }

  public function save($con = null)
  {
    // call the parent function to perform the save and get the new user object
    $new_user = parent::save($con); /* @var $user sfGuardUser */

    // add our groups here by looping the current user's group collection
    foreach($this->current_user->getGroups() as $group) /* @var $group sfGuardGroup */
    {
      // we could use $user->addGroupByName($group->name); here, but it would mean
      // an extra db lookup for each group as it validates the name. we know the
      // group must exist, so can just add directly

      // create and save new user group
      $ug = new sfGuardUserGroup();
      $ug->setsfGuardUser($new_user);
      $ug->setsfGuardGroup($group);

      $ug->save($con);
    }

    // make sure we return the user object
    return $new_user;
  }
}

您可以看到我设置了一个类变量来存储当前用户,这不是必需的,但它不必继​​续调用sfContext::getInstance()->getUser()->getGuardUser(),并且随着您的表单变得更加复杂,这是一种很好的做法。

希望这会有所帮助!:)

于 2013-06-13T16:58:20.447 回答