Ipfilter in every page using access rules.

Ips filter are really useful if you are developing a website for a small quantity of people, or you just dont want the people to access the application outside an intranet.

Actually Yii has a IPFilter that is uded by GII. I wanted to know if anyone knows how to implement it in any webpage, controller, or module.

Could be really useful!

Thanks in advance! :D

  • You could do something similar to the ‘default-deny’ in this wiki article.

Use the ‘ips’ property of the CAccessrule instead.

  • Or you add a beforeAction method to your base controller (compontents/Controller.php) and check the allowed ip there.

  • Or you implement the solution like Gii. You only have to take a look at the source of GiiModule.php

Take a look at the following:

Access Control Filter

“ips” is one of the parameters for access rules. You’d have to put both a filters() and accessRules() method in each controller.

I use it and it works great.

I did something like this :




    public function accessRules() {

        return array(

            array('allow',

                'actions' => array('index','view', 'create', 'update', 'manage'),

                'ips' => array('127.0.0.1'),

            ),

            array('deny',

                'actions' => array('index','view', 'create', 'update', 'manage'),

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

            ),

        );

    }



So, Actually I need to use the acess rule in every controller.

Any idea of how to do this automatically without extending a new controller ?

At the end was a easier solution to implement the ipFilter in a common controller.

This is the code in the common controller.


   public function filterAccessControl($filterChain) {

        $rules = $this->accessRules();

        $filter = new CAccessControlFilter;

        // All the allow rules

        $allowRules[] = array('allow',

            'actions' => array('index', 'view', 'create', 'update', 'manage', 'list', 'show', 'list', 'admin', 'delete', 'logout', 'login'),

            'ips' => array('127.0.0.1','200.42.167.146'));


        $filter->setRules($allowRules);




        //All the deny rules

        $denyRules[] = array('deny',

            'ips' => array('*'));





        $filter->setRules($denyRules);

        $filter->filter($filterChain);

    }

I hope that this will be useful for some of you. If you have other ideas or improvements, could be ool to share.

Ok, this will be the last time.

I created a more professional solution:

  • I created a Common Controller

  • I created a common Control filter

  • I just use my filter instead of the deffault

My common conctroller has the following.




    public function filterAccessControl($filterChain) {

        $rules = $this->accessRules();

        $filter = new myControlFilter();

        $filter->setRules($rules);

        $filter->filter($filterChain);

    }



myControlFilter :




class myControlFilter extends CAccessControlFilter {


    private $_IPS_ALLOW = array('127.0.0.1');

    

    protected function preFilter($filterChain) {

        $app = Yii::app();

        $request = $app->getRequest();

        $ip = $request->getUserHostAddress();

        if (in_array($ip, $this->_IPS_ALLOW)) {

            return parent::preFilter($filterChain);

        } else {

            return false;

        }

    }


}



And that is it ! is elegant, and I will not have any trouble with the filters because of the IP. Now I just need to add a message or something to say that is not authorized :D.

Thanks for all the tips, I could not do it without them.

Just a short note. You can always use a Yii::app()->param for a global ip list. E.g.




    public function accessRules() {

        return array(

            array('allow',

                'actions' => array('index','view', 'create', 'update', 'manage'),

                'ips' => Yii::app()->params['allowIps'],//updated to pull list from Yii app params

            ),

            array('deny',

                'actions' => array('index','view', 'create', 'update', 'manage'),

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

            ),

        );

    }






#in /protected/config/main.php 


	'params'=>array(

		// this is used in contact page

		'allowedIps'=>array('22.150.133.177'),

	),