示例#1
0
 /**
  * Method to return the JAccessRules object for an asset.  The returned object can optionally hold
  * only the rules explicitly set for the asset or the summation of all inherited rules from
  * parent assets and explicit rules.
  *
  * @param mixed   $asset     Integer asset id or the name of the asset as a string.
  * @param boolean $recursive True to return the rules object with inherited rules.
  *
  * @return JAccessRules JAccessRules object for the asset.
  *
  * @since   11.1
  */
 public static function getAssetRules($asset, $recursive = false)
 {
     // Get the database connection object.
     $db = JFactory::getDbo();
     // Build the database query to get the rules for the asset.
     $query = $db->getQuery(true);
     $query->select($recursive ? 'b.rules' : 'a.rules');
     $query->from('#__assets AS a');
     // SQLsrv change
     $query->group($recursive ? 'b.id, b.rules, b.lft' : 'a.id, a.rules, a.lft');
     // If the asset identifier is numeric assume it is a primary key, else lookup by name.
     if (is_numeric($asset)) {
         $query->where('(a.id = ' . (int) $asset . ')');
     } else {
         $query->where('(a.name = ' . $db->quote($asset) . ')');
     }
     // If we want the rules cascading up to the global asset node we need a self-join.
     if ($recursive) {
         $query->leftJoin('#__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt');
         $query->order('b.lft');
     }
     // Execute the query and load the rules from the result.
     $db->setQuery($query);
     $result = $db->loadColumn();
     // Get the root even if the asset is not found and in recursive mode
     if (empty($result)) {
         $db = JFactory::getDbo();
         $assets = JTable::getInstance('Asset', 'JTable', array('dbo' => $db));
         $rootId = $assets->getRootId();
         $query = $db->getQuery(true);
         $query->select('rules');
         $query->from('#__assets');
         $query->where('id = ' . $db->quote($rootId));
         $db->setQuery($query);
         $result = $db->loadResult();
         $result = array($result);
     }
     // Instantiate and return the JAccessRules object for the asset rules.
     $rules = new JAccessRules();
     $rules->mergeCollection($result);
     return $rules;
 }
示例#2
0
 private static function getAssetRules($asset)
 {
     $db = JFactory::getDBO();
     if (is_numeric($asset)) {
         $query = "SELECT b.rules\n                      FROM #__assets AS a LEFT JOIN #__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt\n                      WHERE (a.id = '{$asset}' OR a.parent_id=0) GROUP BY b.id, b.rules, b.lft ORDER BY b.lft";
     } else {
         $query = "SELECT b.rules\n                      FROM #__assets AS a LEFT JOIN #__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt\n                      WHERE (a.name = '{$asset}' OR a.parent_id=0) GROUP BY b.id, b.rules, b.lft ORDER BY b.lft";
     }
     $db->setQuery($query);
     $result = $db->loadResultArray();
     if (empty($result)) {
         $query = "SELECT rules\n                      FROM #__assets\n                      WHERE parent_id=0";
         $db->setQuery($query);
         $result = $db->loadResultArray();
     }
     $rules = new JAccessRules();
     $rules->mergeCollection($result);
     return $rules;
 }
 /**
  * Gets the default asset values for a component.
  *
  * @param   $string  $component  The component asset name to search for
  *
  * @return  JAccessRules  The JAccessRules object for the asset
  */
 protected function getDefaultAssetValues($component, $try = true)
 {
     // Need to find the asset id by the name of the component.
     $db = JFactory::getDbo();
     $query = $db->getQuery(true)->select($db->quoteName('id'))->from($db->quoteName('#__assets'))->where($db->quoteName('name') . ' = ' . $db->quote($component));
     $db->setQuery($query);
     $db->execute();
     if ($db->loadRowList()) {
         // asset alread set so use saved rules
         $assetId = (int) $db->loadResult();
         return JAccess::getAssetRules($assetId);
     } elseif ($try) {
         $try = explode('.', $component);
         $result = $this->getDefaultAssetValues($try[0], false);
         if ($result instanceof JAccessRules) {
             if (isset($try[1])) {
                 $_result = (string) $result;
                 $_result = json_decode($_result);
                 foreach ($_result as $name => &$rule) {
                     $v = explode('.', $name);
                     if ($try[1] !== $v[0]) {
                         // remove since it is not part of this view
                         unset($_result->{$name});
                     } else {
                         // clear the value since we inherit
                         $rule = array();
                     }
                 }
                 // check if there are any view values remaining
                 if (count($_result)) {
                     $_result = json_encode($_result);
                     $_result = array($_result);
                     // Instantiate and return the JAccessRules object for the asset rules.
                     $rules = new JAccessRules();
                     $rules->mergeCollection($_result);
                     return $rules;
                 }
             }
             return $result;
         }
     }
     return JAccess::getAssetRules(0);
 }
