terrid25
terrid25

Reputation: 1946

symfony - adding a user to a group (sfGuardUserGroup) in a form

I'm trying to save some users in a custom admin form and I'd like to set them in a particular group, in the sfGuardUserGroup.

So If the user I've just created has an id of 25, then I'd expect an entry in the sfGuardUserGroup table with a user_id of 25 and a group_id of 8 (8 is my group id I want o add these users to.)

Could I do this in the form class, or in the processForm action?

I'm using doctrine and SF1.4

Thanks

Upvotes: 1

Views: 1313

Answers (2)

evilcelery
evilcelery

Reputation: 16139

This should do what you need:

<?php

class AdminUserForm extends sfGuardUserForm
{
  public function configure()
  {
    //customise form...
  }

  public function save($con = null)
  {
    //Do the main save to get an ID
    $user = parent::save($con);

    //Add the user to the relevant group, for permissions and authentication
    if (!$user->hasGroup('admin'))
    {
      $user->addGroupByName('admin');
      $user->save();
    }

    return $user;
  }
}

Upvotes: 1

d.syph.3r
d.syph.3r

Reputation: 2215

If you require this behaviour for all sfGuardUser's created you should put this logic in the model for sfGuardUser class. [example below]

// sfGuardUser class
public function save(Doctrine_Connection $conn = null) {

    if (!$this->hasGroup('group_name'))
        $this->addGroupByName('group_name', $conn);

    parent::save($conn);

}

If you require this functionality only on this specific form, you should put the logic within the form. Adding logic to the processForm action would be incorrect as you would be placing business logic within the controller.

Upvotes: 0

Related Questions