Home > Actions, Plugins, View > Accessing sfGuardPlugin user id.

Accessing sfGuardPlugin user id.

September 29th, 2009

If you are using sfGuardPlugin all you need to get id of a user that is currently logged in is this:

In your actions.class.php

$user_id = $this->getUser()->getGuardUser()->getId();

But this will cause fatal error if the user is not authenticated. We can prevent this with isAuthenticated().
// /apps/frontend/lib/myUser.class.php

class myUser extends sfGuardSecurityUser
{
     public function getUserId()
     {
        if($this->isAuthenticated())
           return $this->getGuardUser()->getId();
        else
           return 0;
      }
}

Mariusz Actions, Plugins, View

  1. October 1st, 2009 at 10:54 | #1

    Why don’t use proxy method and test only:

    $this->getUser()->isAuthenticated()

    Daniel

  2. October 1st, 2009 at 11:13 | #2

    @Daniel Londero
    True, now when I look at that part of the code, it’s pointless. Let me remove it:)

  1. No trackbacks yet.