Make your permissions an integer and use 1 bit for each menu item or menu items grouped by permission.
Then use a bit wise AND to determine if permissions match.
Both user and links have a permission column.
permissions is an integer
permissions A = 1
permissions B = 2
permissions C = 4
permissions D = 8
permissions E = 16
permissions F = 32
If a menu item is displayed for both Group B and D, then:
if (link_permission & user_permission) is not zero then the user has permission.
The value for the link permissions if only B and D would be:
permissions B + permissions D, or 2+8 (00000010 + 00001000) = 00001010 (10 decimal, A hex)
Now if a user's permissions = 2 (0010) or a user's permission = 8 (1000)
When ANDed with the Link permission of 00001010 the result of a bit wise AND of user permission and link permission will not be zero (true where non-zero = true).
define('LINK_PERMISSION_ACCESS' ,1); // 000000001
define('LINK_PERMISSION_DELETE' ,2) ; // 000000010
define('LINK_PERMISSION_EDIT' ,4) ; // 000000100
define('LINK_PERMISSION_EXPORT' ,8) ; // 000001000
define('LINK_PERMISSION_IMPORT',16) ; // 000010000
define('LINK_PERMISSION_UPDATE',32) ; // 000100000
define('LINK_PERMISSION_VIEW' ,64) ; // 001000000
$linkPermission = LINK_PERMISSION_B + LINK_PERMISSION_D; // 0010 + 01000
$userPermission = LINK_PERMISSION_D; // 01000
You can define group level values as well
define('LINK_PERMISSION_ADMIN' ,255); // 11111111
You can define multiple premissions
I'm going to go Hex rather than Decimal or the number will be unmanagable
define('LINK_PERMISSION_ACCOUNTS' ,0x8000);
define('LINK_PERMISSION_AUDIT' ,0x4000);
define('LINK_PERMISSION_WORKFLOW' ,0x2000);
define('LINK_PERMISSION_BUGTRACKER',0x1000);
A user with only account access would be
`user.permission` = LINK_PERMISSION_ACCOUNTS + LINK_PERMISSION_ACCESS ;
A user with account access, edit, and delete, would be
`user.permission` = LINK_PERMISSION_ACCOUNTS
+ LINK_PERMISSION_ACCESS
+ LINK_PERMISSION_DELETE
+ LINK_PERMISSION_EDIT;
If you would need a user permissions column for each area:
CREATE TABLE IF NOT EXISTS `user` (
...
`accountPermission` int(11) NOT NULL DEFAULT '0',
`workFlowPermission` int(11) NOT NULL DEFAULT '0',
`contactsPermission` int(11) NOT NULL DEFAULT '0',
`campaignPermission` int(11) NOT NULL DEFAULT '0',
But if the number of permissions is 4 or less for example:
define('LINK_PERMISSION_ACCESS' ,1); // 000000001
define('LINK_PERMISSION_DELETE' ,2) ; // 000000010
define('LINK_PERMISSION_EDIT' ,4) ; // 000000100
define('LINK_PERMISSION_VIEW' ,8) ; // 000001000
`permission` int(11) NOT NULL DEFAULT '0',
Where account, workflow, contacts, and campaign are grouped into 4 bits:
account workflow contacts campaign
0000 0000 0000 0000
PERMISSION_ACCOUNT_ACCESS, 0x1000
PERMISSION_WORKFLOW_ACCESS, 0x0100
PERMISSION_CONTACTS_ACCESS, 0x0010
PERMISSION_CAMPAIGN_ACCESS, 0x0001
PERMISSION_ACCOUNT_DELETE, 0x2000
PERMISSION_WORKFLOW_DELETE, 0x0200
PERMISSION_CONTACTS_DELETE, 0x0020
PERMISSION_CAMPAIGN_DELETE, 0x0002
PERMISSION_ACCOUNT_EDIT, 0x4000
PERMISSION_WORKFLOW_EDIT, 0x0400
PERMISSION_CONTACTS_EDIT, 0x0040
PERMISSION_CAMPAIGN_EDIT, 0x0004
PERMISSION_ACCOUNT_VIEW, 0x8000
PERMISSION_WORKFLOW_VIEW, 0x0800
PERMISSION_CONTACTS_VIEW, 0x0080
PERMISSION_CAMPAIGN_VIEW, 0x0008
BACK TO YOUR LINKS
Define Constants
define ('SERVER_ADMIN',2);
define ('UBUNTU_DASHBOARD',4);
define ('REDIS_CACHE_ADMIN',8);
define ('MYSQL_DB_MANAGEMENT',16);
define ('NEON_AND_MORE',32);
define ('NEON_AND_MORE_(NAM)',64);
define ('SUGARCRM',128);
define ('NAM_MAGENTO_ADMIN',256);
define ('NAM_TIME_CLOCK',512);
define ('NEONANDMORE_BLOG_ADMIN',1024);
define ('ORDER_REPORTS',2048);
define ('WORK_ORDERS',4096);
define ('UPDATE_ORDER_STATUS',8192);
define ('CHANNEL_LETTER',16384);
define ('CHANNEL_LETTER',32768);
define ('MAGENTO_ADMIN',65536);
define ('BORDER_TUBING',131072);
define ('BORDER_TUBING',262144);
define ('SIGN_PARTS_AND_MORE',524288);
define ('SIGN_PARTS_AND_MORE',1048576);
define ('OTHER_SERVICES',2097152);
define ('PUSHER_REALTIME_EVENTS',4194304);
define ('ZOPIM_CUSTOMER_SUPPORT_CHAT',8388608);
define ('GOOGLE_ANALYTICS',16777216);
define ('GITLAB_(PRIVATE_GITHUB_CLONE)',33554432);
define ('LABS_/_PROJECTS',67108864);
define ('NAM_LABS',134217728);
define ('CAMERA_PHONE',268435456);
define ('SERVER_EMAIL_VERIFICATION',536870912);
Both links and users have a permissions column:
`permissions` int(11) NOT NULL DEFAULT '0',
define('LINK_PERMISSION_ACCOUNTS' ,0x8000);
define('LINK_PERMISSION_AUDIT' ,0x4000);
define('LINK_PERMISSION_WORKFLOW' ,0x2000);
define('LINK_PERMISSION_BUGTRACKER',0x1000);
If a user has accounts and bug tracker access:
$userPermission = LINK_PERMISSION_ACCOUNTS + LINK_PERMISSION_BUGTRACKER;
UPDATE `users` SET `permissions`= $userPermission WHERE `id` = $user
Then the required links permissions:
$linkPermission = LINK_PERMISSION_ACCOUNTS;
We do a Bit wise AND (&) on the links permissions with the user permissions
SELECT * FROM `links` WHERE (`permissions` & $userPermission)
It does not matter if the link is a sub-menu link
This is your typical hierarchical table:
CREATE TABLE IF NOT EXISTS `links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NOT NULL DEFAULT '0',
`sort` int(11) NOT NULL DEFAULT '0',
`text` char(32) COLLATE utf8_bin NOT NULL,
`link` text COLLATE utf8_bin NOT NULL,
`permission` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;
Instead we can eliminate, parent and sort, the id column will do it all.
a menu structure with 2 levels: main and sub-menu, 'id' is broken down
menu sub-menu
menu numbers are 0x0100 through 0xFF00
sub-menu numbers are 0x0002 through 0x00FE
For this menu:
SQL to Create Links Table:
CREATE TABLE IF NOT EXISTS `links` (
`id` int(11) NOT NULL,
`text` char(64) COLLATE utf8_bin NOT NULL,
`link` text COLLATE utf8_bin NOT NULL,
`permission` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `links` (`id`, `text`, `link`, `permission`) VALUES
(512, 'Server Admin', '#', 1),
(514, 'Ubuntu Dashboard', '#', 2),
(518, 'Redis Cache Admin', '#', 4),
(522, 'MySQL dB Management', '#', 8),
(1024, 'Neon and More', '#', 16),
(1026, 'Neon and More (NAM)', '#', 32),
(1030, 'SugarCRM', '#', 64),
(1034, 'NAM Magento Admin', '#', 128),
(1038, 'NAM Time Clock', '#', 256),
(1042, 'NeonAndMore Blog Admin', '#', 512),
(1046, 'Order Reports', '#', 1024),
(1050, 'Work Orders', '#', 2048),
(1054, 'Update Order Status', '#', 4096),
(1536, 'Channel Letter', '#', 8192),
(1538, 'Channel Letter', '#', 16384),
(1542, 'Magento Admin', '#', 32768),
(2048, 'Border Tubing', '#', 65536),
(2050, 'Border Tubing', '#', 131072),
(2560, 'Sign Parts And More', '#', 262144),
(2562, 'Sign Parts And More', '#', 524288),
(3072, 'Other Services', '#', 1048576),
(3074, 'Pusher Realtime Events<br/>Instant Caller ID Alerts', '#', 2097152),
(3078, 'Zopim Customer Support Chat', '#', 4194304),
(3082, 'Google Analytics', '#', 8388608),
(3086, 'GitLab (Private GitHub Clone)', '#', 16777216),
(3584, 'Labs / Projects', '#', 33554432),
(3586, 'NAM LABS', '#', 67108864),
(3590, 'Camera Phone', '#', 134217728),
(3594, 'Server Email Verification', '#', 268435456);
Now to create the HTML for the links menu:
SQL
SELECT `id`, `text`, `link`, `permission`
FROM `links`
WHERE (`permission` & $userpermission )
PHP
HEAD and CSS
<?php
ob_start("ob_gzhandler");
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=5, max=100');
header('Cache-Control: max-age=84600');
header('Vary: Accept-Encoding');
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Daily Rx</title><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
.submenu,.mainmenu{text-align:left;border-radius: 3px 3px 3px 3px;font: 700 1.1em Arial,Helvetica,Calibri,sans-serif;overflow: visible;}
.submenu{border:1px solid #0f0;color: #fff;margin:.2em 0 .2em .8em;width:16.8em;padding: 0 0 0 .8em;
background-image: -o-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -moz-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -webkit-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -ms-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: linear-gradient(to bottom, #3d5 0%, #370 100%);}
.mainmenu{font-size:1.2em;margin:.2em .2em .2em .2em ;width:16em;padding-left:1em;border:1px solid #00f;color: #fff;
background-image: -o-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -moz-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -webkit-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -ms-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: linear-gradient(to bottom, #2ef 0%, #02f 100%);}
.hide{display:none;}
#x{height:40em;}
#page{margin:0;padding:0;}
hr{font-size:.1em;padding:0;margin:0 0 0 1em;width:50em;opacity:0;}
</style></head><body><div id="page">
EOT;
ob_flush();
Create Menus
$userpermission = 4294967295; // 0xffffffff
$sql = "SELECT `id`, `text`, `link`, `permission` FROM `links` WHERE (`permission` & $userpermission ) > 0";
$results = mysqli_query($conn,$sql);
if (mysqli_errno($conn) > 0){echo mysqli_error($conn) . "<br>\n$sql\n";}
while($row = mysqli_fetch_array($results, MYSQL_NUM)){
$class = $row[0] & 1;
if($class == 0){
$i++;
echo "$closeSubmenu\n<button class=\"mainmenu\" onclick=\"show($i)\">$row[1]</button>\n<div class=\"hide\" id=\"d$i\">\n";
}
else{
echo "<form action=\"$row[2]\"><div><input type=\"hidden\" name=\"user\" value=\"$user\" /><input type=\"hidden\" name=\"id\" value=\"$row[0]\" /><input type=\"hidden\" name=\"permission\" value=\"$userpermission\" /><button class=\"submenu\">$row[1]</button></div></form>\n";
}
$closeSubmenu = '</div><hr/>';
}
JavaScript to expand and contract sub menus
ob_flush();
echo <<<EOT
</div><div id="x"><p> </p></div>
<script type="text/javascript">
//<![CDATA[
var toggle = new Array();
toggle[''] ='block';
toggle['none'] ='block';
toggle['block'] ='none';
var div,disp;
var prev = document.getElementById('d1');
prev.style.display='none';
function show(id){
div = document.getElementById('d' + id);
disp = div.style.display;
prev.style.display='none';
div.style.display=toggle[disp];
prev=div;
var y=div.offsetTop;
window.scrollTo(0, y-32);
}
//]]>
</script></div></body></html>
EOT;
ob_end_flush();
?>
FAST Page Load, Just 132 milliseconds
This PHP page loads in the Browse in just over 100 milliseconds.
That is just the time it takes to the TPC/IP connection.
The time it takes the HTML to be transmitted from the Server to the Browser is just 2 milliseconds.
The below image is from http://www.webpagetest.org
DNS Lookup: 20 ms
Initial Connection: 35 ms
Time to First Byte: 95 ms
Content Download: 2 ms
W3C MobileOK Checker Score: 100%
W3C mobileOK Checker
You will not find many web pages that can do this:
Google PageSpeed Insights 100%
Mobile and Desktop Speed and Usability
Google PageSpeed Insights
Snippet
This snippet was made using the PHP above and pasting the View Source here:
var toggle = new Array();
toggle[''] ='block';
toggle['none'] ='block';
toggle['block'] ='none';
var div,disp;
var prev = document.getElementById('x');
function show(id){
div = document.getElementById('d' + id);
disp = div.style.display;
prev.style.display='none';
div.style.display=toggle[disp];
prev=div;
var y=div.offsetTop;
window.scrollTo(0, y-32); }
.submenu,.mainmenu{text-align:left;border-radius: 3px 3px 3px 3px;font: 700 1.1em Arial,Helvetica,Calibri,sans-serif;overflow: visible;}
.submenu{border:1px solid #0f0;color: #fff;margin:.2em 0 .2em .8em;width:16.8em;padding: 0 0 0 .8em;
background-image: -o-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -moz-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -webkit-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: -ms-linear-gradient(bottom, #3d5 0%, #370 100%);
background-image: linear-gradient(to bottom, #3d5 0%, #370 100%);}
.mainmenu{font-size:1.2em;margin:.2em .2em .2em .2em ;width:16em;padding-left:1em;border:1px solid #00f;color: #fff;
background-image: -o-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -moz-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -webkit-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: -ms-linear-gradient(bottom, #2ef 0%, #02f 100%);
background-image: linear-gradient(to bottom, #2ef 0%, #02f 100%);}
.hide{display:none;}
#x{height:40em;}
#page{margin:0;padding:0;}
hr{font-size:.1em;padding:0;margin:0 0 0 1em;width:50em;opacity:0;}
<div id="page">
<button class="mainmenu" onclick="show(1)">Server Admin</button>
<div class="hide" id="d1">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="257" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Ubuntu Dashboard</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="259" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Redis Cache Admin</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="261" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">MySQL dB Management</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(2)">Neon and More</button>
<div class="hide" id="d2">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="513" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Neon and More (NAM)</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="515" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">SugarCRM</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="517" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">NAM Magento Admin</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="519" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">NAM Time Clock</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="521" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">NeonAndMore Blog Admin</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="523" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Order Reports</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="525" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Work Orders</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="527" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Update Order Status</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(3)">Channel Letter</button>
<div class="hide" id="d3">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="769" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Channel Letter</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="771" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Magento Admin</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(4)">Border Tubing</button>
<div class="hide" id="d4">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1025" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Border Tubing</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(5)">Sign Parts And More</button>
<div class="hide" id="d5">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1281" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Sign Parts And More</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(6)">Other Services</button>
<div class="hide" id="d6">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1537" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Pusher Realtime Events<br/>Instant Caller ID Alerts</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1539" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Zopim Customer Support Chat</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1541" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Google Analytics</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1543" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">GitLab (Private GitHub Clone)</button></div></form>
</div><hr/>
<button class="mainmenu" onclick="show(7)">Labs / Projects</button>
<div class="hide" id="d7">
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1793" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">NAM LABS</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1795" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Camera Phone</button></div></form>
<form action="#"><div><input type="hidden" name="user" value="123" /><input type="hidden" name="id" value="1797" /><input type="hidden" name="permission" value="4294967295" /><button class="submenu">Server Email Verification</button></div></form>
</div><div id="x"><p> </p></div>