权限控制的一个思路
数据库表结构
<span CREATE</span> <span TABLE</span><span `NewTable` ( `id` </span><span int</span>(<span 11</span>) <span NOT</span> <span NULL</span><span AUTO_INCREMENT, `title` </span><span varchar</span>(<span 255</span>) <span NOT</span> <span NULL</span><span , `menu_id` </span><span int</span>(<span 11</span>) <span NOT</span> <span NULL</span><span , `module` </span><span varchar</span>(<span 50</span>) <span NOT</span> <span NULL</span><span , `class` </span><span varchar</span>(<span 50</span>) <span NOT</span> <span NULL</span><span , `method` </span><span varchar</span>(<span 50</span>) <span NOT</span> <span NULL</span><span PRIMARY</span> <span KEY</span><span (`id`) );</span>
假定我们使用了MVC的结构,并通过url访问对应的模块及类和函数。
表中第一行表示一个操作,title表示操作名,menu_id表示属于哪一个菜单选项,module表示模块名(没有可以略过),class表示类名,method表示函数名
此外我们还需要一个用户组的表,大致如下:
其中access_list中保存的当前用户组所拥有的权限ID(对应上个权限表中的ID)
当然我们还需要一个用户表来对应用户组表
group_id表示的第用户所对应的用户组
当我们访问一个url的时候,比如:
http://testApp.test.com/index.php/module/testModule/testClass/testAction
通过路由分析,我们得到相应的数据:
模块->testModule
类->testClass
函数->testAction
通过三个参数,我们查找权限表中的数据,得到一个唯一的id值。
然后对比用户组中access_list中的数据,如果数据中包括有这个id,那么当前用户拥有当前操作的权限,反之没有权限。
那么我们如何来控制菜单选项,当用户有权限时才显示呢?
因为在权限表中的每个操作都对应有一个menu_id,即菜单选项。我们通过查找当前用户所属于的用户组信息,得到他所拥有的权限操作ID,再通过ID取得他所属于的菜单选项,这样就得到了当前用户所拥有的菜单权限。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools