示例#1
0
 /**
  * Return whether the role has the permission within the context of a Page or Template
  * 
  * @param bool $has Result from the hasPermission() method
  * @param Permission $permission Permission to check
  * @param Wire $context Must be a Template or Page
  * @return bool
  * 
  */
 protected function hasPermissionContext($has, Permission $permission, Wire $context)
 {
     if (strpos($permission->name, "page-") !== 0) {
         return $has;
     }
     $type = str_replace('page-', '', $permission->name);
     if (!in_array($type, array('view', 'edit', 'add', 'create'))) {
         $type = 'edit';
     }
     $accessTemplate = $context instanceof Page ? $context->getAccessTemplate($type) : $context;
     if (!$accessTemplate) {
         return false;
     }
     if (!$accessTemplate->useRoles) {
         return $has;
     }
     if ($permission->name == 'page-view') {
         if (!$has) {
             return false;
         }
         $has = $accessTemplate->hasRole($this);
         return $has;
     }
     if ($permission->name == 'page-edit' && !$has) {
         return false;
     }
     switch ($permission->name) {
         case 'page-edit':
             $has = in_array($this->id, $accessTemplate->editRoles);
             break;
         case 'page-create':
             $has = in_array($this->id, $accessTemplate->createRoles);
             break;
         case 'page-add':
             $has = in_array($this->id, $accessTemplate->addRoles);
             break;
         default:
             // some other page-* permission
             $rolesPermissions = $accessTemplate->rolesPermissions;
             if (!isset($rolesPermissions["{$this->id}"])) {
                 return $has;
             }
             foreach ($rolesPermissions["{$this->id}"] as $permissionID) {
                 $revoke = strpos($permissionID, '-') === 0;
                 if ($revoke) {
                     $permissionID = ltrim($permissionID, '-');
                 }
                 $permissionID = (int) $permissionID;
                 if ($permission->id != $permissionID) {
                     continue;
                 }
                 if ($has) {
                     if ($revoke) {
                         $has = false;
                     }
                 } else {
                     if (!$revoke) {
                         $has = true;
                     }
                 }
                 break;
             }
     }
     return $has;
 }