《2022年Joomla.MVC组件开发教程 2.pdf》由会员分享,可在线阅读,更多相关《2022年Joomla.MVC组件开发教程 2.pdf(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、本节标题:添加访问控制列表(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 dif
2、ferent ACL into two sections: components and messages. admin/access.xml 限制访问组件ACL的主要目的是限制对用户组的直接操作行为,第一个行为就是对组件本身的访问。修改入口文件:admin/helloworld.php名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 19 页 - - - - - - - - - authorise(core.manage, com_helloworld) return J
3、Error: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
4、 = 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 f
5、or 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-setDoc
6、ument(); /* * Setting the toolbar */ protected function addToolBar() /每个都加入了判断来控制显示与否 $canDo = HelloWorldHelper:getActions(); JToolBarHelper:title(JText:_(COM_HELLOWORLD_MANAGER_HELLOWORLDS), helloworld); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 19 页 - -
7、- - - - - - - 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) J
8、ToolBarHelper: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.h
9、tml.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-addTool
10、Bar(); / Display the template parent:display($tpl); / Set the document $this-setDocument(); /* * Setting the toolbar */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 19 页 - - - - - - - - - protected function addToolBar() JRequest:setVar(hidemainmenu, true); $u
11、ser = 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
12、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_SA
13、VE_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 perm
14、ission 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 ($c
15、anDo-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
16、-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
17、/helloworld/submitbutton.js); JText:script(COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 19 页 - - - - - - - - - 以上两个文件使用了admin/helpers/helloworld.php中定义的getActions方法。修改该文件如下admin/helpers/helloworld.phpaddStyleDecl
18、aration(.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 页 - - - - - -
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:getAction
20、s(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之类
21、的 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 r
22、eference 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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - -
23、 - - - - - - - 名师精心整理 - - - - - - - 第 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 bo
24、olean $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, ar
25、ray(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; /* * M
26、ethod 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
27、 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; retu
28、rn 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 JR
29、egistry; $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
30、_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 */ protec
31、ted 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 更多的信息可以从以下教程中获
32、取:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 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/v
33、iews/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-G
34、B.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/u
35、pdates/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
36、名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 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
37、/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/submi
38、tbutton.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/in
39、dex.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
40、 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
41、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 页 - - - - - - - - -