Page De Login Non Liée À La Table

Bonjour à tous,

Je suis actuellement nouveau dans la communauté, j’ai commencé il y a deux semaines et pour apprendre le framework un peu plus en profondeur, je crée actuellement un petit jeu du plus ou du moins avec inscription et authentification.

J’ai crée une BDD MySQL avec 3 table pour gérer les parties, et 1 table User qui permet de gérer les utilisateurs. J’utilise le générateur Gii avec les extensions AweModel et AweCRUD.

Ma page d’enregistrement fonctionne bien (mis à part la gestion de l’erreur si les informations données par l’utilisateur existent déjà dans la BDD), mais la page de Login qui a été générée automatiquement pour l’application n’est apparement pas reliée à ma BDD, car elle ne retrouve pas les Logins. J’ai bien référencée ma BDD dans mon main.config, mais impossible de se log après s’être inscrit.

Comment je peux faire pour lier un model déjà crée à une base de donnée ? Où se configure ça ? (Ca devient compliqué si on doit créer une table spécifique pour chaque modele fonctionnel de l’application)

Merci d’avance pour vos réponses !

Bienvenue ici et avec Yii!

Pour ton petit jeu, vu les disparités, il faudrait tout d’abord savoir si tu es parti sur la v.2.0?

La V2.0 de quoi ? Je suis sur la 1.1.14 de Yii, avec les extensions Awemodel, Awecrud et bootstrap.

Il faut modifier le fichier /protected/components/UserIdentity.php


<?php

/**

* UserIdentity represents the data needed to identity a user.

* It contains the authentication method that checks if the provided

* data can identify 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.

*/

public function authenticate()

{

$users=array(

// username => password

'demo'=>'demo',

'admin'=>'admin',

);

if(!isset($users[$this->username]))

$this->errorCode=self::ERROR_USERNAME_INVALID;

else if($users[$this->username]!==$this->password)

$this->errorCode=self::ERROR_PASSWORD_INVALID;

else

$this->errorCode=self::ERROR_NONE;

return !$this->errorCode;

}

}

les utilisateur peuvent se connecter avec demo/demo ou admin/admin .

Pour pouvoir utiliser les informations dans la base de donné il faut donc modifier la valeur de $user.




// pour pouvoir utiliser Yii::app()->user->id. 

private $_id;

	/**

	 * 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.

	 */

	public function authenticate()

	{

		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));

		if($user===null)

				$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if(!$user->$this->password)

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

			{

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

				$this->username=$user->username;

			}

			$this->errorCode=self::ERROR_NONE;

			return $this->errorCode==self::ERROR_NONE;

	}

	public function getId()

	{

	return $this->_id;

	}

ça devrait marcher. ;)

Après quelques modifications, j’en arrive à une erreur ou mon $Password_user ne peut pas être transformer en string.

Object of class UserIdentity could not be converted to string

C:\UwAmp\www\yii\Plus_ou_moins\protected\components\UserIdentity.php(28)


 




16      * @return boolean whether authentication succeeds.

17      */

18      

19      /** pour pouvoir utiliser Yii:app()->user->id.

20      */

21     private $_id;

22      

23     public function authenticate()

24     {

25         $user=UserForm::model()->find('LOWER(Name_user)=?', array(strtolower($this->Name_user)));

26         if($user===null)

27             $this->errorCode=self::ERROR_NAME_USER_INVALID;

28         else if(!$user->$this->Password_user)

29             $this->errorCode=self::ERROR_PASSWORD_USER_INVALID;

30         else

31             {

32                 $this->_id=$user->user_id;

33                 $this->Name_user=$user->Name_user;

34             }

35             $this->errorCode=self::ERROR_NONE;

36             return $this->errorCode==self::ERROR_NONE;

37     }

38     public function getId()

39     {

40     return $this->_id;




Mon CUserIdentity :


<?php

/**

 * CUserIdentity class file

 *

 * @author Qiang Xue <qiang.xue@gmail.com>

 * @link http://www.yiiframework.com/

 * @copyright 2008-2013 Yii Software LLC

 * @license http://www.yiiframework.com/license/

 */


/**

 * CUserIdentity is a base class for representing identities that are authenticated based on a Name_user and a Password_user.

 *

 * Derived classes should implement {@link authenticate} with the actual

 * authentication scheme (e.g. checking Name_user and Password_user against a DB table).

 *

 * By default, CUserIdentity assumes the {@link Name_user} is a unique identifier

 * and thus use it as the {@link id ID} of the identity.

 *

 * @property string $id The unique identifier for the identity.

 * @property string $name The display name for the identity.

 *

 * @author Qiang Xue <qiang.xue@gmail.com>

 * @package system.web.auth

 * @since 1.0

 */

class CUserIdentity extends CBaseUserIdentity

{

	/**

	 * @var string Name_user

	 */

	public $Name_user;

	/**

	 * @var string Password_user

	 */

	public $Password_user;


	/**

	 * Constructor.

	 * @param string $Name_user Name_user

	 * @param string $Password_user Password_user

	 */

	public function __construct($Name_user,$Password_user)

	{

		$this->Name_user=$Name_user;

		$this->Password_user=$Password_user;

	}


	/**

	 * Authenticates a user based on {@link Name_user} and {@link Password_user}.

	 * Derived classes should override this method, or an exception will be thrown.

	 * This method is required by {@link IUserIdentity}.

	 * @return boolean whether authentication succeeds.

	 */

	public function authenticate()

	{

		throw new CException(Yii::t('yii','{class}::authenticate() must be implemented.',array('{class}'=>get_class($this))));

	}


	/**

	 * Returns the unique identifier for the identity.

	 * The default implementation simply returns {@link Name_user}.

	 * This method is required by {@link IUserIdentity}.

	 * @return string the unique identifier for the identity.

	 */

	public function getId()

	{

		return $this->Name_user;

	}


	/**

	 * Returns the display name for the identity.

	 * The default implementation simply returns {@link Name_user}.

	 * This method is required by {@link IUserIdentity}.

	 * @return string the display name for the identity.

	 */

	public function getName()

	{

		return $this->Name_user;

	}

}

Oups… J’ai ecrit n’importe quoi

Le code devrait plutot ressembler à ceci!(/protected/components/UserIdentity.php)




<?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

{


	private $_id;

	/**

	 * 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.

	 */

	public function authenticate()

	{

		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));

		if($user===null)

				$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if(!$user->password==$this->password)

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

			{

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

				$this->username=$user->username;

//				$this->setState('lastLogin', date("m/d/y g:i A", strtotime($user->last_login_time)));

/*				$user->saveAttributes(array(

'last_login_time'=>date("Y-m-d H:i:s", time()),

));*/

			}

			$this->errorCode=self::ERROR_NONE;

			return $this->errorCode==self::ERROR_NONE;

	}

	public function getId()

	{

	return $this->_id;

	}

}



else if(!$user->$this->Password_user) ne veut rien dire!!! ::)

Désolé Tyrfing33!

Maintenant ça marche!

En effet merci beaucoup =)