search
HomeBackend DevelopmentPHP TutorialImplement a simple ACL_PHP tutorial
Implement a simple ACL_PHP tutorialJul 13, 2016 pm 05:44 PM
accessaclphpcodeFunctiondefinitionaccomplishcontrolPermissionsofSimplesurfaceresource

 

Php代码 

/**

* Simple ACL permission control function

*

* Table definition

*

* 1. Resource definition (rsid,access,desc)

* 2. Role definition (id,rolename,desc)

* 3. Resource-role association (rsid, role_id)

* 4. User-role association (user_id, role_id)

*

* Depends on db.php sqlobject.php

*

* @author vb2005xu.iteye.com

​*/ 

class AclBase { 

 

    /**

* No one is allowed to access

​​*/ 

    const NOBODY = 0; 

     

    /**

* Allow anyone to access

​​*/ 

    const EVERYONE = 1; 

     

    /**

* * Allow users with roles to access

​​*/ 

    const HAS_ROLE = 2; 

     

    /**

* Allow users without roles to access

​​*/ 

    const NO_ROLE = 3; 

 

    /**

* * Only roles defined in the resource-role association can access

​​*/ 

    const ALLOCATE_ROLES = 4; 

     

    // 定义相关的 表名 

    public $tbResources = 'aclresources'; 

    public $tbRoles = 'aclroles'; 

    public $tbRefResourcesRoles = 'aclresources_aclroles'; 

    public $tbRefUsersRoles = 'users_aclroles'; 

     

    /**

* Format resource access permissions and return

* *

* @return int

​​*/ 

    static function formatAccessValue($access){ 

        static $arr = array(self::NOBODY,self::EVERYONE,self::HAS_ROLE,self::NO_ROLE,self::ALLOCATE_ROLES); 

        return in_array($access,$arr) ? $access : self::NOBODY; 

    } 

     

    /**

* Create a resource and return the resource record primary key

* *

* @param string $rsid

* @param int $access

* @param string $desc

* *

* @return int

​​*/ 

    function createResource($rsid,$access,$desc){ 

        if (emptyempty($rsid)) return false; 

         

        $resource = array( 

            'rsid' => $rsid, 

            'access' => self::formatAccessValue($access), 

            'desc' => $desc, 

            'created_at' => CURRENT_TIMESTAMP 

        ); 

         

        return SingleTableCRUD::insert($this->tbResources,$resource); 

    } 

     

    /**

* Modify resources and return success status

* *

* @param array $resource

* @return int

​​*/ 

    function updateResource(array $resource){        

        if (!isset($resource['rsid'])) return false; 

         

        $resource['updated_at'] = CURRENT_TIMESTAMP; 

         

        return SingleTableCRUD::update($this->tbResources,$resource,'rsid'); 

    } 

     

    /**

* Delete resources

* *

* @param string $rsid

* @return int

​​*/ 

    function deleteResource($rsid){ 

        if (emptyempty($rsid)) return false; 

        return SingleTableCRUD::delete($this->tbResources,array('rsid'=>$rsid)); 

    } 

     

    /**

* Create a role and return the primary key of the role record

* *

* @param string $rolename

* @param string $desc

* *

* @return int

​​*/ 

    function createRole($rolename,$desc){ 

        if (emptyempty($rolename)) return false; 

         

        $role = array( 

            'rolename' => $rolename, 

            'desc' => $desc, 

            'created_at' => CURRENT_TIMESTAMP 

        ); 

         

        return SingleTableCRUD::insert($this->tbRoles,$role); 

    } 

     

    /**

* Modify the role and return the success status

* *

* @param array $role

* @return int

​​*/ 

    function updateRole(array $role){        

        if (!isset($role['id'])) return false; 

         

        if (isset($role['rolename'])) unset($role['rolename']); 

        $role['updated_at'] = CURRENT_TIMESTAMP; 

         

        return SingleTableCRUD::update($this->tbRoles,$role,'id'); 

    } 

     

    /**

* Delete character

* *

* @param int $role_id

* @return int

​​*/ 

    function deleteRole($role_id){ 

        if (emptyempty($role_id)) return false; 

        return SingleTableCRUD::delete($this->tbRoles,array('role_id'=>(int) $role_id)); 

    } 

     

    /**

* Assign roles to resources, and remove all relevant records from the table before inserting them

* *

* @param int $rsid

* @param mixed $roleIds

* @param boolean $setNull Whether to clear resources from the association table when the role id does not exist

​​*/ 

