0

We have a simple CakePHP 4 development with CakePHP authentication 2 which works almost fine...

With AuthenticationComponent we can access the logged in User with $this->Authentication->getIdentity() in every Controller;

We want to add the logged-in User into any add/edit request...

Therefore we wrote behavior that works and add´s a string to every add/edit...

But how can we access the AuthenticationComponent in a Models Behavior? I´ve found no way...

Thanks for every hint...

majamuel
  • 31
  • 4
  • You want something like what [this plugin](https://github.com/UseMuffin/Footprint) provides? – Greg Schmidt Jan 19 '21 at 15:08
  • Yes... correct... something like that... And for now we have made it... The specialthing was to give the username to Model... You have to go over Instance Manager... – majamuel Jan 22 '21 at 07:09

1 Answers1

0

The Solution to get Userdata to a Behavior

    EventManager::instance()->on(
        'Model.beforeSave',
        ['priority' => -1],
        function (Event $event, EntityInterface $entity, \ArrayObject $options) {
            // retrieve the user id from the auth component
            if(!empty($this->Authentication->getIdentity())){
                $options['user_id'] = $this->Authentication->getIdentity()->get('longname');
            }
        }
    );
majamuel
  • 31
  • 4