I’m creating a console app that updates user’s statuses. I’m running into a problem where my User model is calling Yii::app()->user->isAdmin.
The console command is throwing exception that CConsoleCommand doesn’t have a method named getSession(). This is coming from the CWebUser init() method which is being invoked when the app tries to load the User component.
Am I supposed to be using the CWebUser component in a console app or another console specific component?
Has anybody else run into this issue? Any workarounds?
User Model: Yii::app()->user->isAdmin is instantiating the User component
I solved it by implementing IWebUser but your solution is cleaner so I’ll probably switch.
<?php
class ConsoleUser extends CApplicationComponent implements IWebUser
{
public $primaryKey;
private $admin;
public function init()
{
parent::init();
if (!isset($this->primaryKey))
throw new Exception('You must set the "primary key" of the user
to execute the console application.');
$user = User::model()->findByPk($this->primaryKey);
if ($user)
{
if ($user->role_id != Role::ROLE_ADMIN)
{
throw new Exception('User does not have an Admin role.');
}
$this->admin = $user;
}
else
throw new Exception('Could not find Admin User to execute console application.');
}
/**
* Returns a value that uniquely represents the identity.
* @return mixed a value that uniquely represents the identity (e.g. primary key value).
*/
public function getId()
{
return $this->admin->id;
}
/**
* Returns the display name for the identity (e.g. username).
* @return string the display name for the identity.
*/
public function getName()
{
return $this->admin->username;
}
/**
* Returns a value indicating whether the user is a guest (not authenticated).
* @return boolean whether the user is a guest (not authenticated)
*/
public function getIsGuest()
{
return false;
}
/**
* Performs access check for this user.
* @param string $operation the name of the operation that need access check.
* @param array $params name-value pairs that would be passed to business rules associated
* with the tasks and roles assigned to the user.
* @return boolean whether the operations can be performed by this user.
*/
public function checkAccess($operation, $params=array())
{
return true;
}
public function getIsAdmin()
{
return true;
}
public function setFlash($key,$value,$defaultValue=null)
{
}
}
?>
Don’t know if this helps anyone, but I had this same problem and decided to approach it by creating a ConsoleUser class that extends CApplicationComponent and has a member that maps to a User object in my system. If anyone wants details I’d be glad to share.