Yii Framework Forum: Required Login - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Required Login pages allowed Rate Topic: -----

#1 User is offline   WSBC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-March 13

Posted 18 March 2013 - 12:10 PM

Below are code snippets I am using. It works great but I want to allow access to one more page that is located under /site/pages/news.php. I attempted to add it to the array several different ways but I am stillb being forced to login on this page.

***config main.php
'behaviors' => array(
'onBeginRequest' => array(
'class' => 'application.components.RequireLogin'
)
),

***requirelogin.php
class RequireLogin extends CBehavior
{
public function attach($owner)
{
$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
}

public function handleBeginRequest($event)
{

$app = Yii::app();
$user = $app->user;

$request = trim($app->urlManager->parseUrl($app->request), '/');
$login = trim($user->loginUrl[0], '/');

// Restrict guests to public pages.
$allowed = array($login,'site/index','site/contact');
if ($user->isGuest && !in_array($request, $allowed))
$user->loginRequired();

// Prevent logged in users from viewing the login page.
$request = substr($request, 0, strlen($login));
if (!$user->isGuest && $request == $login)
{
$url = $app->createUrl($app->homeUrl[0]);
$app->request->redirect($url);
}
}
}
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 18 March 2013 - 01:37 PM

Dear Friend

To simulate your scenario, this is the working code in my localhost.

components/ApplicationBehavior.php
class ApplicationBehavior extends CBehavior
{
    public function events()
    {
        return array(
            'onBeginRequest'=>'restrict'
        );
    }
    
    public function restrict()
    {   $owner=$this->owner;
        $routes=array(
            array("site/index",array()),
            array('site/contact',array()),
            array("site/page",array('view'=>'news')), //included "index.php?r=site/page&view=news"
                    );
        $allowed=array();
        foreach($routes as $route)
            $allowed[]=$owner->createUrl($route[0],$route[1]);

        if($owner->user->isGuest && !in_array($owner->request->url,$allowed))
            $owner->catchAllRequest=array('site/login');

        if(!$owner->user->isGuest && $owner->request->url==$owner->createUrl('site/login'))
            throw new CHttpException(" :You have already logged in");//Throwing error is not a good option though.
    }
}


Attach this behavior in main.php
'behaviors'=>array(
    'appBehav'=>'ApplicationBehavior',    
    ),


Regards.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users