IsAllowed -Controlling Content Access by UserID

Forums:

I wanted to be able to control drupal access to internal content generated by php.
You must have the module to allow php in articles turned on. This could be a security risk though.
The concept is fairly simple:
Get the ID of the user on load.
Pass the id to a function called IsAllowed.
Return true or false based on whether or not the current ID is in the list of IDs array.
Allow content based on the response.

Note:
If you had copied this snippet prior to 8/22/2018, copy it again.
It turns out that quotes on the array were causing problems on some themes.
I removed the quotes and it works on both themes I was testing now.
If you still have probs, try changing the funcion and function calls to another name.

Here is the code snippet:

<?php
global $user;
$cuserid= $user->uid;
function IsAllowed($inid)
{
$allowedids = array(1,22);
$ia=false;
foreach($allowedids as $chk)
{
if ($chk==$inid)
{
$ia=true;
}
}
return $ia;
}
if (IsAllowed($cuserid))
{
print ("You are # $cuserid !");
}
else
{
print ("do that");
}

?>

I believe you can also wrap html with php blocks if you don't want to "print" out the html code using php.

Example:
<?php
if (IsAllowed($cuserid))
{
?>
< div>Welcome wonderful< /div>
<?php
}
else
{
?>
< div>Sorry, but no can do...< /div>
<?php
}
?>

Cuzin Ican's picture

You could also set the ID list as a global variable and call it as such from inside the function.
This would allow you to have the list at the top of the code and the function somewhere else in the code.
I may just change it to do that.