Yii-User, Yii-Rights E Login Con Email

Ciao,

uso yii da diverso tempo ma è la prima volta che ho bisogno di autenticare gli utenti.

Ho la mia applicazione già impostata e funzionante per permettere il login con email: per farlo ho usato yii standard e yii-rights. E tutto funge.

Ora ho bisogno di aggiungere yii-user: ho cercato un bel po’ e mi sono letto un po’ di post (qui e sul forum principale) ma non riesco bene a capire cosa fare per utilizzare il login con email in yii-user, dal momento che il modulo crea comunque un campo email e uno username.

Suggerimenti? Link che spieghino?

EDIT: le mie perplessità nascono dal fatto che vorrei utilizzare un model diverso dallo standard User di yii-user. Nel frattempo (non tenevo la piscia) ho letto altro materiale e finalmente attivato il modulo yii-user nella mia applicazione… Considerando anche che è sempre meglio riutilizzare quello che già c’è, credo che utilizzerò il model User standard, lavorando sui ProfileFields per ottenere le stesse funzionalità del mio model per gli utenti.

Grazie per qualsiasi aiuto.

rash*

io il login l’ho implementato con l’email:

questa la classe User.php




<?php


class User extends CActiveRecord

{

	const STATUS_NOACTIVE=0;

	const STATUS_ACTIVE=1;

	const STATUS_BANED=-1;

	

	/**

	 * The followings are the available columns in table 'users':

	 * @var integer $id

	 * @var string $password

	 * @var string $email

	 * @var string $activkey

	 * @var integer $createtime

	 * @var integer $lastvisit

	 * @var integer $superuser

	 * @var integer $status

	 */


	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return Yii::app()->getModule('user')->tableUsers;

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		

		return ((Yii::app()->getModule('user')->isAdmin())?array(

			array('email', 'email'),

			array('email', 'unique', 'message' => UserModule::t("Indirizzo email già presente nel database.")),

			array('status', 'in', 'range'=>array(self::STATUS_NOACTIVE,self::STATUS_ACTIVE,self::STATUS_BANED)),

			array('superuser', 'in', 'range'=>array(0,1)),

			array('email, password, createtime, lastvisit, superuser, status', 'required'),

			array('createtime, lastvisit, superuser, status', 'numerical', 'integerOnly'=>true),

		)<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />(Yii::app()->user->id==$this->id)?array(

			array('email', 'required'),

			array('email', 'email'),

			array('email', 'unique', 'message' => UserModule::t("Indirizzo email già presente nel database.")),

		):array()));

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		$relations = array(

			'profile'=>array(self::HAS_ONE, 'Profile', 'id'),

		);

		if (isset(Yii::app()->getModule('user')->relations)) $relations = array_merge($relations,Yii::app()->getModule('user')->relations);

		return $relations;

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'email'=>UserModule::t("E-mail"),

			'password'=>UserModule::t("Password"),

			'id' => UserModule::t("Id"),

			'activkey' => UserModule::t("Chiave di Attivazione"),

			'createtime' => UserModule::t("Data di Registrazione"),

			'lastvisit' => UserModule::t("Ultima Visita"),

			'superuser' => UserModule::t("Amministratore"),

			'status' => UserModule::t("Stato"),

		);

	}

	

	public function scopes()

    {

        return array(

            'active'=>array(

                'condition'=>'status='.self::STATUS_ACTIVE,

            ),

            'notactvie'=>array(

                'condition'=>'status='.self::STATUS_NOACTIVE,

            ),

            'banned'=>array(

                'condition'=>'status='.self::STATUS_BANED,

            ),

            'superuser'=>array(

                'condition'=>'superuser=1',

            ),

            'notsafe'=>array(

            	'select' => 'id, password, email, activkey, createtime, lastvisit, superuser, status',

            ),

        );

    }

	

	public function defaultScope()

    {

        return array(

        	'select' => 'id, email, createtime, lastvisit, superuser, status',

        );

    }

	

	public static function itemAlias($type,$code=NULL) {

		$_items = array(

			'UserStatus' => array(

				self::STATUS_NOACTIVE => UserModule::t('Non Attivo'),

				self::STATUS_ACTIVE => UserModule::t('Attivo'),

				self::STATUS_BANED => UserModule::t('Bloccato'),

			),

			'AdminStatus' => array(

				'0' => UserModule::t('No'),

				'1' => UserModule::t('Si'),

			),

		);

		if (isset($code))

			return isset($_items[$type][$code]) ? $_items[$type][$code] : false;

		else

			return isset($_items[$type]) ? $_items[$type] : false;

	}

	

}



questo è il component 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;


	const ERROR_EMAIL_INVALID=3;

	const ERROR_STATUS_NOTACTIV=4;

	const ERROR_STATUS_BAN=5;

	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the email 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()->notsafe()->findByAttributes(array('email'=>$this->username));

		if($user===null)

			$this->errorCode=self::ERROR_EMAIL_INVALID;	

		else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password)

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else if($user->status==0&&Yii::app()->getModule('user')->loginNotActiv==false)

			$this->errorCode=self::ERROR_STATUS_NOTACTIV;

		else if($user->status==-1)

			$this->errorCode=self::ERROR_STATUS_BAN;

		else {

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

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

			$this->errorCode=self::ERROR_NONE;

			$this->setState('nomecompleto', $user->profile->cognome.' '.$user->profile->nome);

		}

		return !$this->errorCode;

	}

    

    /**

    * @return integer the ID of the user record

    */

	public function getId()

	{

		return $this->_id;

	}

}



Ciao st4nny,

grazie per la risposta e il codice.

Anche a me funziona già l’autenticazione tramite mail. Il mio problema è mettere in gioco yii-user senza utilizzare uno username.

rash*

a manina! ;)

Orcazozza! Mi distruggi! :rolleyes:

Ma scusate, non basta far si che lo username sia la mail?

certo che si :D