定制 eluhr/yii2-user-access-token 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eluhr/yii2-user-access-token

最新稳定版本:2.0.1

Composer 安装命令:

composer require eluhr/yii2-user-access-token

包简介

User access token Yii2

README 文档

README

This extension provides a simple way to authenticate users via a token. It is easily extendable and can be used with any user model.

Installation

composer require eluhr/yii2-user-access-token

Configuration

In your console part of your config file add this:

<?php
return [
    'controllerMap' => [
        'migrate' => [
            'migrationPath' => [
                '@vendor/eluhr/yii2-user-access-token/src/migrations'
            ]
        ]
    ]
]

Example usage

In your user model you can do this:

<?php

namespace app\models;

use eluhr\userAccessToken\models\Token;
use yii\base\NotSupportedException;
use yii\filters\auth\QueryParamAuth;
use yii\web\IdentityInterface;

class User extends yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * @param $token
     * @param string $type
     *
     * @throws NotSupportedException
     * @return IdentityInterface|null
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        if ($type === QueryParamAuth::class) {
            $token = Token::findValidToken($token); // Change this to your token model if needed
            if ($token instanceof Token) {
                return $token->user;
            }
            return null;
        }

        return parent::findIdentityByAccessToken($token, $type);
    }
}

In your controller:

<?php

namespace app\controllers;

use yii\filters\AccessControl;
use yii\filters\auth\QueryParamAuth;
use yii\web\Controller;

class ExampleController extends Controller
{

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => QueryParamAuth::class
        ];
        $behaviors['access'] = [
            'class' => AccessControl::class,
            'rules' => [
                [
                    'actions' => ['index'],
                    'allow' => true,
                    'roles' => ['@']
                ]
            ]
        ];
        return $behaviors;
    }

    public function actionIndex()
    {
        return 'Hello World';
    }

}

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-01-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固