I'm using a component called SimpleAuth and SimpleAcl (instead of the
standard Auth and Acl ones because they are too complicated for my
small project).

SimpleAuth features a getActiveUser() method which returns the
currently logged in user. This method calls $this->Controller, which
is only available when the component's startup(...) method was called
from CakePHP.

In my view I'd like to have a $user object contains all the user's
data and which can also handle stuff like $user->isAllowedTo(...) or
$user->isGuest(). I have created some UserProxy class for this:

class UserProxy {
  var $auth;
  var $acl;
  var $guestId = 1;

  function __construct($auth, $acl) {
    $this->auth = $auth;
    $this->acl = $acl;
    $this->user = $this->auth->getActiveUser(); // This line will be important!
  }

  function name() {
    return $this->user['User']['name'];
  }

  function isGuest() {
    return $this->user['User']['id'] == $this->guestId;
  }
}

To automatically have this $user object available in my views, I want
to set it in a beforeFilter().

  function _prepareUser() {
    $this->user = new UserProxy($this->SimpleAuth, $this->SimpleAcl);
    $this->set('user', $this->user);
  }

This requires the SimpleAuth and SimpleAcl components to already be
initalized, because in $this->auth->getActiveUser() in the constructor
of UserProxy the SimpleAuth components needs access to the controller
and the session:

  function getActiveUser() {
    if ($activeUserId=$this->Controller->Session->read($this->userModel.'.id'))
{
      ...
    }
    ...
  }

And because the startup() method doesn't seem to be called at this
time already, I get the following error:

Warning: Trying to get property of non-object
[APP/controllers/components/simple_auth.php, line 120]
SimpleAuthComponent::getActiveUser() -
APP/controllers/components/simple_auth.php, line 120
UserProxy::__construct() - APP/controllers/app_controller.php, line 10
AppController::_prepareUser() - APP/controllers/app_controller.php, line 37
AppController::beforeFilter() - APP/controllers/app_controller.php, line 28
Controller::startupProcess() -
CORE/cake/libs/controller/controller.php, line 526
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 187
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83

Fatal error: Call to a member function read() on a non-object in
/Users/josh/Sites/working_tests/checking_out_simple_auth_and_simple_acl/app/controllers/components/simple_auth.php
on line 120

So I need a filter that's called before the components are initialized
(that's the beforeFilter()) and a filter that's called after the
initialization of the components but before the controller and view
codes are processed.

So @Miles: no, the beforeFilter() can't be executed after the
components' initialization, because often we set component-specific
parameters in the beforeFilter, like

function beforeFilter() {
  $this->Security->blackHoleCallback = '_blackHole';
  parent::beforeFilter();
}

Anyone got an idea on how to accomplish this? IMHO there should be a
beforeComponents() and an afterComponents() filter...

On Thu, Nov 11, 2010 at 8:43 PM, Miles J <[email protected]> wrote:
> Also forgot to mention that there is an afterFilter() as well.
>
> On Nov 11, 10:30 am, Miles J <[email protected]> wrote:
>> Components are initialized before they get to beforeFilter(), so
>> beforeFilter() should work fine.
>>
>> https://github.com/cakephp/cakephp/blob/master/cake/libs/controller/c...
>>
>> On Nov 11, 9:24 am, Bogdan Bursuc <[email protected]> wrote:
>>
>> > Why do you need one ?
>>
>> > On Thu, Nov 11, 2010 at 7:18 PM, psybear83 <[email protected]> wrote:
>> > > Hi all
>>
>> > > Is there a filter that runs after the initialization of components but
>> > > before beforeRender?
>>
>> > > Thanks for help
>> > > Josh
>>
>> > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others
>> > > with their CakePHP related questions.
>>
>> > > You received this message because you are subscribed to the Google Groups
>> > > "CakePHP" group.
>> > > To post to this group, send email to [email protected]
>> > > To unsubscribe from this group, send email to
>> > > [email protected]<cake-php%[email protected]>For
>> > >  more options, visit this group at
>> > >http://groups.google.com/group/cake-php?hl=en
>>
>> > --
>> > Thanks,
>> > Bogdan Iulian Bursuc
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to