Exemple #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  object   Rules object for the asset.
  */
 public static function getAssetRules($asset, $recursive = false)
 {
     // Get the database connection object.
     /*$db = App::get('db');
     
     		// 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();*/
     // Build the database query to get the rules for the asset.
     $query = Asset::all()->select($recursive ? 'b.rules' : 'a.rules');
     $query->from($query->getTableName(), 'a');
     // If the asset identifier is numeric assume it is a primary key, else lookup by name.
     if (is_numeric($asset)) {
         $query->whereEquals('a.id', (int) $asset);
     } else {
         $query->whereEquals('a.name', $asset);
     }
     // If we want the rules cascading up to the global asset node we need a self-join.
     if ($recursive) {
         $query->joinRaw($query->getTableName() . ' AS b', 'b.lft <= a.lft AND b.rgt >= a.rgt', 'left');
         $query->order('b.lft', 'asc');
     }
     $query->group($recursive ? 'b.id, b.rules, b.lft' : 'a.id, a.rules, a.lft');
     $result = $query->rows();
     // Get the root even if the asset is not found and in recursive mode
     if (empty($result)) {
         $result = Asset::oneOrFail(Asset::getRootId());
         $result = array($result);
     }
     // Instantiate and return the JAccessRules object for the asset rules.
     $rules = new Rules();
     $rules->mergeCollection($result);
     return $rules;
 }