Question

I want to add my Yii Project IP filtering capabilities.. Below code add this capabilities to me actually (it helps).. But I want to add this capabilities in the run time, after the my program (yii project) is running, user may add another IP addresses into allow ip list (whitelist), and deny some others to block list (like blacklist).. Could you help me about how chould I add these functionalities into my project..

Thanks from now,

#in the SiteController
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
           ),
            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'),
    ),
Was it helpful?

Solution

The other answer will only work during a given session and then the blacklist will revert.

I would suggest you use a Security component (that you write) that stores the appropriate information in a database (or some other system that allows you to change it). Then, either have the security component modify the params array or better yet modify your system to retrieve the info directly from the component.

OTHER TIPS

For adding new IP

array_push(Yii::app()->params['allowIps'],'4.4.4.4');

For Removing IP

if (($key = array_search('4.4.4.4', Yii::app()->params['allowIps'])) !== false) {
    unset(Yii::app()->params['allowIps'][$key]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top