Automatic Login

Hi, I need that the user was logged automatically.

I don’t want to grant access to any user, no logged.

That’s because i will allow user to login in the near future.

But right now I need that the app thinks that the user is authenticated as admin.

How can I achieve that?

Best regards

Nicolas

Add application behavior to your config




'behaviors' => array(

  'application.behaviors.AlwaysLoginBehavior'

),



Check there if user is guest, then login. Login example you may take from default yii web page.

read this topic http://www.yiiframework.com/doc/guide/1.1/en/topics.auth

In your controller you should put access rules:


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',

				'users'=>array('@'),

			),

			array('deny',

				'users'=>array('*'),

			),

		);

	}

Hi, this behavior allready exists? or I should write it from scratch ?

Regards.

No. Do it your self, like this




<?php

class ApplicationBehavior extends CBehavior {


	public function events(){

		return array(

			'onBeginRequest' => 'beginRequest',

		);

	}


	public function beginRequest(){

		if ( Yii::app()->user->isGuest ) {

			// auto login

		}

	}


}



hi, I present you AlwayLoginBehavior.


class AlwaysLoginBehavior extends CBehavior{

    //put your code here

    public function events(){

                return array(

                        'onBeginRequest' => 'beginRequest',

                );

        }


        public function beginRequest(){

                if ( Yii::app()->user->isGuest ) {

                        // auto login

                    Yii::Log('Estoy en beginRequest con user->id:'. Yii::app()->user->id);

                    

                    $model=new LoginForm;

                    $model->username='admin';

                    $model->password='admin';

    		// validate user input and redirect to the previous page if valid

       		if($model->validate() && $model->login())

                    CController::redirect(Yii::app()->user->returnUrl);

		//Yii::app()->redirect(Yii::app()->user->returnUrl);

                

                }

        }

}



My first try, if something is wrong, please correct me!

It seems all is ok. Now look in wiki how to setup RBAC to grant and check administrator access.

Useless string after if ( Yii::app()->user->isGuest ). You still have no user id.