bedezign / yii2-audit

Yii2 Audit records and displays web/cli requests, database changes, php/js errors and associated data.
https://bedezign.github.io/yii2-audit/
Other
193 stars 112 forks source link

CLI Don't save user identity #210

Closed marcoadasilvaa closed 6 years ago

marcoadasilvaa commented 7 years ago

Hello team.

When have a cron with console command, don't register the user_id provider by Yii::$app->user->identity->id. Audit set by default "Guest"

image

I set identity user with next code:

<?php

namespace app\commands;

use yii;
use yii\console\Controller;
use app\models\User;
use app\models\Subscription;
use app\models\SubscriptionOrder;
use app\models\Setting;

/**
 * Cron Tracking Controller
 */
class CronTrackingController extends Controller 
{
    public $user = 'interfazrastreo';

    /**
     *  Login User interface
     */
    public function init() {
        parent::init();
        Yii::$app->user->setIdentity(User::findOne(['username' => $this->user]));
    }
...

But I can not register the authenticated user console

Blizzke commented 7 years ago

It's not saving the user id on console because by default the console does not do users. We explicitly check for this.

You'll have to add this manually to the entry. You could perhaps link to the onAfterRequest event before the Audit module is loaded and manually add the user id. It is a bit of an edge case that we can't support.