Yii Framework Forum: Simple question: authenticate - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple question: authenticate Rate Topic: -----

#1 User is offline   heuri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-October 10

Posted 30 October 2010 - 03:05 PM

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 :)
                else
                     // FUNCTION TO SET THE USER TO NORMAL - I'm just a normal user :(
            		
            }
        }
        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.yiiframew...ation-hierarchy

Thank you very much!
0

#2 User is offline   Parcouss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 12-September 10
  • Location:Toulouse, France

Posted 30 October 2010 - 07:39 PM

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 :)
                     $this->setState('amin')
                else
                     // FUNCTION TO SET THE USER TO NORMAL - I'm just a normal user :(
                */
                $this->setState('admin', $user->admin == 1)
        
            }
        }
        return !$this->errorCode;
    } 


You can use then:

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

0

#3 User is offline   heuri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-October 10

Posted 31 October 2010 - 06:00 AM

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;
}

0

#4 User is offline   diggy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 112
  • Joined: 18-November 09
  • Location:Ukraine, Khmelnitsky

Posted 31 October 2010 - 06:24 AM

View Postheuri, on 31 October 2010 - 06:00 AM, said:

 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((!Yii::app()->user->isGuest && Yii::app()->user->admin)?Yii::app()->user->name:'admin'),
                ),
            array('deny', // deny all users
                'users' => array('*'),
                ),
            );
    }



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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users