PHP code example of liliuwei / thinkphp-auth

1. Go to this page and download the library: Download liliuwei/thinkphp-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liliuwei / thinkphp-auth example snippets


//简单实例
namespace app\admin\controller;
use think\Controller;

class Base extends Controller
{
    public function initialize()
    {
        if (!session('admin_id')) {
           $this->redirect('login/index');
        }
        $auth = new \liliuwei\think\Auth();
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $url = $controller . "/" . $action;
        if (!$auth->check($url, session('admin_id'))) {
           $this->error('抱歉,您没有操作权限');
        }
    }

//高级实例  根据用户积分判断权限

//Auth类还可以按用户属性进行判断权限, 比如 按照用户积分进行判断,假设我们的用户表 (tp_admin) 有字段 score 记录了用户积分。我在规则表添加规则时,定义规则表的condition 字段,condition字段是规则条件,默认为空 表示没有附加条件,用户组中只有规则 就通过认证。如果定义了 condition字段,用户组中有规则不一定能通过认证,程序还会判断是否满足附加条件。 比如我们添加几条规则:

//name字段:grade1 condition字段:{score}<100 
//name字段:grade2 condition字段:{score}>100 and {score}<200
//name字段:grade3 condition字段:{score}>200 and {score}<300

//这里 {score} 表示 think_members 表 中字段 score 的值。

//那么这时候

$auth = new \liliuwei\think\Auth();
$auth->check('grade1', 1); //是判断用户积分是不是0-100
$auth->check('grade2', 1); //判断用户积分是不是在100-200
$auth->check('grade3', 1); //判断用户积分是不是在200-300

~~~


~~~php


namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class Base extends \app\BaseController
{
    public function initialize()
    {
        if (!session('admin_id')) {
            $this->redirect('/admin/login/index');
        }
        $auth = new \liliuwei\think\Auth();
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $url = $controller . "/" . $action;
        $list = Db::name('auth_rule')->order('sort','asc')->select()->toArray();
        $data = $this->buildMenu($list);
        $no_check_default = ['index/index','login/logout'];
        $no_check_status_list = Db::name('auth_rule')->where('status', 0)->column('name');
        $no_check_rules_list = explode(',', strtolower(implode(',', array_merge($no_check_default, (array)$no_check_status_list))));
        $no_check_user_list = Db::name('admin')->where('is_admin', 1)->column('id');
        if (!in_array(session('admin_id'), $no_check_user_list)) {
            if (!in_array($url, $no_check_rules_list)) {
                if (!$auth->check($url, session('admin_id'))) {
                    $this->error('抱歉,您没有操作权限');
                }
            }
            foreach ($data as $k => $v) {
                if (!$auth->check($v['name'], session('admin_id'))) {
                    unset($data[$k]);
                } else {
                    if (isset($v['_child'])) {
                        foreach ($v['_child'] as $key => $value) {
                            if (!$auth->check($value['name'], session('admin_id'))) {
                                unset($data[$k]['_child'][$key]);
                            }
                        }
                    }
                }
            }
        }
        $menu_nav = [];
        foreach ($data as $k => $v) {
            if (!empty($v['is_menu']) && !empty($v['menu_type']) && $v['menu_type'] != 3) {
                $item = $v;
                if (isset($item['_child'])) {
                    $children = [];
                    foreach ($item['_child'] as $child) {
                        if (!empty($child['menu_type']) && $child['menu_type'] != 3) {
                            $children[] = $child;
                        }
                    }
                    $item['_child'] = $children;
                }
                $menu_nav[] = $item;
            }
        }
        $rule = Db::name('auth_rule')->where('name', '=', $url)->field('id,pid')->find();
        $active_ids = [];
        if ($rule) {
            $active_ids[] = $rule['id'];
            $pid = $rule['pid'];
            while ($pid > 0) {
                $active_ids[] = $pid;
                $parent = Db::name('auth_rule')->where('id', '=', $pid)->field('pid')->find();
                $pid = $parent ? $parent['pid'] : 0;
            }
        }
        View::assign([
            'active_id' => implode(',', $active_ids),
            'menu_nav' => $menu_nav,
            'crumb_list' => get_crumb_list($url)
        ]);
    }

    /**
     * 构建后台侧边栏菜单树
     *
     * 查询所有标记为菜单的权限项,
     * 根据当前用户的权限过滤可见菜单,按 parent_id 组装为树形结构。
     *
     * 返回结构:
     * [
     *   ['id', 'name', 'title', 'icon', 'sort', 'menu_type', 'parent_id', 'children' => [...], 'patterns' => [...]],
     *   ...
     * ]
     *
     * @return array 菜单树数组
     */
    protected function buildMenu($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        $tree = [];
        if (is_array($list)) {
            $refer = [];
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }