PHP code example of bud / auth

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

    

bud / auth example snippets



declare(strict_types=1);

use Bud\Auth\Manager\EncryptAdapters as Encrypter;
use function Hyperf\Support\env;

return [
    'scenes' => [
        'admin' => [
            // redis连接池
            'redis_pool' => 'default',
            // token秘钥,根据加密类的实现该配置可以为数组。默认仅实现了对称加密,所以默认为字符串
            'secret' => env('TOKEN_SECRET', 'dgfjkdhsjghkdfjhshgjsssjh;dhf'),
            // 请求头token使用的字段
            'header_name' => env('TOKEN_HEADER_NAME', 'Authorization'),
            // token 生命周期,单位秒,默认一天
            'ttl' => (int)env('TOKEN_TTL', 60 * 60 * 24),
            // 允许过期多久以内的 token 自动刷新,单位秒,默认一周
            'refresh_ttl' => (int)env('TOKEN_REFRESH_TTL', 60 * 60 * 24 * 7),
            // 是否自动刷新,仅在使用Auth注解下生效
            'auto_refresh' => true,
            // 默认使用的加密类,alg算法标识|完整类名
            'encrypter' => Encrypter\PasswordHashEncrypter::class,
            // 可选加密类。加密类必须实现 \Bud\Auth\Manager\Encrypter 接口
            'drivers' => [
                Encrypter\PasswordHashEncrypter::alg() => Encrypter\PasswordHashEncrypter::class,
                Encrypter\CryptEncrypter::alg() => Encrypter\CryptEncrypter::class,
                Encrypter\SHA1Encrypter::alg() => Encrypter\SHA1Encrypter::class,
                Encrypter\Md5Encrypter::alg() => Encrypter\Md5Encrypter::class,
            ],
        ],
    ]
];


declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Bud\Auth\Annotation\Auth;
use Bud\Auth\Annotation\Permission;
use Bud\Auth\Annotation\Roles;
use Bud\Auth\AuthInterface;

#[Controller]
class IndexController extends AbstractController
{
    #[Inject]
    protected AuthInterface $auth;

    /**
     * 登录
     * @return array
     */
    #[GetMapping(path:"/login")]
    public function login()
    {
        /** @var User $user */
        $user = User::query()->firstOrCreate(['name' => 'test', 'avatar' => 'avatar']);
        return [
            'token' => $this->auth->scene('admin')->login($user), // 常规登录返回token字符串
            //  'token' => $this->auth->scene('admin')->parseLogin($user,['agent' => $this->request->getHeader('user-agent')[0]])->token(), // 自定义登录返回jwt对象
            //  'token' => auth('admin')->login($user), // 助手函数公共方法登录
        ];
    }

    /**
     * 退出当前登录用户
     */
    #[GetMapping(path:"/logout"),Auth]
    public function logout()
    {
        $this->auth->scene('admin')->logout();
        // auth('admin')->logout();
        return 'logout ok';
    }

    /**
     * 使用 Permission 注解确保当前登录用户必需拥有user或者user:list权限才能获取列表
     * @return string
     */
    #[GetMapping(path:"/user"),Permission(codes: "user,user:list", where: "OR")]
    public function list(int $id)
    {
        return User::query()->get();
    }

    /**
     * 使用 Permission 注解确保当前登录用户必需拥有 user:add 权限
     * @return string
     */
    #[PostMapping(path:"/user"),Permission(codes: "user:add", title: "添加用户")]
    public function add()
    {
        return User::create(['name' => 'test', 'avatar' => 'avatar']);
    }

    /**
     * 使用 Auth 注解可以保证该方法必须是已登录用户才能访问
     * @return string
     */
    #[GetMapping(path:"/info/{id}"),Auth]
    public function info(int $id)
    {
        return User::find($id);
    }

    /**
     * 使用 Roles 注解确保必须是超级管理员才能删除用户
     * @return string
     */
    #[DeleteMapping(path:"/user"),Roles("superAdmin")]
    public function delete()
    {
        return User::create(['name' => 'test', 'avatar' => 'avatar']);
    }
}
bash
php bin/hyperf.php vendor:publish bud/auth