С ACL разбираться долго пришлось, на русском доков почти нет, на буржуйском есть и очень мало, да и не все раскрыто и с ошибками, пришлось ковырять стандартные компоненты и разбираться. Надеюсь, что то, что переведено (и дополнено) вам поможет. Будут вопросы задавайте, если смогу отвечу.
Описание ACL (Список управления доступом)
Каждый компонент имеет свои собственные ACL. Они могут быть описаны в файле access.xml, размещаемом в корне папки admin. В данном примере, мы разделим различные ACL на две секции: компонент и сообщения.
Создайте файл: admin/access.xml со следующим содержимым:
<?xml version="1.0" encoding="utf-8" ?> <access component="com_helloworld"> <section name="component"> <action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" /> <action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" /> <action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" /> <action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" /> <action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" /> </section> <section name="message"> <action name="core.delete" title="JACTION_DELETE" description="COM_HELLOWORLD_ACCESS_DELETE_DESC" /> <action name="core.edit" title="JACTION_EDIT" description="COM_HELLOWORLD_ACCESS_EDIT_DESC" /> </section> </access>
В комментах как то попросили меня делать хоть какие то комментарии к коду, ладно исправлюсь, буду писать, но не много.
В этом файле 2 секции: component и message.
Секция component содержит список действий, доступом к которым мы можем управлять, для нашего компонента в целом, а секция message содержит список действий для самих сообщений.
core.admin - Настраивать (разрешает пользователям группы изменять настройки данного расширения)
core.manage - Управление компонентом (разрешает пользователям группы администрировать данное расширение)
core.create - Создавать (разрешает пользователям группы создавать любое содержимое в данном расширении)
core.delete - Удалять (разрешает пользователям группы удалять любое содержимое в данном расширении)
core.edit - Изменять (разрешает пользователям группы изменять любое содержимое в данном расширении)
Кстате, про core.edit. Если нам запрещено изменять сообщения, но разрешено создавать, то кнопка изменить на панеле инструментов тоже имеется, и мы можем выбрать какое либо сообщение и нажать на нее, НО кнопки "Сохранить" уже не будет, а вместо этого будет кнопка "Сохранить как копию". Во как!
Ограничение доступа к компоненту
Основная идея ACL заключается в ограничении действий групп пользователей. Первым действием, которое ограничивается, является доступ к самому компоненту. Используя привычный для вас текстовый редактор, отредактируйте файл admin/helloworld.php, по приведенному образцу:
<?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // Access check. if (!JFactory::getUser()->authorise('core.manage', 'com_helloworld')) { return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); } // require helper file JLoader::register('HelloWorldHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'helloworld.php'); // import joomla controller library jimport('joomla.application.component.controller'); // Get an instance of the controller prefixed by HelloWorld $controller = JController::getInstance('HelloWorld'); // Perform the Request task $controller->execute(JRequest::getCmd('task')); // Redirect if set by the controller $controller->redirect();
Здесь строки:
// Access check. if (!JFactory::getUser()->authorise('core.manage', 'com_helloworld')) { return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); }
отвечают за доступ определенной группы пользователей к компоненту. Если какой то группе запрещен доступ, то мы не увидим нашего компонента в меню "Компоненты", если мы даже введем в строке браузера адрес: site.ru/administrator/index.php?option=com_helloworld, то выпадет соответствующее сообщение, а именно "Для просмотра этой информации необходимо пройти авторизацию".
Вывод только нужных кнопок на панель инструментов
То, какие кнопки панели инструментов, будут изображаться, зависит от установленных прав в ACL.
Откройте файл admin/views/helloworlds/view.html.php и замените в нем код на следующий:
<?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla view library jimport('joomla.application.component.view'); /** * HelloWorlds View */ class HelloWorldViewHelloWorlds extends JView { /** * HelloWorlds view display method * @return void */ function display($tpl = null) { // Get data from the model $items = $this->get('Items'); $pagination = $this->get('Pagination'); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode('<br />', $errors)); return false; } // Assign data to the view $this->items = $items; $this->pagination = $pagination; // Set the toolbar $this->addToolBar(); // Display the template parent::display($tpl); // Set the document $this->setDocument(); } /** * Setting the toolbar */ protected function addToolBar() { $canDo = HelloWorldHelper::getActions(); JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'), 'helloworld'); if ($canDo->get('core.create')) { JToolBarHelper::addNew('helloworld.add', 'JTOOLBAR_NEW'); } if ($canDo->get('core.edit')) { JToolBarHelper::editList('helloworld.edit', 'JTOOLBAR_EDIT'); } if ($canDo->get('core.delete')) { JToolBarHelper::deleteList('', 'helloworlds.delete', 'JTOOLBAR_DELETE'); } if ($canDo->get('core.admin')) { JToolBarHelper::divider(); JToolBarHelper::preferences('com_helloworld'); } } /** * Method to set up the document properties * * @return void */ protected function setDocument() { $document = JFactory::getDocument(); $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION')); } }
Откройте файл admin/views/helloworld/view.html.php и замените в нем код на следующий:
<?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla view library jimport('joomla.application.component.view'); /** * HelloWorld View */ class HelloWorldViewHelloWorld extends JView { /** * display method of Hello view * @return void */ public function display($tpl = null) { // get the Data $form = $this->get('Form'); $item = $this->get('Item'); $script = $this->get('Script'); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode('<br />', $errors)); return false; } // Assign the Data $this->form = $form; $this->item = $item; $this->script = $script; $this->canDo = HelloWorldHelper::getActions($this->item->id); // Set the toolbar $this->addToolBar(); // Display the template parent::display($tpl); // Set the document $this->setDocument(); } /** * Setting the toolbar */ protected function addToolBar() { JRequest::setVar('hidemainmenu', true); $user = JFactory::getUser(); $userId = $user->id; $isNew = $this->item->id == 0; $canDo = HelloWorldHelper::getActions($this->item->id); JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') :
JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld'); // Built the actions for new and existing records. if ($isNew) { // For new records, check the create permission. if ($canDo->get('core.create')) { JToolBarHelper::apply('helloworld.apply', 'JTOOLBAR_APPLY'); JToolBarHelper::save('helloworld.save', 'JTOOLBAR_SAVE'); JToolBarHelper::custom('helloworld.save2new', 'save-new.png',
'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false); } JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CANCEL'); } else { if ($canDo->get('core.edit')) { // We can save the new record JToolBarHelper::apply('helloworld.apply', 'JTOOLBAR_APPLY'); JToolBarHelper::save('helloworld.save', 'JTOOLBAR_SAVE'); // We can save this record, but check the create permission to see if we can
//return to make a new one. if ($canDo->get('core.create')) { JToolBarHelper::custom('helloworld.save2new', 'save-new.png',
'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false); } } if ($canDo->get('core.create')) { JToolBarHelper::custom('helloworld.save2copy', 'save-copy.png',
'save-copy_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false); } JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CLOSE'); } } /** * Method to set up the document properties * * @return void */ protected function setDocument() { $isNew = $this->item->id == 0; $document = JFactory::getDocument(); $document->setTitle($isNew ? JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING') :
JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING')); $document->addScript(JURI::root() . $this->script); $document->addScript(JURI::root() . "/administrator/components/com_helloworld/views/helloworld/submitbutton.js"); JText::script('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE'); } }
Эти два файла используют метод getActions, определенный в файле admin/helpers/helloworld.php.
Откройте файл admin/helpers/helloworld.php и замените в нем код на следующий:
<?php // No direct access to this file defined('_JEXEC') or die; /** * HelloWorld component helper. */ abstract class HelloWorldHelper { /** * Configure the Linkbar. */ public static function addSubmenu($submenu) { JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
'index.php?option=com_helloworld', $submenu == 'messages'); JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),
'index.php?option=com_categories&view=categories&extension=com_helloworld',
$submenu == 'categories'); // set some global property $document = JFactory::getDocument(); $document->addStyleDeclaration('.icon-48-helloworld {background-image: url('/../media/com_helloworld/images/hello_icon48.png');}'); if ($submenu == 'categories') { $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES')); } } /** * Get the actions */ public static function getActions($messageId = 0) { jimport('joomla.access.access'); $user = JFactory::getUser(); $result = new JObject; if (empty($messageId)) { $assetName = 'com_helloworld'; } else { $assetName = 'com_helloworld.message.'.(int) $messageId; } $actions = JAccess::getActions('com_helloworld', 'component'); foreach ($actions as $action) { $result->set($action->name, $user->authorise($action->name, $assetName)); } return $result; } }
Расписывать не буду, здесь я думаю и так все понятно (либо мы спрашиваем права для всего компонента, либо права для определенного сообщения, и в зависимости от полученного ответа выводим ту или иную кнопку.