User

Difference between revisions of "Rvsjoen/tutorial/Developing an MVC Component/Part 14"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
Line 211: Line 211:
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/index.html]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_12#admin/helloworld.php|admin/helloworld.php]]</tt>
+
* <tt>[[#admin/helloworld.php|admin/helloworld.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_12#admin/controller.php|admin/controller.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_12#admin/controller.php|admin/controller.php]]</tt>
 
* <tt>[[#admin/access.xml|admin/access.xml]]</tt>
 
* <tt>[[#admin/access.xml|admin/access.xml]]</tt>

Revision as of 11:51, 12 July 2011

Adding ACL[edit]

The time has come to add some access control to our component, we don't want nasty critters playing with our greetings now do we. So we will start by writing a relatively simple access.xml file in order to define which actions we want available for our component and the messages.

Creating the access XML[edit]

With your favorite editor, create the following file

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>

Adding access configuration to component options[edit]

Now that we have declared ourselves some actions that may or may not be performed on certain objects, we need a way for the user to be able to configure which groups get to perform what actions and which do not. This already exists for us, all we have to do it to add it to config.xml

With your favorite editor, modify the following file to look like this

admin/config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
        <fieldset
                name="greetings"
                label="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL"
                description="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC">
                <field
                        name="show_category"
                        type="radio"
                        label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
                        description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
                        default="0">
                        <option value="0">JHIDE</option>
                        <option value="1">JSHOW</option>
                </field>
        </fieldset>
        <fieldset
                name="permissions"
                label="JCONFIG_PERMISSIONS_LABEL"
                description="JCONFIG_PERMISSIONS_DESC">
                <field
                        name="rules"
                        type="rules"
                        label="JCONFIG_PERMISSIONS_LABEL"
                        class="inputbox"
                        validate="rules"
                        filter="rules"
                        component="com_helloworld"
                        section="component" 
                />
        </fieldset>
</config>

Enforcing permissions[edit]

admin/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

// Set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-48-helloworld {background-image: url(../media/com_helloworld/images/tux-48x48.png);}');

// 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');

// 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();

Installation manifest[edit]

In the installation manifest, we have updated the version number and added access.xml

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">

        <name>COM_HELLOWORLD</name>
        <!-- The following elements are optional and free of formatting constraints -->
        <creationDate>June 2011</creationDate>
        <author>John Doe</author>
        <authorEmail>john.doe@example.org</authorEmail>
        <authorUrl>http://www.example.org</authorUrl>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <!--  The version string is stored in the components table -->
        <version>0.0.14</version>
        <!-- The description is optional and defaults to the name -->
        <description>COM_HELLOWORLD_DESCRIPTION</description>

        <install>
                <sql>
                        <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
                </sql>
        </install>
        <uninstall>
                <sql>
                        <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
                </sql>
        </uninstall>

        <!-- Note the folder attribute: This attribute describes the folder
                to copy FROM in the package to install therefore files copied
                in this section are copied from "site/" in the package -->
        <files folder="site">
                <filename>index.html</filename>
                <filename>helloworld.php</filename>
                <filename>controller.php</filename>
                <folder>views</folder>
                <folder>models</folder>
                <folder>language</folder>
        </files>

        <media destination="com_helloworld" folder="media">
                <filename>index.html</filename>
                <folder>images</folder>
                <folder>js</folder>
        </media>

        <administration>
                <menu img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
                <submenu>
                        <menu view="helloworlds">COM_HELLOWORLD_SUBMENU_MESSAGES</menu>
                        <menu link="option=com_categories&amp;view=categories&amp;extension=com_helloworld">COM_HELLOWORLD_SUBMENU_CATEGORIES</menu>
                </submenu>
                <!-- Note the folder attribute: This attribute describes the folder
                        to copy FROM in the package to install therefore files copied
                        in this section are copied from "admin/" in the package -->
                <files folder="admin">
                        <filename>index.html</filename>
                        <filename>access.xml</filename>
                        <filename>helloworld.php</filename>
                        <filename>controller.php</filename>
                        <filename>config.xml</filename>
                        <folder>sql</folder>
                        <folder>tables</folder>
                        <folder>models</folder>
                        <folder>views</folder>
                        <folder>language</folder>
                        <folder>controllers</folder>
                        <folder>helpers</folder>
                </files>
        </administration>

</extension>

Testing your component[edit]

For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.


File listing[edit]

Download this part[edit]

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5