Logowanie

Hej.

Pisałem już na ten temat, ale nie do końca rozumiem jak zrobić dobre logowanie. Mam:


 public function actionIndex()

    {

		

	var_dump(Yii::$app->user->getIsGuest());

    }


    public function actionLogin()

    {

        $model = new User();

        $model->login();

		

	echo "logged in!";


    }

Dlaczego jak wywołam akcję index, dostanę po var_dumpie true (gość), mimo, że wcześniej wywołałem login (logowanie przebiegło poprawnie z użyciem obiektu tożsamości - \yii\web\IdentityInterface metodą login()).

Inne rozwiązanie to ustawić zmienną sesyjną, ale to chyba w ostateczności.

Dzięki za pomoc - pozdrawiam.

Proponuję zerknąć i zobaczyć jak to zostało zrobione w LoginForm

Powinno działać tak:




Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);


protected function getUser()

{

	if ($this->_user === null) {

		$this->_user = User::findByUsername($this->username); //tutaj Twoja metoda wyszukująca użytkownika

	}

	return $this->_user;

}




<?php


namespace app\models;


use Yii;

use yii\db\ActiveRecord;

use yii\db\Query;




/**

 * This is the model class for table "uzytkownik".

 *

 * @property integer $id

 * @property string $imie

 * @property string $nazwisko

 * @property string $adres

 * @property string $telefon

 * @property string $email

 * @property string $rola

 * @property integer $punkty

 * @property string $ban

 * @property string $login

 * @property string $haslo

 * @property string $zarejestrowany

 * @property string $token

 */

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface

{

    /**

     * @inheritdoc

     */

	 

	public $username = "";

 

	 

    public static function tableName()

    {

        return 'uzytkownik';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            


			[['login'], 'required', 'message'=>'Prosimy podać login.'],

            [['haslo'], 'required', 'message'=>'Prosimy podać hasło.'],

			//[['email'], 'required', 'message'=>'Prosimy podać email.'], utworzyć dwa modele dla register2 i loginu tez przez gii 

			

			//[['email'], 'email', 'message'=>'Niepoprawny format adresu email.'],

            [['haslo'], 'validatePassword']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'imie' => 'Imie',

            'nazwisko' => 'Nazwisko',

            'adres' => 'Adres',

            'telefon' => 'Telefon',

            'email' => 'EMAIL',

            'rola' => 'Rola',

            'punkty' => 'Punkty',

            'ban' => 'Ban',

            'login' => 'LOGIN',

            'haslo' => 'HASLO',

            'zarejestrowany' => 'Zarejestrowany',

            'token' => 'Token',

        ];

    }

	

	public function login()

    {

        if($this->validate())

        {

			

			$rola = User::find()->select('rola')->from('uzytkownik')->where(['login'=>$this->login])->scalar();

			

			

			$session = Yii::$app->session;

			

			if($rola !== false) {

			

				$session->set('rola_po_logowaniu', $rola);

				$session->set('login', $this->login);

			}

		

            $this->username = $this->login;

			return Yii::$app->user->login($this, 0);    

        }

        else

        {

            return false;    

        } 

    }


    public static function findIdentity($id) 

    {

        return; //static::find()->findOne($id);

    }

	

    public function validatePassword($password, $params)

    {


        $login = $this->login;


        $query = new Query();

        $haslo = $query->select("haslo")->from("uzytkownik")->where(['login'=>$login])->scalar();


        if ($this->$password !== $haslo) {

            $this->addError($password, 'Niepoprawne hasło lub login.');

        }

        return true;


    }

 

    public function getId()

    {

        return $this->id;

    }

 

    public static function findIdentityByAccessToken($token, $type = null)

    {

    }


    public static function findByUsername($username)

    {

    }


    public function getAuthKey()

    {

    }


    public function validateAuthKey($authKey)

    {

    }

}



Może to komuś pomoże… nie za bardzo wiem co robie nie tak