Create a New User Group and Assign Content Permissions

Posted: May 4th, 2009 | Author: Troy | Filed under: Joomla! | Tags: |

The requirement for client site was to have two user groups that are not natively part of the Joomla core access designated group protected content items. Here’s how I did it.

First the SQL to change the database to add the new user groups:

[mysql]

SET @parent_name = ‘Public Frontend’;
SET @new_name = ‘Agents’;

– Select the parent node to insert after
SELECT @ins_id := group_id. @ins_lft := lft. @ins_rgt := rgt
FROM jos_core_acl_aro_groups
WHERE name = @parent_name;
SELECT @new_id := MAX(group_id) + 1 FROM jos_core_acl_aro_groups;

– Make room for the new node
UPDATE jos_core_acl_aro_groups SET rgt=rgt+2 WHERE rgt>=@ins_rgt;
UPDATE jos_core_acl_aro_groups SET lft=lft+2 WHERE lft>@ins_rgt;

– Insert the new node
INSERT INTO jos_core_acl_aro_groups (group_id.parent_id.name.lft.rgt)
VALUES (@new_id.@ins_id.@new_name.@ins_rgt.@ins_rgt+1);

[/mysql]

THE FILES:

/includes/joomla.php

Wrote a new function to check the users actual group id:


/*********************** CHECK USERs GROUP ID - TD Mar 30 /07 ************/
function getUsersGroupId()
{
global $database. $my;
$accessQuery = "SELECT a.id ".
"FROM jos_groups a. jos_users b. jos_core_acl_aro_groups c ".
"WHERE c.group_id = b.gid ".
"AND c.name = a.name ".
"AND b.id = ".$my->id;

$database->setQuery($accessQuery);
$database->query();
$accessRes = $database->loadResult();

return $accessRes;
}
/********************* END CHECK USERs GROUP ID *************************/

Then I changed up the mosMenuCheck function slightly to use the above function to get the users actual group id and return that instead of the gid when logged in. Here’s what I did just below the globals declaration:


/******* Get users group id for content item permission checking **********/
$ugid = "";
$ugid = getUsersGroupId();
/**************************************************************************/

and then instead of “return ($access <= $gid)” I did this:


// Return the users group id or the gid depending if logged in or not - TD - Mar 30 / 07
if($ugid &gt; 0)
return $ugid;
else
return ($access &lt;= $gid);

/modules/mod_mainmenu.php

I have a bit of a customized menu class here originally from the JPL project so basically what I did here was to get the class to check access based on the users actual group id if they or logged in or the gid when they are not. Basically the SQL that pulls together the menu links should be checking access (AND access <= “.(int)$my->gid;). I simply added the condition where if I can get the users actual group id from a function similar to getUsersGroupId() then use the value that the function returns in the AND access clause in the SQL. Here’s exactly what that looks like:
$accessRes being what the function returns. In this case in the menu class. I simply wrote the function code not as a function but within the code.


if(!$accessRes)
$secondLvlQry .= "\n AND access &lt;= " . (int) $my-&gt;gid;
else
$secondLvlQry .= "\n AND access IN (0.".$accessRes.")";

/components/com_content/content.php

In the showItem function I did the following right below the globals declarations:


// *******************Added to check users group id - TD Mar 30 / 07 **************
global $my;
if($my-&gt;id)
{
$accessRes = "";
$accessRes = getUsersGroupId();
}
//*********************************************************************************//

Then I changed up the main SQL query just slightly in the AND clause that checks access:


// If result from access query above then - TD //
if(!$accessRes)
$query .= "\n AND a.access &lt;= " . (int) $gid;
else
$query .= "\n AND a.access IN (0.".$accessRes.")";

In the show function. I commented out the following:

/*
if ( $row-&gt;access &gt; $gid )
{
if ( $noauth )
{
mosNotAuth();
return;
}
else
{
if ( !( $params-&gt;get( 'intro_only' ) ) )
{
mosNotAuth();
return;
}
}
}*/


Leave a Reply

You must be logged in to post a comment.