0

I am trying to add a custom Behavior that is a clone of the default Timestamp. Mine is "Userstamp" adding the current user to audit-trail fields on all tables. At the point where Timestamp sets the field to "new FrozenTime($ts)", I want to set to "$identity->get('username')" or similar. I am having trouble reaching anything from Authentication/Authorization or Identity from within the Behavior.

I know that the information is there, somewhere. But what do I need to include in my Behavior class in order to retrieve it? I found this link, but I don't see where to put the suggested code.

In my Table:

public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Userstamp');

        .
        .
        .
}

Cake's timestamp Behavior:

    public function timestamp(?DateTimeInterface $ts = null, bool $refreshTimestamp = false): DateTimeInterface
    {
        if ($ts) {
            if ($this->_config['refreshTimestamp']) {
                $this->_config['refreshTimestamp'] = false;
            }
            $this->_ts = new FrozenTime($ts);
        } elseif ($this->_ts === null || $refreshTimestamp) {
            $this->_ts = new FrozenTime();
        }

        return $this->_ts;
    }

My userstamp Behavior:

    public function userstamp($userstamp = null, bool $refreshUserstamp = false)
    {
// Variations of this do not work, the Property is not available in UserstampBehavior
//        $currentUser = $this->Authentication
//            ->getIdentity()
//            ->getIdentifier();   
        $currentUser = 'abc';   <<<<<<<<<<<<< Hard-coded temporarily

        if ($userstamp) {
            if ($this->_config['refreshUserstamp']) {
                $this->_config['refreshUserstamp'] = false;
            }
            $this->_userstamp = $currentUser;
        } elseif ($this->_userstamp === null || $refreshUserstamp) {
            $this->_userstamp = $currentUser;
        }

        return $this->_userstamp;
    }
DSmith
  • 15
  • 4
  • Check https://github.com/UseMuffin/Footprint. It might do what you want out of the box. If not, it'll probably provide inspiration. – Greg Schmidt Jan 27 '22 at 20:54
  • Thank you, @GregSchmidt. That plugin does head in the direction I wanted. I decided that it was overkill for my purposes just now. I went with the more manual route, of updating my audit trail in each controller as the record is added or edited. Thank you for the helpful response! – DSmith Jan 28 '22 at 18:07

1 Answers1

0

Your Auth-Informations lives also in the session.

So you can access Session-stuff in a table-class like this: (new Session)->read('Auth')

And therefore you give this information from a table-class to your Behaviour like this:

$this->addBehavior('Userstamp', ['user_info'=>(new Session)->read('Auth')]);

Then you can access this information in your behaviour:

public function initialize(array $config){
    $this->user_info = $config['user_info'];
}

public function myFunction(){
   // do something with $this->user_info
}
Steve Kirsch
  • 175
  • 1
  • 10