californiamountainsnake/longmantelegrambot-laravel-api-auth-system

此包已被废弃且不再维护。未建议替代包。

这是一个为longman/telegram-bot库设计的laravel api认证系统。

1.3.0 2020-01-14 12:48 UTC

This package is auto-updated.

Last update: 2022-05-14 18:50:48 UTC


README

这是为longman/telegram-bot库设计的laravel api认证系统。重要!此库使用californiamountainsnake/simple-laravel-auth-system库,您必须先安装并配置。

安装

使用Composer安装此包

通过Composer安装此包。编辑您的项目composer.json文件,要求californiamountainsnake/longmantelegrambot-laravel-api-auth-system

{
    "name": "yourproject/yourproject",
    "type": "project",
    "require": {
        "php": "^7.2",
        "californiamountainsnake/longmantelegrambot-laravel-api-auth-system": "*"
    }
}

并运行composer update

或者

在命令行中运行此命令

composer require californiamountainsnake/longmantelegrambot-laravel-api-auth-system

使用方法

  1. 扩展抽象类AuthTelegrambotUserEntityAuthTelegrambotUserRepository
  2. AuthBotAccessUtilsAuthApiUtilsAuthUserUtils特质包含到您的基类Command中,并实现抽象方法。在phpdoc中为方法getUserEntity()getUserRole()getUserAccountType()设置正确的返回类型提示。
<?php
class BaseCommand extends Command {
    use AuthBotAccessUtils;
    use AuthApiUtils;
    use AuthUserUtils;
    
    /**
     * Get user's entity. Just specify return type hint.
     * @return UserEntity|null
     */
    protected function getUserEntity(): ?AuthUserEntity
    {
        return $this->userUtilsGetUserEntity();
    }

    /**
     * Get user's role. Just specify return type hint.
     * @return UserRoleEnum
     */
    protected function getUserRole(): AuthUserRoleEnum
    {
        return $this->userUtilsGetUserRole();
    }

    /**
     * Get user's account type. Just specify return type hint.
     * @return UserAccountTypeEnum
     */
    protected function getUserAccountType(): AuthUserAccountTypeEnum
    {
        return $this->userUtilsGetUserAccountType();
    }
}
  1. preExecute()方法中调用$this->initTelegramParams()$this->reInitUserParams(),然后调用$this->assertUserHasAccessToMainRoute()并处理异常
<?php
class BaseCommand extends Command {
    use AuthBotAccessUtils;
    use AuthApiUtils;
    use AuthUserUtils;
    
    public function preExecute(): ServerResponse
    {
        $this->initTelegramParams();
        $this->reInitUserParams();

        try {
            // If user try to execute a wrong command, the exception will throw.
            $this->assertUserHasAccessToMainRoute($this->getUserEntity());
            
            return parent::preExecute();
        } catch (ApiProxyException $e) {
            // handle the error!
        } catch (UserRoleNotEqualsException $e) {
            // handle the error!
        } catch (UserAccountTypeNotEqualsException $e) {
            // handle the error!
        } catch (\Throwable $t) {
            // A good idea is to handle other exceptions.
        }
    }
}
  1. 使用AuthApiUtils的方法执行对laravel api的查询
<?php
class MyCommand extends BaseCommand {
    
    public function execute (): ServerResponse {
        $json = $this->callApiNotAuth($this->getMainRoute(), [
            'email' => 'new@email.com',
        ])->getContent();
    }
}
  1. 如果您愿意,可以实现ApiProxyInterface并按您的喜好处理api查询。