Todo not showing up in 'Content' context menu after adding contoller content.php

Posted by Darwin Biler on December 23, 2012
This post is for PHP Developers that was trying to do the "ToDo" module tutorial in CI Bonfire.

There seems a fundamental problem in the supposedly "basic" tutorial of CI Bonfire. Maybe due to the fast transition of changes in the current version of the said CI flavor. But either way, let me teach you how to solve this problem.

First, in the Migration of your module, you have to edit that file and add this array in the migration class:

 private $permission_array = array(
        'MODULE.Content.Manage' => 'Manage ...',
        'MODULE.Content.Add' => 'Add ...',
        'MODULE.Content.Delete' => 'Delete ...',
	'MODULE.Content.View' => 'To view the .... Content menu.'
    );

Note that you should replace the MODULE with the actual module name. Also the value of each key should be adjusted to whatever caption you would like.

Next. Add this in the up method of your migration class.

$prefix = $this->db->dbprefix;
foreach ($this->permission_array as $name => $description)
{
   $this->db->query("INSERT INTO {$prefix}permissions(name, description) VALUES('".$name."', '".$description."')");
   // give current role (or administrators if fresh install) full right to manage permissions
   $this->db->query("INSERT INTO {$prefix}role_permissions VALUES(1,".$this->db->insert_id().")");
}

This will insert those permissions in the permissions table, so that Bonfire will be aware of those permissions.
It will also insert those permission to the Admin role, so that the admin roles will gonna have those permissions.

Add this in your migration file's down method so that the permissions will be revoked when the module is uninstalled.

$prefix = $this->db->dbprefix;
foreach ($this->permission_array as $name => $description)
{
	$query = $this->db->query("SELECT permission_id FROM {$prefix}permissions WHERE name = '".$name."'");
	foreach ($query->result_array() as $row)
	{
		$permission_id = $row['permission_id'];
		$this->db->query("DELETE FROM {$prefix}role_permissions WHERE permission_id='$permission_id';");
	}
	//delete the role
	$this->db->query("DELETE FROM {$prefix}permissions WHERE (name = '".$name."')");
}

Then on the Content controller, make sure you add this line to the method to enforce the permission:

$this->auth->restrict('MODULE.Content.Manage');

After saving the changes, go to admin,
Developer > Database Tools > Migrations
Modules tab
run the uninstall of your module
then re-install it again.

You should be fine now.


Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).