PHP code example of hulang / think-auth

1. Go to this page and download the library: Download hulang/think-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/ */

    

hulang / think-auth example snippets


// auth配置
'auth' => [
    'auth_on' => 1, // 权限开关
    'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证
    'auth_user' => 'admin', // 用户信息不带前缀表名
],

// 引入类库
use think\Auth;

// 检测权限
if(Auth::check('show_button', 1)){// 第一个参数是规则名称,第二个参数是用户UID
    //有显示操作按钮的权限
}else{
    //没有显示操作按钮的权限
}

// 或通过全局函数进行判断
if(AuthCheck('show_button', 1)){// 第一个参数是规则名称,第二个参数是用户UID
    //有显示操作按钮的权限
}else{
    //没有显示操作按钮的权限
}

php think make:middleware Auth



declare(strict_types=1);

namespace app\http\middleware;

use think\exception\HttpResponseException;
use think\Auth as AuthHandle;
use traits\controller\Jump;

class Auth
{
    use Jump;

    /**
     * 授权业务处理
     * @param $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // 白名单
        $allow = ['user/login'];

        $rule = strtolower("{$request->controller()}/{$request->action()}");

        // 初始化 user_id
        $user_id = is_login();

        // 权限检查
        $check = AuthHandle::check($rule, $user_id);
        if (false === $check) {
            $this->error('[403] 未授权访问');
        }

        return $next($request);
    }
}


$auth->check('rule1,rule2', uid);

$auth->check('rule1,rule2', uid, 'and');