本文整理汇总了PHP中CAccessControlFilter::getRules方法的典型用法代码示例。如果您正苦于以下问题:PHP CAccessControlFilter::getRules方法的具体用法?PHP CAccessControlFilter::getRules怎么用?PHP CAccessControlFilter::getRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CAccessControlFilter
的用法示例。
在下文中一共展示了CAccessControlFilter::getRules方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: filterItems
/**
* Filter recursively the menu items received setting
visibility true or
* false according to controller/action preFilter
*
* @param array $items The menu items being filtered.
* @return array The menu items with visibility
defined by preFilter().
*/
protected function filterItems(array $items)
{
$app = Yii::app();
foreach ($items as $pos => $item) {
if (!isset($item['visible'])) {
// get the url parameter
if (isset($item['url']) && is_array($item['url'])) {
$url = $item['url'][0];
}
// parse the url into controller and action
$parts = explode("/", $url);
if (count($parts) == 1) {
$controller = $app->controller;
$actionId = $parts[0];
} else {
$controllerId = ucfirst($parts[1]);
$actionId = count($parts) > 2 ? $parts[2] : 'index';
$controllerList = $app->createController($controllerId);
$controller = $controllerList[0];
}
// generate a controller instance to access and
//compare the rules
$action = $controller->createAction($actionId);
$filter = new CAccessControlFilter();
$filter->setRules($controller->accessRules());
$user = $app->getUser();
$request = $app->getRequest();
$ip = $request->getUserHostAddress();
$item['visible'] = false;
foreach ($filter->getRules() as $rule) {
// we are making an assumption for now that all
// menu items are GET actions
if ($rule->isUserAllowed($user, $controller, $action, $ip, 'GET') > 0) {
$item['visible'] = true;
break;
}
}
}
/**
* If current item is visible and has sub items,
loops recursively
* on them.
*/
if (isset($item['items']) && $item['visible']) {
$item['items'] = $this->filterItems($item['items']);
}
$items[$pos] = $item;
}
return $items;
}