search
HomeBackend DevelopmentPHP Tutorialyotaku的开发日志(一)

yotaku的开发日志(1)

2015-12-18 21:17:46

连续看了几天的ThinkPHP框架,目前看到基于角色的用户访问权限控制。

 相关代码如下:

数据库

用户表(管理员)

 

mg_id mg_name mg_pwd mg_time mg_role_id
0 creatint 123 2587413547 1
1 yotaku 123 258744984 4
CREAATE TABLE `sw_manager` (    `mg_id` int(11) NOT NULL AUTO_INCREMENT,    `mg_name` varchar(32) NOT NULL,    `mg_pwd` varchar(32) NOT NULL,     `mg_time` int(10) unsigned NOT NULL COMMENT '时间',    `mg_role_id` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '角色id',    PRIMARY KEY (`mg_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

权限表

auth_id(权限ID) auth_name(权限名称) auth_pid(父id) auth_c(控制器) auth_a(操作方法) auth_path(全路径) auth_level(权限级别)
100 产品中心 0 '' '' 100 0
101 产品展示 100 ManagerController show 100-101 1
CREATE TABLE `sw_auth` (    `auth_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,    `auth_name` varchar(20) NOT NULL COMMENT '权限名称',    `auth_pid` smallint(6) unsigned NOT NULL COMMENT'父id',    `auth_c` varchar(32) NOT NULL DEFAULT '' COMMENT '控制器',    `auth_a` varchar(32) NOT NULL DEFAULT '' COMMENT '操作方法',    `auth_path` varchar(32) NOT NULL COMMENT '全路径',    `auth_level` tinyint(4) NOT NULL DEFAULT '0' COMMENT '级别',    PRIMARY KEY(`auth_id`)) ENGING-InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

角色表

role_id role_name role_auth_ids role_auth_ac
0 站主 1,3,9 操作器-控制器,操作器-控制器,...
1 高级管理员 1,2,3,9,12 操作器-控制器,操作器-控制器,...
CREATE TABLE `sw_role` (    `role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,    `role_name` varchar(20) NOT NULL COMMENT '角色名称',    `role_auth_ids` varchar(128) NOT NULL DEFAULT '' COMMENT '权限id,1,3,..',    `role_auth_ac` text COMMENT '控制器2-操作3,控制器1-操作6,...',    PRIMARY KEY(`role_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;<br><br>

 


数据模拟

1.权限数据

  产品中心(产品展示,最新产品,分类管理,子类管理) 高级管理(用户留言,留言簿,产品订购,文件管理) 系统管理(基本设置,样式管理,首页设置,管理员列表)

<span class="zhushi">顶级权限</span>insert into sw_auth values ( 100,'产品中心',0,'','',100,0 );insert into sw_auth values ( 101,'高级管理',0,'','',101,0 );insert into sw_auth values ( 102,'系统管理',0,'','',102,0 );insert into sw_auth values ( 103,'权限管理',0,'','',103,0 );<span class="zhushi">次级权限</span>insert into sw_auth values ( 104,'产品展示',100,'Goods','show','100-104',1 );insert into sw_auth values ( 105,'最新产品',100,'Goods','showlist','100-105',1 );insert into sw_auth values ( 106,'分类管理',100,'Goods','cate','100-106',1 );insert into sw_auth values ( 107,'用户留言',101,'Goods','words','101-107',1 );insert into sw_auth values ( 108,'留言簿',  101,'Goods','wordsbook','101-108',1 );insert into sw_auth values ( 109,'基本设置',102,'Goods','set','102-109',1 );insert into sw_auth values ( 110,'样式管理',102,'Goods','css','102-110',1 );insert into sw_auth values ( 111,'用户列表',103,'Goods','userlist','103-111',1 );insert into sw_auth values ( 112,'角色管理',103,'Goods','role','103-112',1 );insert into sw_auth values ( 113,'权限列表',103,'Goods','auth','103-113',1 );

2.角色数据

  sw_role 站主 所有权限(103,104,105,106,107,108,109) 管理员 部分权限(104,105,109) 版主 部分权限(103,108)

<span class="zhushi">角色</span>insert into sw_role values (10,'站主','100,101,102,103,104,105,106,107,108,109,110,111,112,113','Goods-show,Goods-showlist,Goods-cate,Goods-words,Goods-wordsbook,Goods-set,Goods-css');insert into sw_role values (11,'管理员','100,102,104,105,109','Goods-showlist,Goods-cate,Goods-css');insert into sw_role values (12,'版主','100,101,103,106,108,113','Goods-show,Goods-set');

3.流程说明

  Index控制器内 获取用户的角色id,进而获得角色权限 进行判断是否展现数据 Index控制器--->left方法--->left.html模板 Index控制器

//(1)根据用户id获取本身记录信息
        $mg_id = session('admin_id');        
//D('Manager')实例化了一个Manager的Model对象
        $manager_info = D('Manager')->find($mg_id);        $role_id = $manager_info['mg_role_id'];        
//(2)根据role_id 获得本身记录信息
        $role_info = D('Role')->find($role_id);        $auth_ids = $role_info['role_auth_ids'];        
//(3)根据$auth_ids 获得具体权限
        $auth_infoA = D('Auth')->where("auth_level=0 and auth_id in($auth_ids)")->select();
//父级
        $auth_infoB = D('Auth')->where("auth_level=1 and auth_id in($auth_ids)")->select();
//子级
        $this->assign('auth_infoA',$auth_infoA);        $this->assign('auth_infoB',$auth_infoB);        
//传到模板中
        $this->assign('auth_info',$auth_info);        $this->display();

4.模板 left.html

  {foreach $auth_infoA as $k=>$v}

    

    

      background={$smarty.const.ADMIN_IMG_URL}/menu_bt.jpg >{$v.auth_id})

      href="javascript:void(0);">{$v.auth_name}

    

{$v.auth_id} style="display: none" cellspacing=0 cellpadding=0 width=150 border=0>

      {foreach $auth_infoB as $k2=>$v2}

        {if $v2.auth_pid == $v.auth_id}

          

          

         {/if}

      {/foreach}

      

    

yotaku的开发日志(一){$smarty.const.ADMIN_IMG_URL}/menu_icon.gif" width=9> {$smarty.const.__MODULE__}/{$v2.auth_c}/{$v2.auth_a}" target=right>{$v2.auth_name}

          

  {/foreach}

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software