Yii Framework Forum: Beginner Trying To Figure Out Useridentity - Yii Framework Forum

Jump to content

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

Beginner Trying To Figure Out Useridentity Rate Topic: -----

#1 User is offline   Brando 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-January 13

Posted 24 January 2013 - 05:57 AM

Alright so I have begun using yii and completed the blog tutorial and am now trying a very simple website on my own. Unfortunately I have run into a issue with the userIdentity I created a model for a User which holds things like username, passwordhash, passwordsalt, attempted tries etc. I keep getting parsing errors on
$user->tries = $user->tries + 1;
$user->save();

on failed logins. I am not sure how I should be able to do this...Any suggestions? Below is my UserIdentity
<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	 private $_id;
	public function authenticate()
	{
		$user=User::model()->findByAttributes(array('name'=>$this->username));
		if ($user === null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if ($user->passwordHash!==hash('sha512',$this->password . $user->passwordSalt)){
		  $user->tries = $user->tries + 1;
		  $user->save();
      $this->errorCode=self::ERROR_PASSWORD_INVALID;
		}
		else if ($user->tries >= 3)
			$this->errorCode=self::ERROR_STATUS_LOCKED;
		else {
			$this ->_id=$user->Id;
			$this ->setState('title',$user->title);
			$this ->errorCode=self::ERROR_NONE;
			/*$auth = Yii::app()->authManager;
			if(!$auth->isAssigned($user->role, $this->_id))
			{
				if($auth->assign($user->role, $this->_id))
				{
					Yii::app()->authManager->save();
				}
			}*/
		}
		
		return !$this->errorCode;
	}
	
	public function getId()
	{
		return $this-> _id;
	}
}

0

#2 User is offline   Brando 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-January 13

Posted 28 January 2013 - 03:52 PM

I hate to bump, but really need to resolve this issue. Let me know if there is more information needed. Thanks.
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 January 2013 - 05:55 PM

When you do $user->save() the whole record is tryed to be saved and before that the whole record is validated... so it's not good to be used in your case...

instead try with saveAttributes() that saves just the attribute you need and does not validate the record - http://www.yiiframew...tributes-detail
Find more about me.... btw. Do you know your WAN IP?
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