0

I have this code that sends email every time a new post is created. It sends an email to all users from the database with a certain link. The thing is, users can't (shouldn't) be allowed to view the post if they are not signed in. How do I make this in such a way that after I click on the link on the email, it redirects me to the log in page and then redirects me back to the original link from the email?

What's the approach to this?

remedy.
  • 2,032
  • 3
  • 25
  • 48
  • the redirection code should be written on that page from where your are showing the post, before showing the post make sure the user is logged in. – Harshal Jan 21 '14 at 07:49

3 Answers3

2
  1. if(Yii::app()->user->isGUest) You can use this condition to check if the user is authenticated or not.
  2. $this->redirect('path/where you want to redirect');
    You can use this statement to redirect the user to the desired URL from any action of any controller or from anywhere.
  3. Yii::app()->user->returnUrl This statement stores your destiny Url. In other words, suppose You are going to project/blog/create, But in order to access this action you need to be authenticated. SO if you are not authenticated , it will redirect you to the login page. And if you are successfully logged in, you can go to your desired Url which is stored in Yii::app()->user->returnUrl

Eg:-

public function actionCreateBlog()
            {
            if(!Yii::app()->user->isGUest)
            {
            // create the blog here
            }
            else
            {
            Yii::app()->user->loginRequired();
            }

Then in your login action you can do this

public function actionLogin()
    {
        $model=new LoginForm;

        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login',array('model'=>$model));
    }

}
Let me see
  • 5,063
  • 9
  • 34
  • 47
  • So let's say that I have a link in my email and I click on it and it points towards lets say announcement/view/id/186836 but since they aren't logged in, they should be redirected towards the index to log in. So if I'm getting this right, I'll place the if(Yii:app()->user->isGuest() with the actionView() inside the controller than link it towards the index and where the login form is, I'll place the returnUrl. Correct? – remedy. Jan 21 '14 at 13:24
  • Also, I have to set the returnUrl to something like this : returnUrl('announcement/view/id/2ef2') correct? If the link is coming form the email, how would I set it so that it works for everyones link since different users will have different ids? – remedy. Jan 21 '14 at 13:47
  • no you do not need to set the return Url. In my example check the action login. In that particular action you will find how returnUrl is used. returnUrl automatically has the Url of your destiny – Let me see Jan 22 '14 at 04:44
1

This answer addresses the general approach, basically to detect that you're not logged in but saving the current location in a variable that is passed into the login page so that it can redirect you back to the link:

Redirecting to previous page after login? PHP

To implement it using yii, this answer adds more context

Redirect page after login in Yii framework

Community
  • 1
  • 1
Justin K
  • 369
  • 1
  • 10
1

You can do this in simple way.

In your controller, you have accessRules function to set the user role for all action functions of that controller. Here you can restrict the user access. So After login, yii will do redirection.

For ex, you have function called

 public function actionView($id)

For this view function, you need to set logged in user privilage, you have to set rules as below.

In accessRules set,

'actions' => array('view'), 'users' => array('@'), // @ for logged in user
Kumar V
  • 8,810
  • 9
  • 39
  • 58