Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT :</strong> Here are the final working queries. Also, I thought it might be useful for others to see how the queries are used, so a full explanation is included.</p> <pre><code>//build the array(s) $userperms = array(); $userroles = array(); if(isset($_SESSION['userid'])): //get the userid $thisuserid = $_SESSION['userid']; //get the permissions for each of the user's roles $Cpermissions_role = mysql_query(" SELECT r.type, ur.user_id, pr.perm_id, ur.role_id, p.name FROM user_roles ur LEFT JOIN permissions_role pr ON ur.role_id = pr.role_id LEFT JOIN roles r ON ur.role_id = r.id LEFT JOIN permissions p ON p.id = pr.perm_id WHERE ur.user_id = '$userid'") or die(mysql_error()); //get the extra permissions for the user $Cpermissions_user = mysql_query(" SELECT pu.user_id, pu.perm_id, p.name FROM permissions_user pu LEFT JOIN permissions p ON p.id = pu.perm_id WHERE pu.user_id = '$userid'") or die(mysql_error()); //build an array of the user's roles &amp; an array of the user's permissions if(mysql_num_rows($Cpermissions_role) &gt; 0): while($Rpermissions_role = mysql_fetch_array($Cpermissions_role)): if(empty($userperms)): $userperms[] = $Rpermissions_role['name']; elseif(!in_array($Rpermissions_role['name'],$userperms)): $userperms[] = $Rpermissions_role['name']; endif; if(empty($userroles)): $userroles[] = $Rpermissions_role['type']; elseif(!in_array($Rpermissions_role['type'],$userroles)): $userroles[] = $Rpermissions_role['type']; endif; endwhile; endif; if(mysql_num_rows($Cpermissions_user) &gt; 0): while($Rpermissions_user = mysql_fetch_array($Cpermissions_user)): if(empty($userperms)): $userperms[] = $Rpermissions_user['name']; elseif(!in_array($Rpermissions_user['name'],$userperms)): $userperms[] = $Rpermissions_user['name']; endif; endwhile; endif; endif; /** * Determines if the user has permission for the page or parts of page * @param string $perm the permission constant * @return boolean true if user has access, false if not */ function hasPermission($perm){ global $userperms; if(empty($userperms)): return false; else: if(is_array($userperms)): if(in_array($perm,$userperms)): return true; else: return false; endif; else: if($perm == $userperms): return true; else: return false; endif; endif; endif; } </code></pre> <p>Then, I use the following decision statement:</p> <pre><code>if(hasPermission("ACCESS_HOME_PAGE")): //perform circus tricks here endif; </code></pre> <p>So, now <code>print_r($userperms);</code> outputs this:</p> <pre><code>Array ( [0] =&gt; ACCESS_TELEPHONELIST_PAGE [1] =&gt; ACCESS_VACATIONSCHED_PAGE [2] =&gt; ACCESS_TOURSCHED_PAGE [3] =&gt; ACCESS_WORKSCHED_PAGE [4] =&gt; ACCESS_RESOURCES_LINKS [5] =&gt; ACCESS_PMTOOL_PAGE [6] =&gt; ACCESS_TOOLSTOOL_PAGE [7] =&gt; ACCESS_SHOPTOOLLIST_PAGE [8] =&gt; ACCESS_TOOLINVENTORY_PAGE [9] =&gt; ACCESS_MANAGETOOLLIST_PAGE [10] =&gt; ACCESS_TOOLREPORTS_PAGE [11] =&gt; ACCESS_JOBSLIST_LINKS [12] =&gt; MAIN_TAB_TOOLSTOOL [13] =&gt; ADMIN_TAB_PODMANAGEMENT [14] =&gt; TOOL_TAB_SHOPTOOLLIST [15] =&gt; ACCESS_HOME_PAGE ) </code></pre> <p>And, <code>print_r($userroles);</code> outputs this:</p> <pre><code>Array ( [0] =&gt; administrator [1] =&gt; humanresourcees [2] =&gt; podmanager ) </code></pre> <p>And, just to be thorough, here is the JavaScript I use to access the php array:</p> <pre><code>var js_userperms = new Array(); js_userperms = ["&lt;?php echo join("\", \"", $userperms); ?&gt;"]; </code></pre> <p>And, the jQuery decision statement:</p> <pre><code>if(jQuery.inArray("ACCESS_HOME_PAGE", js_userperms) != -1){ //perform circus tricks here } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload