2022年Joomla.MVC组件开发教程 2.pdf
本节标题:添加访问控制列表(ACL) ACL (Access Control List)的描述Each component has its own ACL. They can be described in an access.xml file located at the root of the admin folder. This file describes the ACL for the com_helloworld component in a different section. In this example, we have chosen to separate the different ACL into two sections: components and messages. admin/access.xml 限制访问组件ACL的主要目的是限制对用户组的直接操作行为,第一个行为就是对组件本身的访问。修改入口文件:admin/helloworld.php名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 19 页 - - - - - - - - - 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(ponent.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(); 根据权限显示工具栏按钮为了使工具栏的按钮按照ACL显示,需要修改视图:admin/views/helloworlds/view.html.phpget(Items); $pagination = $this-get(Pagination); / Check for errors. if (count($errors = $this-get(Errors) JError:raiseError(500, implode(, $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); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 19 页 - - - - - - - - - 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.phpget(Form); $item = $this-get(Item); $script = $this-get(Script); / Check for errors. if (count($errors = $this-get(Errors) JError:raiseError(500, implode(, $errors); return false; / Assign the Data $this-form = $form; $this-item = $item; $this-script = $script; / Set the toolbar $this-addToolBar(); / Display the template parent:display($tpl); / Set the document $this-setDocument(); /* * Setting the toolbar */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 19 页 - - - - - - - - - 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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 19 页 - - - - - - - - - / 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); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 19 页 - - - - - - - - - 以上两个文件使用了admin/helpers/helloworld.php中定义的getActions方法。修改该文件如下admin/helpers/helloworld.phpaddStyleDeclaration(.icon-48-helloworld . background-image: url(./media/com_helloworld/images/tux-48x48.png);); if ($submenu = categories) $document-setTitle(JText:_(COM_HELLOWORLD_ADMINISTRATION_CATEGORIES); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 19 页 - - - - - - - - - /* * 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; 在组件的首选项中添加许可设置因为我们是在组件中应用ACL控制,所以需要在组件层级上设置,这当然应该在首选项中修改。admin/config.xml JHIDE JSHOW 在权限表( assets table)中设置值为了给每条信息设置权限(asset ),我们需要做以下两件事:首先, core.edit之类的 ACL 权限是从信息本身而不是从组件中提取出来的,。admin/models/helloworld.php名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 19 页 - - - - - - - - - authorise(core.edit, com_helloworld.message. (int) isset($data$key) ? $data$key : 0) or parent:allowEdit($data, $key); /* * Returns a reference to the a Table object, always creating it. * * param type The table type to instantiate * param string A prefix for the table class name. Optional. * param array Configuration array for model. Optional. * return JTable A database object * since 2.5 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 19 页 - - - - - - - - - */ public function getTable($type = HelloWorld, $prefix = HelloWorldTable, $config = array() return JTable:getInstance($type, $prefix, $config); /* * Method to get the record form. * * param array $data Data for the form. * param boolean $loadData True if the form is to load its own data (default case), false if not. * return mixed A JForm object on success, false on failure * since 2.5 */ public function getForm($data = array(), $loadData = true) / Get the form. $form = $this-loadForm(com_helloworld.helloworld, helloworld, array(control = jform, load_data = $loadData); if (empty($form) return false; return $form; /* * Method to get the script that have to be included on the form * * return string Script files */ public function getScript() return administrator/components/com_helloworld/models/forms/helloworld.js; /* * Method to get the data that should be injected in the form. * * return mixed The data for the form. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 19 页 - - - - - - - - - * since 2.5 */ protected function loadFormData() / Check the session for previously entered form data. $data = JFactory:getApplication()-getUserState(com_helloworld.edit.helloworld.data, array(); if (empty($data) $data = $this-getItem(); return $data; 其次,在 helloworld表中设置权限的 name 、title和 parent (父权限)admin/tables/helloworld.phploadArray($arrayparams); $arrayparams = (string)$parameter; return parent:bind($array, $ignore); /* * Overloaded load function * * param int $pk primary key * param boolean $reset reset data * return boolean * see JTable:load */ public function load($pk = null, $reset = true) if (parent:load($pk, $reset) / Convert the params field to a registry. $params = new JRegistry; $params-loadJSON($this-params); $this-params = $params; return true; else return false; /* 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 19 页 - - - - - - - - - * Method to compute the default name of the asset. * The default name is in the form table_name.id * where id is the value of the primary key of the table. * * return string * since 2.5 */ protected function _getAssetName() $k = $this-_tbl_key; return com_helloworld.message.(int) $this-$k; /* * Method to return the title to use for the asset table. * * return string * since 2.5 */ protected function _getAssetTitle() return $this-greeting; /* * Get the parent asset id for the record * * return int * since 2.5 */ protected function _getAssetParentId() $asset = JTable:getInstance(Asset); $asset-loadByName(com_helloworld); return $asset-id; 扩展阅读关于 actions, assets and ACL 更多的信息可以从以下教程中获取:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 19 页 - - - - - - - - - ACL Tutorial for Joomla 1.6(超级链接不改,回头有兴趣的朋友可以访问)How to implement actions in your code打包升级文件如下:helloworld.xmlsite/index.htmlsite/helloworld.phpsite/controller.phpsite/views/index.htmlsite/views/helloworld/index.htmlsite/views/helloworld/view.html.phpsite/views/helloworld/tmpl/index.htmlsite/views/helloworld/tmpl/default.xmlsite/views/helloworld/tmpl/default.phpsite/models/index.htmlsite/models/helloworld.phpsite/language/index.htmlsite/language/en-GB/index.htmlsite/language/en-GB/en-GB.com_helloworld.iniadmin/index.htmladmin/access.xmladmin/config.xmladmin/helloworld.phpadmin/controller.phpadmin/sql/index.htmladmin/sql/install.mysql.utf8.sqladmin/sql/uninstall.mysql.utf8.sqladmin/sql/updates/index.htmladmin/sql/updates/mysql/index.htmladmin/sql/updates/mysql/0.0.1.sqladmin/sql/updates/mysql/0.0.6.sqladmin/sql/updates/mysql/0.0.12.sqladmin/sql/updates/mysql/0.0.13.sqladmin/models/index.htmladmin/models/fields/index.htmladmin/models/fields/helloworld.phpadmin/models/forms/index.htmladmin/models/forms/helloworld.xmladmin/models/forms/helloworld.jsadmin/models/rules/index.html名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 19 页 - - - - - - - - - admin/models/rules/greeting.phpadmin/models/helloworld.phpadmin/models/helloworlds.phpadmin/views/index.htmladmin/views/helloworlds/index.htmladmin/views/helloworlds/view.html.phpadmin/views/helloworlds/tmpl/index.htmladmin/views/helloworlds/tmpl/default.phpadmin/views/helloworlds/tmpl/default_head.phpadmin/views/helloworlds/tmpl/default_body.phpadmin/views/helloworlds/tmpl/default_foot.phpadmin/views/helloworld/index.htmladmin/views/helloworld/view.html.phpadmin/views/helloworld/submitbutton.jsadmin/views/helloworld/tmpl/index.htmladmin/views/helloworld/tmpl/edit.phpadmin/helpers/index.htmladmin/helpers/helloworld.phpadmin/tables/index.htmladmin/tables/helloworld.phpadmin/language/en-GB/en-GB.com_helloworld.iniadmin/language/en-GB/en-GB.com_helloworld.sys.iniadmin/controllers/index.htmladmin/controllers/helloworld.phpadmin/controllers/helloworlds.phplanguage/en-GB/en-GB.inimedia/index.htmlmedia/images/index.htmlmedia/images/tux-16x16.pngmedia/images/tux-48x48.pnghelloworld.xml 文件内容如下: COM_HELLOWORLD November 2009 John Doe john.doeexample.org http:/www.example.org Copyright Info 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 19 页 - - - - - - - - - License Info 0.0.14 COM_HELLOWORLD_DESCRIPTION sql/install.mysql.utf8.sql sql/uninstall.mysql.utf8.sql sql/updates/mysql index.html helloworld.php controller.php views models language index.html 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 19 页 - - - - - - - - - images COM_HELLOWORLD_MENU index.html config.xml access.xml helloworld.php controller.php sql tables models views controllers helpers language/en-GB/en-GB.com_helloworld.ini language/en-GB/en-GB.com_helloworld.sys.ini 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 19 页 - - - - - - - - -