About PHP Permissions Design Solutions

Source: Internet
Author: User
Tags knowledge base
About PHP Permissions Design
Description: Want to do a PHP permissions menu, depending on the user login from the database to cycle out the different main menu and sub-menu
The database structure is as follows
Cfg_useraccount \ \ Store user Information
Cfg_role \ \ Store role information
Cfg_resource \ \ Store menu Information
Cfg_permission \ \ role-owned permissions

Now is the user ID that can be obtained by login, Fetch to the main menu owned by the user, but what should I do with a submenu that is owned by that user? There's no clue yet.
\\select A.roleid,b.resourcename,b.url from Cfg_permission a joins Cfg_resource B on A.resourceid=b.resourceid where RoleI D in (select Roleid from Cfg_useraccount where username= ' admin ');



PHP Code
  Databases cimcreate database CIM default charset=utf8;//user table CREATE TABLE cfg_useraccount (ID int (4) not NULL PRIMARY KEY auto_ INCREMENT, UserName varchar, PassWord varchar, mobilephone varchar (+), Createtm varchar, U Pdatetm varchar (+), Roleid Int (4)) DEFAULT charset=utf8;//data format and inserting test data insert into Cfg_useraccount (USERNAME,PASSW Ord,mobilephone,createtm,roleid) VALUES (' admin ', ' 111111 ', ' 15957118564 ', ' 2011-12-30 ', ' ten '); INSERT INTO Cfg_ UserAccount (Username,password,mobilephone,createtm,roleid) VALUES (' Liu Cheng ', ' 111111 ', ' 15957118564 ', ' 2011-12-30 ', ' 20 ' Insert into Cfg_useraccount (Username,password,mobilephone,createtm,roleid) VALUES (' Jiang Jongfang ', ' 111111 ', ' 13735862872 ', ' 2011-12-30 ', ' 30 ');//Role Table CREATE TABLE cfg_role (Roleid int (4), RoleName varchar (), Remark varchar) DEFAULT CH arset=utf8;//inserting test data insert into Cfg_role (Roleid,rolename,remark) VALUES (' 10 ', ' super admin ', ' with all system permissions '); INSERT INTO Cfg_ Role (Roleid,rolename,remark) VALUES (' 20 ', ' Technical department ', ' with customer service management, etc. '); insertInto Cfg_role (Roleid,rolename,remark) VALUES (' 30 ', ' Department manager ', ' permission to modify Data '); INSERT into Cfg_role (Roleid,rolename,remark) VALUES (' 40 ', ' Finance manager ', ' statistical attendance and bonuses ');            Resource table CREATE TABLE Cfg_resource (ResourceID int (4), resourcename varchar, Remark varchar), URL varchar (+), insert into Cfg_resource (Resourceid,resourcename,remark,url) VALUES (' 100 ', ' Customer service Management ', ' management of daily customer service Records ', ' Sevicemanager.php '); insert into Cfg_resource (Resourceid,resourcename,remark,url) VALUES (' 101 ', ' Customer Service submenu 1 ', ' Management of daily customer service Records ', ' Sevicemanager.php?action=add '); insert into Cfg_resource (Resourceid,resourcename,remark,url) VALUES ( ' 102 ', ' Customer Service submenu 2 ', ' Manage daily customer service records ', ' Sevicemanager.php?action=del '); insert into Cfg_resource (Resourceid,resourcename, Remark,url) VALUES (' 200 ', ' member Management ', ' member additions and Inquiries ', ' memberservice.php '); insert into Cfg_resource (Resourceid,resourcename, Remark,url) VALUES (' 300 ', ' query management ', ' Query management ', ' search.php '); insert into Cfg_resource (Resourceid,resourcename,remark,url) VALUES (' 400 ', ' Return to management ', ' Return to Customer service records ', ' visit.php '); INSERT INTO Cfg_rEsource (Resourceid,resourcename,remark,url) VALUES (' 500 ', ' Knowledge base ', ' Product FAQs ', ' knowledge.php '); insert into Cfg_resource ( Resourceid,resourcename,remark,url) VALUES (' 600 ', ' data management ', ' data backup statistics related ', ' backup.php '); insert into Cfg_resource ( Resourceid,resourcename,remark,url) VALUES (' 700 ', ' Hardware rework ', ' hardware product rework ', ' repair.php '); insert into Cfg_resource ( Resourceid,resourcename,remark,url) VALUES (' 800 ', ' System Management ', ' System Management related ', ' system.php '); Create table Cfg_permission (Roleid Int (4), ResourceID Int (4))//permission table INSERT into Cfg_permission (Roleid,resourceid) VALUES (' Ten ', ' + '); INSERT INTO Cfg_ Permission (Roleid,resourceid) VALUES (' Ten ', ' $ '), insert into cfg_permission (Roleid,resourceid) VALUES (' 10 ', ' 300 '); Insert into cfg_permission (Roleid,resourceid) VALUES (' Ten ', ' + '); insert into cfg_permission (Roleid,resourceid) VALUES (' Ten ', ' $ '), insert into cfg_permission (Roleid,resourceid) VALUES (' Ten ', ' + '), insert into cfg_permission ( Roleid,resourceid) VALUES (' Ten ', ' ' Max '), insert into cfg_permission (Roleid,resourceid) VALUES (' 10 ', ' 800 ');



------Solution--------------------
Some suggestions

1. The table appears to be merged into a
2. With respect to permissions, you can assign a value to the session after reading permissions
Then, depending on the permissions, include menu/submenu content

The right to judge of course is to use the IF, which should be made by PHP, rather than let MySQL to "read".

Write permissions as a class and then include different files.
------Solution--------------------
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.