Beginner Trying To Figure Out Useridentity

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;

	}

}



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

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.yiiframework.com/doc/api/1.1/CActiveRecord#saveAttributes-detail