I see.... I would re-do that at the controller level:
class AuditLogger implements \Cake\Event\EventListener {
public function construct(AuthComponent $auth) {
$this->_auth = $auth;
}
public function implementedEvents() {
return ['Model.afterSave' => 'logSave'];
}
public function logSave($event, $entity, $options) {
$logs->save(new Entity(['user_id' => $this->auth->user('id'), ...]));
}
}
And in your Controller:
public function beforeFilter() {
....
$this->Customers->eventManager()->attach(new AuditLogger($this->Auth));
}
This way you keep your separation of concerns and have smaller and more
testable classes that you can reuse :)
On Wednesday, July 23, 2014 12:56:51 PM UTC+2, Stefano Zoffoli wrote:
>
> Yes of course:
> I have a CustomersTable, a UsersTable and a LogsTable. The LogsTable has
> basically 4 fields: id, user_id, description, created.
>
> When an authenticated user saves a customer, i'd like to log this user
> action. So, in CustomersTable I added a callback method like this:
>
> public function afterSave(Event $event, Entity $entity, array $options) {
> $logs = TableRegistry::get('Logs');
> $log = $logs->newEntity([
> 'user_id' => AuthComponent::user('id'),
> 'description' => 'Saved customer',
> ]);
> $logs->save($log);
> }
>
> But AuthComponent::user() is not static and gave me fatal error.
>
>
> --
> Stefano Zoffoli
> *Resp. Sviluppo Software*
> 347 8180258
>
> *Librasoft Snc*
> www.LibrasoftSnc.it <http://www.librasoftsnc.it/>
> www.ProgettoKuma.it <http://www.progettokuma.it/>
> 0543 424612
>
>
> 2014-07-23 12:40 GMT+02:00 José Lorenzo <[email protected]>:
>
>> That's not possible anymore. We are currently investigating clean ways of
>> implementing this use case. May I ask why you need the session in the table?
>>
>>
>> On Wednesday, July 23, 2014 11:53:48 AM UTC+2, Stefano Zoffoli wrote:
>>>
>>> Hi,
>>> i'd like to read user auth infos in a Table Class (for example i want to
>>> log which user has saved a given entity in a AfterSave callback method).
>>>
>>> In cakephp 2 there was AuthComponent::user() static method, but in
>>> cakephp 3 that method is not static anymore, and I can't find a simple way
>>> to access that information outside of Controllers.
>>>
>>> How can i do that?
>>> Thank you
>>>
>>> --
>>> Stefano Zoffoli
>>> *Resp. Sviluppo Software*
>>> 347 8180258
>>>
>>> *Librasoft Snc*
>>> www.LibrasoftSnc.it <http://www.librasoftsnc.it/>
>>> www.ProgettoKuma.it <http://www.progettokuma.it/>
>>> 0543 424612
>>>
>> --
>> Like Us on FaceBook https://www.facebook.com/CakePHP
>> Find us on Twitter http://twitter.com/CakePHP
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "CakePHP" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/cake-php.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.