示例#4
0
文件: access.php 项目: adjaika/J3Base
 /**
  * Method to return the JAccessRules object for an asset.  The returned object can optionally hold
  * only the rules explicitly set for the asset or the summation of all inherited rules from
  * parent assets and explicit rules.
  *
  * @param   mixed    $asset                 Integer asset id or the name of the asset as a string.
  * @param   boolean  $recursive             True to return the rules object with inherited rules.
  * @param   boolean  $recursiveParentAsset  True to calculate the rule also based on inherited component/extension rules.
  *
  * @return  JAccessRules   JAccessRules object for the asset.
  *
  * @since   11.1
  */
 public static function getAssetRules($asset, $recursive = false, $recursiveParentAsset = true)
 {
     // Get instance of the Profiler:
     $_PROFILER = JProfiler::getInstance('Application');
     $extensionName = self::getExtensionNameFromAsset($asset);
     // Almost all calls should have recursive set to true
     // so we'll get to take advantage of preloading:
     if ($recursive && $recursiveParentAsset && isset(self::$assetPermissionsByName[$extensionName]) && isset(self::$assetPermissionsByName[$extensionName][$asset])) {
         // Mark in the profiler.
         JDEBUG ? $_PROFILER->mark('Start JAccess::getAssetRules New (' . $asset . ')') : null;
         $assetType = self::getAssetType($asset);
         $assetId = self::$assetPermissionsByName[$extensionName][$asset]->id;
         $ancestors = array_reverse(self::getAssetAncestors($assetType, $assetId));
         // Collects permissions for each $asset
         $collected = array();
         foreach ($ancestors as $id) {
             $collected[] = self::$assetPermissionsById[$extensionName][$id]->rules;
         }
         /**
          * Hashing the collected rules allows us to store
          * only one instance of the JAccessRules object for
          * Assets that have the same exact permissions...
          * it's a great way to save some memory.
          */
         $hash = md5(implode(',', $collected));
         if (!isset(self::$assetRulesIdentities[$hash])) {
             $rules = new JAccessRules();
             $rules->mergeCollection($collected);
             self::$assetRulesIdentities[$hash] = $rules;
         }
         // Mark in the profiler.
         JDEBUG ? $_PROFILER->mark('Finish JAccess::getAssetRules New (' . $asset . ')') : null;
         return self::$assetRulesIdentities[$hash];
     } else {
         // Mark in the profiler.
         JDEBUG ? $_PROFILER->mark('Start JAccess::getAssetRules Old (' . $asset . ')') : null;
         if ($asset === "1") {
             // There's no need to process it with the
             // recursive method for the Root Asset ID.
             $recursive = false;
         }
         // Get the database connection object.
         $db = JFactory::getDbo();
         // Build the database query to get the rules for the asset.
         $query = $db->getQuery(true)->select($recursive ? 'b.rules' : 'a.rules')->from('#__assets AS a');
         $extensionString = '';
         if ($recursiveParentAsset && ($extensionName !== $asset || is_numeric($asset))) {
             $extensionString = ' OR a.name = ' . $db->quote($extensionName);
         }
         $recursiveString = '';
         if ($recursive) {
             $recursiveString = ' OR a.parent_id=0';
         }
         // If the asset identifier is numeric assume it is a primary key, else lookup by name.
         if (is_numeric($asset)) {
             $query->where('(a.id = ' . (int) $asset . $extensionString . $recursiveString . ')');
         } else {
             $query->where('(a.name = ' . $db->quote($asset) . $extensionString . $recursiveString . ')');
         }
         // If we want the rules cascading up to the global asset node we need a self-join.
         if ($recursive) {
             $query->join('LEFT', '#__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt')->order('b.lft');
         }
         // Execute the query and load the rules from the result.
         $db->setQuery($query);
         $result = $db->loadColumn();
         // Get the root even if the asset is not found and in recursive mode
         if (empty($result)) {
             $db = JFactory::getDbo();
             $assets = JTable::getInstance('Asset', 'JTable', array('dbo' => $db));
             $rootId = $assets->getRootId();
             $query->clear()->select('rules')->from('#__assets')->where('id = ' . $db->quote($rootId));
             $db->setQuery($query);
             $result = $db->loadResult();
             $result = array($result);
         }
         // Instantiate and return the JAccessRules object for the asset rules.
         $rules = new JAccessRules();
         $rules->mergeCollection($result);
         JDEBUG ? $_PROFILER->mark('Finish JAccess::getAssetRules Old (' . $asset . ')') : null;
         return $rules;
     }
 }