1

I am new to yii. I want my admin upon login from webapp/user/login to redirect to the page I want which is localhost/webapp/story right now it is redirecting me to the index.php.

I have also registered a user and given that user a role which is authenticated and I want that when my user(the authenticated user) logs in via webapp/user/login then that user is redirected to index.php.

so there are two things:

1. redirecting admin to the desired page which is webapp/story.
2. redirecting the authenticated user to index.php.

I am using yii user and right extension. Please help me with this. The code LoginController is below:

<?php

class LoginController extends Controller
{
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {
                $this->lastViset();
                if (Yii::app()->user->returnUrl=='/index.php')
                        $this->redirect(Yii::app()->controller->module->returnUrl);
                else// yehen par kuch aye ga according
                    $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
        } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
     }

    private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

 }   
Salik Asad
  • 291
  • 4
  • 15

1 Answers1

1

I think could be somethings like this

<?php

class LoginController extends Controller
{
    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
    if (Yii::app()->user->isGuest) {
        $model=new UserLogin;
        // collect user input data
        if(isset($_POST['UserLogin']))
        {
            $model->attributes=$_POST['UserLogin'];
            // validate user input and redirect to previous page if valid
            if($model->validate()) {

                $this->lastViset();

                // Old code commentede
                //if (Yii::app()->user->returnUrl=='/index.php')
                //        $this->redirect(Yii::app()->controller->module->returnUrl);
                //else// yehen par kuch aye ga according
                //    $this->redirect(Yii::app()->user->returnUrl);

                // new code
                if (UserModule::isAdmin()){
                    $this->redirect(array('story/index'));
                }  
                else  {
                    $this->redirect(Yii::app()->user->returnUrl);   
                }  


            }
        }
        // display the login form
        $this->render('/user/login',array('model'=>$model));
        } else
        $this->redirect(Yii::app()->controller->module->returnUrl);
     }

    private function lastViset() {
    $lastVisit =     User::model()->notsafe()->findByPk(Yii::app()->user->id);
    $lastVisit->lastvisit = time();
    $lastVisit->save();
    }

 }   
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • i really appreciate but i had mentioned the wrong url in the question and im sorry for that. the correct url is "webapp/user/login". i think changes will be made in the LoginController.php which is in the user extension. i have updated the question.. please see the code upthere and i have also updated the question...!! – Salik Asad Nov 09 '15 at 19:05
  • the code you showed now is the one you want modify? – ScaisEdge Nov 09 '15 at 19:06
  • yes mate, a friend of mine told me that you have to make changes in there for the things you want. – Salik Asad Nov 09 '15 at 19:09
  • thanks mate,i will check... actually the work i am trying to do is to get the roles of the registered users. Like if the user is admin, then upon login the admin should see the sidebarwidget as i have created the sidebar widget and if the user(authenticated) gets logged in it just see's the index.php..!! i hope u understand. – Salik Asad Nov 09 '15 at 19:21
  • please have a look at this. http://stackoverflow.com/questions/33617055/getting-roles-of-registered-users – Salik Asad Nov 09 '15 at 20:44
  • please help me with this. http://stackoverflow.com/questions/33631514/yii-error-cannot-modify-header-information-headers-already-sent-by – Salik Asad Nov 10 '15 at 13:48