Tuesday 11 March 2014

How to Check Url Access In YII for Role based Access

Below function return true and false(1 or null) either yii user is able to access the url(authItemName) or not.

Explample.
Player list
$url = http://localhost/YiiProject/index.php?r=player/playerList/index
$allowedAccess = $this->checkUrlAccess($url );
if($allowedAccess){
 echo "You are allowed to access Url";
}else{
 echo 'Access denied';
}
function checkUrlAccess($authItemName)
  {
     #New Code ADDED            
            $allowedAccess='';
        if (strpos($authItemName, '?r=') != false)
        {
                   
            $urlQueryString=explode('?r=',$authItemName);          
            if (strpos($urlQueryString[1], '/') != false)
            {
                $parts = explode('/', $urlQueryString[1]);
                if ($this->upperCaseFirstLetter)
                {
                  foreach ($parts as $i => $part)
                  {
                    $parts[$i] = ucfirst($part);
                  }
                }
                $moduleName=$controllerName=$actionName='';
                $numOfParts = count($parts);
                if ($numOfParts > 2)
                {
                  $moduleName=$parts[$numOfParts - 3];
                }
                                $controllerName=$parts[$numOfParts - 2];
                $actionName=$parts[$numOfParts - 1];
                                $itemName = '';                   
                                 if(!empty($moduleName)){
                                         $itemName .= $moduleName . '.';
                                 }
                                 $itemName .= $controllerName;
                 $user = Yii::app()->getUser();
                 if ($user->isGuest){
                           if(empty($moduleName)){
                               $allowedAccess =   Yii::app()->beforeControllerAction($controllerName,$actionName);
                                             }
                 }else{
                      if ($user->checkAccess($itemName . '.*')){
                         $allowedAccess = 1;
                      }else{
                          $itemName .= '.' . $actionName;
                          if ($user->checkAccess($itemName, array())){
                              $allowedAccess = 1;
                          }
                      }
                                            if(empty($moduleName) && empty($allowedAccess)){
                                                $allowedAccess =   Yii::app()->beforeControllerAction($controllerName,$actionName);
                                             }
                 }
          
             }
        }
        #END
                return $allowedAccess;
   
  }

No comments:

Post a Comment

List OF BANK PAN Numbers

List OF BANK PAN Numbers Bank/Home Loan Providers PAN Number Allahabad Bank AACCA8464F Andhra Bank AABCA7375C Axis Bank...