Develop new module - Part 3 - Module permissions

From TomatoCMS Documentation

Jump to: navigation, search

At this step, we will complete the simple module.

Contents

Step 1: Define the routes

Create a contact.ini file

TomatoCMS_Root_Folder
|__app
	|__modules
		|__contact
			|__config
				|__ routes
					|__contact.ini

This file define the routes match with our module's action.
Assume, we have two action in this module, list and add, so content of this contact.ini file is

[routes]
; ========== Backend actions ===================================================
routes.contact_index_list.type = "Zend_Controller_Router_Route_Static"
routes.contact_index_list.route = "admin/contact/list/"
routes.contact_index_list.defaults.module = "contact"
routes.contact_index_list.defaults.controller = "Index"
routes.contact_index_list.defaults.action = "list"
 
routes.contact_index_add.type = "Zend_Controller_Router_Route_Static"
routes.contact_index_add.route = "admin/contact/add"
routes.contact_index_add.defaults.module = "contact"
routes.contact_index_add.defaults.controller = "Index"
routes.contact_index_add.defaults.action = "add"

Note that, you should write the route name in order moduleName_controllerName_actionName, to ensure that routes are difference.
In backend section, you should write route start with admin to distinguish from frontend section.

Step 2: Define permission

How to manage access permission of users, we will create permissions.xml file to solve this issue.

TomatoCMS_Root_Folder
|__app
	|__modules
		|__contact
			|__config
				|__ permissions.xml

with content like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE permissions SYSTEM "http://schemas.tomatocms.com/dtd/module_permission.dtd">
<permissions>
	<resource name="index" description="Manage contacts" langKey="permisson_contact">
		<privilege name="list" description="View the list of contacts" langKey="permisson_contact_list" />
		<privilege name="add" description="Add new contact" langKey="permisson_contact_add" />
	</resource>
</permissions>

Note, name of resource is name of controller, and name of privilege is name of action correspond to contact.ini file.

At here, we see langKey again, so at lang.en_US.ini file, we have to add following code

permisson_contact = "Manage contacts"
permisson_contact_list = "View the list of contacts"
permisson_contact_add = "Add new contact"

Step 3: Add resource

And then, we have to add this module to our system.
Click User/Privileges at the backend

You will see the list of resources of modules, and see our module which has created

File:Addmodule_resource_module_add.png

Click to Add and + symbol to add our resource module to system.
Then, you will see

File:Addmodule_resource_module_added.png

it's mean our module was added to system.
Now, come back to administrator and click to one of two link we have just created.
Let's guest what do we see?

It's a empty page, let's write something will display on it.

Step 4: Init to code

We will create controller and view to start code.

TomatoCMS_Root_Folder
|__app
	|__modules
		|__contact
			|__controllers
				|__ indexController.php
 
TomatoCMS_Root_Folder
|__app
	|__modules
		|__contact
			|__views
				|__ scripts
					|__index
						|__list.phtml
 
TomatoCMS_Root_Folder
|__app
	|__modules
		|__contact
			|__views
				|__ scripts
					|__index
						|__add.phtml

Put following code into indexController.php file

<?php
class Contact_IndexController extends Zend_Controller_Action
{
	public function listAction()
	{
 
	}
 
	public function addAction()
	{
 
	}
}

Note that we should write the name of controllers and actions follow Zend Framework' standard like Contact_IndexController, listAction, and addAction
And what's about the view? Example, put the following code into list.phtml file

<h1>List of contacts</h1>

Refresh your page, and see something new.
At the and of this step, we had a simple module.
We will connect database and preview data on our page at next part.
<to be continuos...>

Personal tools