    function allocateRolesForResource($rsid,$roleIds,$setNull=false,$defaultAccess=-1){ 

        if (emptyempty($rsid)) return false; 

         

        $roleIds = normalize($roleIds,','); 

        if (emptyempty($roleIds)){ 

            if ($setNull){ 

                SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid)); 

                 

                if ($defaultAccess != -1){ 

                    $defaultAccess = self::formatAccessValue($defaultAccess); 

                    $this->updateResource(array('rsid'=>$rsid,'access'=>$defaultAccess)); 

                } 

                return true;  

            } 

            return false; 

        } 

         

        SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid)); 

         

        $roleIds = array_unique($roleIds); 

         

        foreach ($roleIds as $role_id){ 

            SingleTableCRUD::insert($this->tbRefResourcesRoles,array('rsid'=>$rsid,'role_id'=>(int)$role_id)); 

        } 

        return true; 

    } 

     

    function cleanRolesForResource($rsid){ 

        if (emptyempty($rsid)) return false; 

        return SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid)); 

    } 

     

    function cleanResourcesForRole($role_id){ 

        if (emptyempty($role_id)) return false; 

        return SingleTableCRUD::delete($this->tbRefResourcesRoles,array('role_id'=>(int) $role_id)); 

    } 

     

    /**

* * To allocate resources to roles, remove all relevant records from the table before inserting them

* *

* @param int $role_id

* @param mixed $rsids

* *

* @return boolean

​​*/ 

    function allocateResourcesForRole($role_id,$rsids){ 

        if (emptyempty($role_id)) return false; 

         

        $role_id = (int) $role_id; 

        $rsids = normalize($rsids,','); 

        if (emptyempty($rsids)){ 

            return false;        

        } 

         

        SingleTableCRUD::delete($this->tbRefResourcesRoles,array('role_id'=>$role_id)); 

         

        $rsids = array_unique($rsids); 

         

        foreach ($rsids as $rsid){ 

            SingleTableCRUD::insert($this->tbRefResourcesRoles,array('rsid'=>$rsid,'role_id'=>$role_id)); 

        } 

        return true; 

    } 

     

    /**

* Assign roles to users, and remove all relevant records from the table before inserting them

* *

* * There may be performance issues here when there are many users... I will think about how to optimize it later

* *

* @param int $user_id

* @param mixed $roleIds

* *

* @return boolean

​​*/ 

    function allocateRolesForUser($user_id,$roleIds){ 

        if (emptyempty($user_id)) return false; 

         

        $user_id = (int) $user_id; 

        $rsids = normalize($rsids,','); 

        if (emptyempty($rsids)){ 

            return false;        

        } 

         

        SingleTableCRUD::delete($this->tbRefUsersRoles,array('user_id'=>$user_id)); 

         

        $roleIds = array_unique($roleIds); 

         

        foreach ($roleIds as $roleId){ 

            SingleTableCRUD::insert($this->tbRefUsersRoles,array('user_id'=>$user_id,'role_id'=>$role_id)); 

        } 

        return true; 

    } 

         

    function cleanRolesForUser($user_id){ 

        if (emptyempty($user_id)) return false; 

        return SingleTableCRUD::delete($this->tbRefUsersRoles,array('user_id'=>(int) $user_id)); 

    } 

     

    function cleanUsersForRole($role_id){ 

        if (emptyempty($role_id)) return false; 

        return SingleTableCRUD::delete($this->tbRefUsersRoles,array('role_id'=>(int) $role_id)); 

    } 

     

 

/**

* Perform acl verification on resources

*

* @param string $rsid resource identifier

* @param array $user Specific user, if not specified, the current user will be verified

*

* @return boolean

​*/ 

