californiamountainsnake / longmantelegrambot-laravel-api-auth-system
此包已被废弃且不再维护。未建议替代包。
这是一个为longman/telegram-bot库设计的laravel api认证系统。
1.3.0
2020-01-14 12:48 UTC
Requires
- php: ^7.2
- californiamountainsnake/json-response: ~1.0.1
- californiamountainsnake/longmantelegrambot-utils: ~1.1.3
- californiamountainsnake/php-utils: ~1.0.1
- californiamountainsnake/simple-laravel-auth-system: ~3.0
- longman/telegram-bot: ~0.55
- myclabs/php-enum: ~1.5
Requires (Dev)
- ext-dom: *
- phpunit/phpunit: ^7.0
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
使用方法
- 扩展抽象类
AuthTelegrambotUserEntity
和AuthTelegrambotUserRepository
。 - 将
AuthBotAccessUtils
、AuthApiUtils
和AuthUserUtils
特质包含到您的基类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(); } }
- 在
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. } } }
- 使用AuthApiUtils的方法执行对laravel api的查询
<?php class MyCommand extends BaseCommand { public function execute (): ServerResponse { $json = $this->callApiNotAuth($this->getMainRoute(), [ 'email' => 'new@email.com', ])->getContent(); } }
- 如果您愿意,可以实现ApiProxyInterface并按您的喜好处理api查询。