Table of Contents
yotaku’s development log (1), yotaku development log
Home Backend Development PHP Tutorial Yotaku's development log (1), yotaku development log_PHP tutorial

Yotaku's development log (1), yotaku development log_PHP tutorial

Jul 12, 2016 am 09:03 AM
vbs

yotaku’s development log (1), yotaku development log

2015-12-18 21:17:46

I have been looking at the ThinkPHP framework for several days and now I see role-based user access control.

The relevant code is as follows:

Database

User table (administrator)

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;
Copy after login

Permission table

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;
Copy after login

Character List

role_idrole_namerole_auth_idsrole_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 />
Copy after login


数据模拟

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 );

Copy after login

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');
Copy after login

3.流程说明

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

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

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}>      {foreach $auth_infoB as $k2=>$v2}        {if $v2.auth_pid == $v.auth_id}                              {/if}      {/foreach}          
{$v2.auth_name}           
  {/foreach}  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1081947.htmlTechArticleyotaku的开发日志(1),yotaku开发日志 2015-12-1821:17:46 连续看了几天的ThinkPHP框架,目前看到基于角色的用户访问权限控制。 相关代码如下: 数...
Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable the VBS function of Win11 How to enable the VBS function of Win11 Dec 25, 2023 pm 02:09 PM

If you want to open vbs after closing it before, you can also open it. We can use the command code to open it. Let's take a look at how to open vbs. It is actually very simple. How to open win11vbs: 1. First, we click on the “Start Menu”. 2. Then click "Windows Terminal". 3. Then enter "bcdedit/sethypervisorlaunchtypeauto". 4. Then restart the computer, open the start menu, and search for "system information" in the search bar. 5. Then check whether "virtualization-based security" is turned on.

How to open vbs in win11 system? How to restart vbs in win11 How to open vbs in win11 system? How to restart vbs in win11 Jan 31, 2024 pm 11:24 PM

vbs is the abbreviation of Virtual-BasedSecurity, which is a security function based on virtualization. However, in Windows 11 systems, it is turned off by default. For users who need to use this feature, they may not know how to enable it. Fortunately, we can enable it via Windows Terminal (as administrator). For the convenience of users, we will share detailed steps in today’s Win11 tutorial. If you need more information, please visit our website. Tutorial on restarting vbs in win11 1. First, we right-click the start menu on the left side of the taskbar. 3. Then enter bcdedit/sethypervisorlaun

Windows11 VBS Tutorial: How to gracefully close applications? Windows11 VBS Tutorial: How to gracefully close applications? Mar 08, 2024 am 11:54 AM

Windows 11 VBS Tutorial: How to gracefully close applications? When using the Windows 11 operating system daily, we often encounter situations where we need to close multiple applications at the same time. Sometimes we may habitually click the close button, or use the task manager to end application processes one by one. However, using VBS script (VisualBasicScript) can more efficiently close multiple applications at once, making the operation smoother and more convenient. 1.What is

Windows Script Host error 'The system cannot find the file specified' fix Windows Script Host error 'The system cannot find the file specified' fix Apr 13, 2023 pm 12:22 PM

Windows Script Host provides users with an environment for executing scripts. Some scripts execute as soon as you log into your computer, and others may execute when you trigger them. When you log into your computer and then find an error saying "The system cannot find the file specified" error, this article will help you troubleshoot the problem. Well, the reasons for this problem can be different like malware attack or improper installation or some files have been deleted etc. Here we have different solutions that you can try and solve your problem, so let's get started! Method 1 – Download the autorun utility and check vbs entries Step 1 – Go to this website and click on the download icon in the upper right corner and save Step 2 – After downloading the folder

How to use vbs whole person code How to use vbs whole person code Nov 22, 2023 am 10:05 AM

First of all, it needs to be clear that using pranks or bullying codes may cause unnecessary distress or harm to others. It is recommended to think twice before writing and implementing any form of malicious code. Such behavior is irresponsible and unethical. How to use vbs whole person code: 1. Understand the goal: before writing the code, you need to understand the goal to be corrected; 2. Determine the goal: determine what effect you want to achieve through the script; 3. Write the code: use VBScript to write the script; 4. Test Code: Test it in a safe environment first; 5. Implementation: If there is no problem with the test, it can be implemented.

what file is vbs what file is vbs Feb 19, 2021 pm 02:43 PM

VBS is a scripting language based on Visual Basic. The full name of VBS is "Microsoft Visual Basic Script Edition"; because VBS is relatively simple and feasible, you must ensure the security of VBS and the credibility of the channel before opening a VBS file.

Microsoft is testing the 24H2 version service pipeline in the Windows 11 Dev channel. After turning on VBS, you will receive the 26080.1400 update Microsoft is testing the 24H2 version service pipeline in the Windows 11 Dev channel. After turning on VBS, you will receive the 26080.1400 update Mar 19, 2024 pm 01:25 PM

Microsoft pushed two service updates on DevChannel today to test the Win1124H2 version of the service pipeline. These updates do not contain any new content and are provided solely to evaluate system functionality and stability. According to official instructions, most users in the Dev channel will receive cumulative update 26080.1300 (KB5037139), but if the virtualization-based security (VBS) feature is enabled, they will receive Build 26080.1400 (KB5037140). Microsoft emphasizes that Arm64 devices will only receive the KB5037139 update, even if they have VBS enabled. IT Home Note: Virtualization-based security (VBS) uses hardware virtualization and Windo

How to close win11vbs How to close win11vbs Jan 07, 2024 am 09:10 AM

Win11 will enable a vbs system protection function by default. According to tests, turning off this function can improve system performance by nearly 30%, so it is very suitable for gamers to turn it off. So how to turn off win11 vbs. How to close win11vbs 1. First, we click on the "Start Menu" at the bottom. 2. Then search for "Windows PowerShell" at the top and click "Run as Administrator". 3. After opening, enter "bcdedit/sethypervisorlaunchtypeoff" and press Enter to run. 4. After the operation is completed, restart the computer to close vbs.

See all articles