Console App and WebUser

Hi,

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




    public function behaviors()

    {

        return array(

            'CTimestampBehavior' => array(

                'class' => 'zii.behaviors.CTimestampBehavior',

                'createAttribute' => 'create_time',

                'updateAttribute' => (Yii::app()->user->isAdmin) ? 'admin_update_time' : 'user_update_time',

            ),

            'NotificationBehavior' => array(

                'class' => Yii::getPathOfAlias('behaviors') . '.NotificationBehavior',

            ),

        );

    }



CWebUser Init method:




        public function init()

	{

		parent::init();

		Yii::app()->getSession()->open();

		if($this->getIsGuest() && $this->allowAutoLogin)

			$this->restoreFromCookie();

		else if($this->autoRenewCookie && $this->allowAutoLogin)

			$this->renewCookie();

		if($this->autoUpdateFlash)

			$this->updateFlash();


		$this->updateAuthStatus();

	}



Cheers,

Matt

That’s exactly the reason why I removed CTimestampBehavior, at least from those classes used in my console commands.

It’s not a big deal to handle the timestamps ‘manually’.

A console user is admin by definition, because only admins can run them.

Try


'updateAttribute' => (Yii::app() instanceof CConsoleApplication || Yii::app()->user->isAdmin) ? 'admin_update_time' : 'user_update_time',

Y!!-Haw! :D

That’s a neat solution.

Thanks for the info.

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)

    {

        

    }

}

?>



Cheers,

Matt

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.

I want to implement the same solution.

Still able to share your code?

Thanks