Simple question: authenticate

Hello,

I’m quite new with this framework and I have a simple question regarding the authenticate:

I modified the function authenticate to this:


public function authenticate()

    {

        $user = Users::model()->findByAttributes(array('user' => $this->username));

        if ($user === null) {

            $this->errorCode = self::ERROR_USERNAME_INVALID;

        } else {

            if ($user->password !== $user->encrypt($this->password)) {

                $this->errorCode = self::ERROR_PASSWORD_INVALID;

            } else {

                $this->_id = $user->id;

                if (null === $user->lastlogin) {

                    $lastLogin = time();

                } else {

                    $lastLogin = strtotime($user->lastlogin);

                }

                $this->setState('lastlogin', $lastLogin);

                $this->errorCode = self::ERROR_NONE;

           	if($user->admin == 1)

                     // FUNCTION TO SET THE USER TO ADMIN? - Admin right rules <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

                else

                     // FUNCTION TO SET THE USER TO NORMAL - I'm just a normal user <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

            		

            }

        }

        return !$this->errorCode;

    }

Exists there any call to set simple the user to admin or to a normal user like the default configuration "demo/demo" and "admin/admin" ?

Or I really need to create a Authorization Hierarchy?: http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#defining-authorization-hierarchy

Thank you very much!

I think you can do something like this:




public function authenticate()

    {

        $user = Users::model()->findByAttributes(array('user' => $this->username));

        if ($user === null) {

            $this->errorCode = self::ERROR_USERNAME_INVALID;

        } else {

            if ($user->password !== $user->encrypt($this->password)) {

                $this->errorCode = self::ERROR_PASSWORD_INVALID;

            } else {

                $this->_id = $user->id;

                if (null === $user->lastlogin) {

                    $lastLogin = time();

                } else {

                    $lastLogin = strtotime($user->lastlogin);

                }

                $this->setState('lastlogin', $lastLogin);

                $this->errorCode = self::ERROR_NONE;

                /*if($user->admin == 1)

                     // FUNCTION TO SET THE USER TO ADMIN? - Admin right rules <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

                     $this->setState('amin')

                else

                     // FUNCTION TO SET THE USER TO NORMAL - I'm just a normal user <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

                */

                $this->setState('admin', $user->admin == 1)

        

            }

        }

        return !$this->errorCode;

    } 

You can use then:




if (Yii;:app()->user->admin) {

   ...

}



Thank you, works great!

I added to the default accessRules simply my global function:


 public function accessRules()

    {

        return array(

            array('allow', // allow all users to perform 'index' and 'view' actions

                'actions' => array('shops', 'clubs', 'fields'),

                'users' => array('*'),

                ),

            array('allow', // allow admin user to perform 'admin' and 'delete' actions

                'actions' => array('admin', 'delete', 'create', 'update', 'index', 'view'),

                'users' => array(getAdmin()),

                ),

            array('deny', // deny all users

                'users' => array('*'),

                ),

            );

    }

globals.php




function getAdmin()

{

    if (!Yii::app()->user->isGuest) {

        $adminusers = (Yii::app()->user->admin) ? Yii::app()->user->name : 'admin';

    } else

        $adminusers = 'admin';

    return $adminusers;

}

You can make it more simple than using globals.php which is bad way to extend your application functionality B)