function aclVerity($rsid,array $user = null){ 

    if (emptyempty($rsid)) return false; 

     

     

     

     

 

 

Java代码 

/*

     * 校验步骤如下:

     * 

     * 1. 先校验 资源本身access 属性

     *    EVERYONE => true,NOBODY  => false * 其它的属性在下面继续校验

     * 2. 从session(或者 用户session表)中获取角色id集合

* 3. If the user has a role, then HAS_ROLE => true, NO_ROLE => false; vice versa

* 4. If resource access == ALLOCATE_ROLES

* 1. Obtain the role id set corresponding to the resource from the cache (or $tbRefResourcesRoles)

* 2. Find the intersection of the role id set owned by the user and the role id set corresponding to the resource

* 3. Intersection exists=> true; otherwise=> false

*/

It took me half an hour and I’m so dizzy that I’ll find time to perfect it tomorrow....

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478723.htmlTechArticlePhp code?php /** * Simple ACL permission control function * * Table definition * * 1. Resource definition (rsid, access, desc) * 2. Role definition (id, rolename, desc) * 3. Resource-role association (rsid, role_id...
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中实现SEO优化如何在PHP中实现SEO优化May 20, 2023 pm 01:30 PM

随着互联网的发展,SEO(SearchEngineOptimization,搜索引擎优化)已经成为了网站优化的重要一环。如果您想要使您的PHP网站在搜索引擎中获得更高的排名,就需要对SEO的内容有一定的了解了。本文将会介绍如何在PHP中实现SEO优化,内容包括网站结构优化、网页内容优化、外部链接优化,以及其他相关的优化技巧。一、网站结构优化网站结构对于S

如何在PHP中实现ERP系统如何在PHP中实现ERP系统May 20, 2023 pm 06:21 PM

随着电子商务和企业管理的发展,许多企业开始寻找更好的方法来处理其日常业务流程。ERP系统是一种能够整合企业各种业务流程的软件工具。它提供了全面的功能,包括生产、销售、采购、库存、财务等方面,帮助企业提高效率、控制成本和提高客户满意度。而在PHP编程语言中,也能够实现ERP系统,这就需要我们掌握一些基本的知识和技术。下面,我们将深入探讨如何在PHP中实现ERP

在PHP中如何实现物联网开发?在PHP中如何实现物联网开发?May 12, 2023 am 11:51 AM

随着物联网技术的发展和普及,越来越多的应用场景需要使用PHP语言进行物联网开发。PHP作为一种广泛应用于Web开发的脚本语言,它的易学易用、开发速度快、可扩展性强等特点,使其成为开发物联网应用的一种优秀选择。本文将介绍在PHP中实现物联网开发的常用技术和方法。一、传输协议和数据格式物联网设备通常使用TCP/IP或UDP协议进行数据传输,而HTTP协议是一个优

如何在PHP中实现CRM系统如何在PHP中实现CRM系统May 20, 2023 pm 12:31 PM

随着企业的发展,客户管理变得越来越重要。为了提高客户满意度和忠诚度,越来越多的企业采用客户关系管理系统(CRM)来帮助其管理客户关系。而PHP是一种流行的编程语言,因其简单易学、灵活和强大而被广泛应用于Web开发。那么,如何在PHP中实现CRM系统呢?本文将为您介绍实现CRM系统的步骤和技巧。Step1:需求分析在开始开发CRM系统之前,您需要进行需求分析

如何在PHP中实现轮播图如何在PHP中实现轮播图May 22, 2023 am 08:25 AM

随着互联网的发展,轮播图已经成为了网页设计中必不可少的一部分。在很多网页中,轮播图经常被用作展示企业文化、最新产品或是推广活动等场景。本篇文章将会分享如何使用PHP来实现轮播图的功能。一、轮播图的概念轮播图是网页中一种常见的视觉效果,一般由多个图片组成,在页面中自动或手动进行切换,展示多个内容。可以添加符合业务要求的动画效果,有助于引起用户的关注和提高网站的

在PHP中如何实现智能合约?在PHP中如何实现智能合约?May 12, 2023 am 08:09 AM

智能合约(SmartContract)是一种基于区块链的自动化交易程序,可以实现自动化执行、验证和执行交易。智能合约可以减少交易中的人为干扰,提高交易的安全性和效率。在不同的区块链中,智能合约的实现方式略有不同。本文将介绍在PHP中如何实现智能合约。PHP是一种广泛使用的编程语言,特别适合Web开发。PHP有着成熟的开源生态系统,以及许多可靠的框架和库。在

如何在PHP中实现验证码如何在PHP中实现验证码May 20, 2023 am 11:31 AM

随着互联网的不断发展,越来越多的网站需要使用验证码来保证安全性。验证码是一种借助人类能力而无法被计算机破解的认证技术,广泛应用于网站注册、登录、找回密码等功能中。下面将介绍如何使用PHP实现验证码功能。一、生成验证码图片验证码图片的生成是验证码功能的核心,需要生成一个随机字符,并将其渲染为图像展示给用户。在PHP中,可以使用GD库来生成图片。GD库是一种用于

在PHP中如何实现OA开发?在PHP中如何实现OA开发?May 12, 2023 am 08:36 AM

随着现代企业管理的需求与时俱进,各种管理软件如ERP、CRM、HRM和OA等软件的使用已经变得越来越普遍。特别是办公自动化(OA)软件,已经成为企业必不可少的一部分。随着PHP发展的越来越成熟,越来越多的企业开始使用PHP作为开发OA软件的工具,并取得了很好的效果。那么,在PHP中如何实现OA开发呢?确定OA的需求在开始OA的开发之前,必须先明确自己的OA需

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

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version