This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Reboot
Reboot

I know it's possible to obtain the current user through a plugin's controller, but is there a way to get the current user while inside a plugin model?

Basically I'm trying to use the beforeSave model event to set the current users id as the "last_edited_by" field:

public function beforeSave()
{
    $this->last_edited_by = //need to get the current logged in users id
}

I also tried writing a custom update controller action to do the job. I was able to get the current logged in user's id, but I couldn't figure out how to pass that to the standard form behavior method:

public function update($recordId, $context = null)
{
    //provides the current user's ID, but how do I pass it to the update method below?
    //$this->user->attributes['id'] 

    // Call the FormController behavior update() method
    return $this->asExtension('FormController')->update($recordId, $context);
}

It looks like the controller is the place to handle logic like that so I would prefer to use the controller update action to accomplish the task.

Any advice/guidance is greatly appreciated.

Last updated

alxy
alxy

If you are rerering to user of the Rainlab.User plugin, there is Auth::getUser(): https://github.com/rainlab/user-plugin/blob/master/components/Account.php#L75

Reboot
Reboot

Unfortunately no, I was referring to basic user handling that OctoberCMS provides out of the box. I was hoping that there was a method or object that I could access that has the user data that I need.

Daniel81
Daniel81

Try:

use BackendAuth;
$user = BackendAuth::getUser();
$this->last_edited_by = $user->first_name;

Last updated

Reboot
Reboot

That did it! Thanks!

Troiscent
Troiscent

How to get the current user and his permissions into the frontend ? (twig) ?

xyz.qtc63355
xyz.qtc63355

use Auth;
if (!Auth::check()) { return null; } $user = Auth::getUser();

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.