Yii::app()->user in console applications

How to use Yii::app()->user in console applications?

I know it is expected to use for web applications only, but my usecase is the following:

  1. My objects for web application (Image/Videos/Posts etc) if user is not signed in are created from Anonymous Users Id = 1. This behavior is described in model beforeValidate rules.

  2. When I try to access current user state in console app - I always get exception ‘CException’ with message 'Object configuration must be an array containing a “class” element. Even if I try to check Yii::app()->user

The only workaround then comes to my mind is a try catch block here…

Can’t we just create a fake user? or a console user? and assign that user to Yii::app()->user when in console mode, aka in console configuration file.

I never got this exception, but maybe you can just add a line in console configuration file:




'user'=>array(

   'class'=>'CWebUser',

   ...

),



I am just guessing by the description of the exception, I never tried but really looks like that he is complaining about this line.

I’d go with jamesmoey’s proposal, too and create something like:


class DummyWebUser extends CApplicatinComponente implementes IWebUser 



All you should have to implement are the methods defined in IWebUser interface.

EDIT:

You could even add more configuration properties to let the dummy user component behave like a logged in/guest user, in case you want to test that too. Or you create sublcasses like DummyGuestUser and DummyLoggedInUser.

A possible implementation can be found in the Thread: Console App and WebUser