0

独自の登録フォームを使用して、新しいユーザーを sfDoctrineGuard テーブルに追加しようとしています。これは私が作ったconfigure関数です:

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 に答える