Looking to hire Laravel developers? Try LaraJobs

longmantelegrambot-laravel-api-auth-system maintained by californiamountainsnake

Description
This is the laravel api auth system intended for the longman/telegram-bot
Last update
2020/01/14 13:48 (dev-master)
License
Links
Downloads
61

Comments
comments powered by Disqus

longmantelegrambot-laravel-auth-system

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:

  1. Extend the abstract classes AuthTelegrambotUserEntity and AuthTelegrambotUserRepository.
  2. Include the AuthBotAccessUtils, AuthApiUtils and AuthUserUtils traits into your base Command class and realise the abstract methods. Set the correct return type hints in the phpdoc for the methods getUserEntity(), getUserRole() and 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. Call $this->initTelegramParams(), $this->reInitUserParams() and then $this->assertUserHasAccessToMainRoute() in the preExecute() 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.
        }
    }
}
  1. 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();
    }
}
  1. If you want, you can realise the ApiProxyInterface and process api queries as you prefer.