californiamountainsnake / longmantelegrambot-laravel-api-auth-system
This package is abandoned and no longer maintained.
No replacement package was suggested.
This is the laravel api auth system intended for the longman/telegram-bot
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
This is the laravel api auth system intended for the longman/telegram-bot library. Important! This library uses the californiamountainsnake/simple-laravel-auth-system library, you must install and config one first.
Install:
Require this package with Composer
Install this package through Composer.
Edit your project's composer.json
file to require californiamountainsnake/longmantelegrambot-laravel-api-auth-system
:
{ "name": "yourproject/yourproject", "type": "project", "require": { "php": "^7.2", "californiamountainsnake/longmantelegrambot-laravel-api-auth-system": "*" } }
and run composer update
or
run this command in your command line:
composer require californiamountainsnake/longmantelegrambot-laravel-api-auth-system
Usage:
- Extend the abstract classes
AuthTelegrambotUserEntity
andAuthTelegrambotUserRepository
. - Include the
AuthBotAccessUtils
,AuthApiUtils
andAuthUserUtils
traits into your base Command class and realise the abstract methods. Set the correct return type hints in the phpdoc for the methodsgetUserEntity()
,getUserRole()
andgetUserAccountType()
.
<?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(); } }
- Call
$this->initTelegramParams()
,$this->reInitUserParams()
and then$this->assertUserHasAccessToMainRoute()
in thepreExecute()
method and handle the exceptions:
<?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. } } }
- Use AuthApiUtils's methods to execute queries to your laravel api:
<?php class MyCommand extends BaseCommand { public function execute (): ServerResponse { $json = $this->callApiNotAuth($this->getMainRoute(), [ 'email' => 'new@email.com', ])->getContent(); } }
- If you want, you can realise the ApiProxyInterface and process api queries as you prefer.