2013-06-12 54 views
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.'))); 
} 

現在我需要創造這些新用戶與用戶登錄同一組(S),但我不知道怎麼辦。我讀this post,但不知道如果使用getGroups()將做的工作,我的意思是設置groups_list默認,任何建議?什麼是最好的方法來做到這一點?

回答

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(),並且隨着您的表單變得越來越複雜,這是很好的做法。

希望這有助於! :)

+0

好的答案+1,現在我想知道,而不是保存所有組作爲主要職位說,只是爲了保存讓我們說組稱爲「組1」,這是可能的嗎?怎麼樣? – Reynier

+0

謝謝@Reynier!如果你知道組名,只需用'$ new_user-> addGroupByName('你的組名')替換'foreach';' – moote

+0

優秀,它的工作原理。最後一個問題:我和'sfGuardUserProfile'表有關係,我也想爲新創建的用戶設置'$ id_empresa',怎麼樣?我可以通過save()方法訪問'BasesfGuardUser'類中的'setSfGuardUserProfile()'方法嗎? – Reynier