Archived

Adding ACL rules to your component

From Joomla! Documentation

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

This tutorial will demonstrate how to add custom action permission to your component down to object level. Normally we familiar with ACL in global configuration level, category level and component level. Now we will learn how to add action permission control in our component.

JTable Support[edit]

JTable class has some built-in support for ACL, to use this you have to add one integer field in your table named 'asset_id'. This field will be used to control action control for your object. Do not confuse with 'access', that is only for access permission (read side) as Joomla! separates other actions from read acess.

Normally, we have to override __construct() method in our descendant class. We may also override check() method to validate our data before saving to database. For ACL action support we have to override these methods:-

  • public function bind($array, $ignore = ) to let JTable save rule data to Joomla's assets table.
  • protected function _getAssetName() to give asset name for our asset.
  • protected function _getAssetParentId($table = null, $id = null) to give parent id of our asset, normally Joomla! uses root (1) as a parent. Generally we will change this to our component.

JTable Example[edit]

class AstermanTableUnit extends JTable
{
	public function __construct($db)
	{
		parent::__construct('#__asterman_units', 'id', $db);
	}

	public function bind($array, $ignore = '')
	{
		// Bind the rules. 
		if (isset($array['rules']) && is_array($array['rules'])) { 
			$rules = new JRules($array['rules']); 
			$this->setRules($rules); 
		}
		return parent::bind($array, $ignore);
	}
	
	/**
	 * Redefined asset name, as we support action control
	 */
        protected function _getAssetName() {
		$k = $this->_tbl_key;
		return 'com_asterman.unit.'.(int) $this->$k;
        }
    
        /**
         * We provide our global ACL as parent
     	 * @see JTable::_getAssetParentId()
         */
	protected function _getAssetParentId($table = null, $id = null)
	{
		$asset = JTable::getInstance('Asset');
		$asset->loadByName('com_asterman');
		return $asset->id;
	}
    
}

Add Rules Field to Your Form[edit]

Other Tips[edit]