<p>If you know the user ID at the point the task is created, you can pass it in as a property of the task class. Here's an example queue task class:</p>
<pre><code>class myQueueJob extends BaseJob
{

    // Public Properties

    /** @var string */
    public $userId;

    // Public Methods

    /**
     * @param QueueInterface|Queue $queue
     * @return mixed|void
     * @throws Exception
     */
    public function execute($queue)
    {
        $user = Craft::$app-&gt;getUsers()-&gt;getUserById($this-&gt;userId);

        // Do queue job stuff
    }
}
</code></pre>
<p>When the task is created, the user ID can be passed in. For example, this would pass in the user ID of the current user, assuming the code was executed as part of a web request:</p>
<pre><code>// Get current user
$user = Craft::$app-&gt;getUser()-&gt;getIdentity()

Craft::$app-&gt;getQueue()-&gt;delay(0)-&gt;push(new 
    myQueueJob([
        'userId' =&gt; $user-&gt;id
    ])
);
</code></pre>
<p>You could look the user up by ID, rather than using the current user, or even pass in a static ID.</p>

CC BY-SA 4.0
2022-05-03T10:06:49.157Z
2022-05-03T10:06:49.157Z
Martin Spain
<p>Are you running this task as part of a web request or a console request? The <code>craft\console\User</code> indicates that it's a console request. For console requests, there's no current user, as the console command isn't associated with any session or user account. In this case, you're gonna have to use a user query to find the user account you want to save info on:</p>
<pre class="lang-php prettyprint-override"><code>use craft\elements\User;

// add whatever conditions you want
$user = User::find()-&gt;id(123)-&gt;one();
</code></pre>
<p>For a web request <code>Craft::$app-&gt;getUser()-&gt;getIdentity()</code> is the correct way to get the current user. If it's returning null, it means the current request is performed by a guest, so there's no current user.</p>

CC BY-SA 4.0
2022-04-29T09:21:24.220Z
2022-04-29T09:21:24.220Z
MoritzLost
<p>I've been trying to get the user id when running a task to save it on a table but cannot find a way to get it not even the user info.</p>
<p>Using <code>Craft::$app-&gt;getUser()</code> or <code>Craft::$app-&gt;user</code> I get:</p>
<pre><code>[&quot;_identity&quot;:&quot;craft\console\User&quot;:private]=&gt;
  bool(false)
[&quot;_events&quot;:&quot;yii\base\Component&quot;:private]=&gt;
  array(0) {
}
[&quot;_eventWildcards&quot;:&quot;yii\base\Component&quot;:private]=&gt;
  array(0) {
}
[&quot;_behaviors&quot;:&quot;yii\base\Component&quot;:private]=&gt;
NULL
</code></pre>
<p>which is not good and if I try with <code>Craft::$app-&gt;getUser()-&gt;getIdentity()</code> I get an error.</p>
<p>Is there a way to get the user information within a task?.</p>
<p>Thank you for all your help!</p>

CC BY-SA 4.0
2022-04-29T09:18:18.667Z
What is the error you're getting?
MoritzLost
<craft3><users>
How to get user info in a custom plugin task?
# How to get user info in a custom plugin task?

(Asked by: username_0 on 2022-04-28)

I've been trying to get the user id when running a task to save it on a table but cannot find a way to get it not even the user info.
Using Craft::$app->getUser() or Craft::$app->user I get:
["_identity":"craft\console\User":private]=>
  bool(false)
["_events":"yii\base\Component":private]=>
  array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
  array(0) {
}
["_behaviors":"yii\base\Component":private]=>
NULL

which is not good and if I try with Craft::$app->getUser()->getIdentity() I get an error.
Is there a way to get the user information within a task?.
Thank you for all your help!

**Tags:** craft3, users

> **Comments:**
> **username_1**: What is the error you're getting?  

---

## 2 answers

### Answer by username_2 on 2022-05-03. Score: 2

If you know the user ID at the point the task is created, you can pass it in as a property of the task class. Here's an example queue task class:
class myQueueJob extends BaseJob
{

    // Public Properties

    /** @var string */
    public $userId;

    // Public Methods

    /**
     * @param QueueInterface|Queue $queue
     * @return mixed|void
     * @throws Exception
     */
    public function execute($queue)
    {
        $user = Craft::$app->getUsers()->getUserById($this->userId);

        // Do queue job stuff
    }
}

When the task is created, the user ID can be passed in. For example, this would pass in the user ID of the current user, assuming the code was executed as part of a web request:
// Get current user
$user = Craft::$app->getUser()->getIdentity()

Craft::$app->getQueue()->delay(0)->push(new 
    myQueueJob([
        'userId' => $user->id
    ])
);

You could look the user up by ID, rather than using the current user, or even pass in a static ID.


---

### Answer by username_1 on 2022-04-29. Score: 2

Are you running this task as part of a web request or a console request? The craft\console\User indicates that it's a console request. For console requests, there's no current user, as the console command isn't associated with any session or user account. In this case, you're gonna have to use a user query to find the user account you want to save info on:
use craft\elements\User;

// add whatever conditions you want
$user = User::find()->id(123)->one();

For a web request Craft::$app->getUser()->getIdentity() is the correct way to get the current user. If it's returning null, it means the current request is performed by a guest, so there's no current user.


---

