error handling in CWebUser

CWebUser component relies on session component and that is correct in CWebApplication. However when you use Yii::app()->user in console application - you will get error:

CConsoleApplication and its behaviors do not have a method or closure named "getSession"

I think it would be nice if CWebUser could handle such situation and return Guest user or something like that without error (this behaviour can be controlled by some switch) instead of just throwing exception…

You can’t even use some DummySession component, because CWebUser calls Yii::app()->getSession() instead of Yii::app()->session or Yii::app()->getComponent(‘session’); and ‘getSession’ method is only in CWebApplication.

Now - real life situation:

I have behavior BlameableBehavior, which just set who and when modified record (fields created_at, created_by, etc). Behavior assume that there is ‘user’ component (I made my ‘console.php’ config file extend ‘main.php’ config just like ‘test.php’ does, so I have ‘user’ component defined). When working in CWebApplication - works like a charm, but using same model (with behaviors) in CConsoleApplication is not so easy…

I am just thinking loud - if anyone has any cons just write them in this topic. Pros are also welcome :)

CWebUser should be used just in web applications, not console applications.

Like you concluded, in console application, there are no sessions too.

I don’t think this feature should be in core, maybe you can write new class that will extend CWebUser, and that you would be able to use in both web and console applications.