PHP code example of the-real-start / yii2-oauth2-server-tools

1. Go to this page and download the library: Download the-real-start/yii2-oauth2-server-tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

the-real-start / yii2-oauth2-server-tools example snippets


 
 
 namespace common\components;
 
 use common\components\enums\Scope;
 use TRS\yii2\oauth2server\tools\oauth2\AppIdentity as BaseAppIdentity
 
 class AppIdentity extends BaseAppIdentity
 {
     /**
      * @inheritdoc
      */
     abstract public function isPublicClient($client_id){
        $app = self::findByClientId($client_id);
        
        return !!$app && $app->scope == Scope::_PUBLIC;
     }
 } 
 



namespace common\components;

use common\enums\Scope;
use TRS\yii2\oauth2server\tools\oauth2\User as BaseUser;

class User extends BaseUser
{
    /**
     * @ingeritdoc
     */
    public function getIsPublic()
    {
        /** @var \common\models\User $identity */
        $identity = $this->getIdentity(false);
        
        return ( $identity->scope == Scope::_PUBLIC );
    }
}


 public function accessRules()
    {
        return [
            [
                'allow'   => true,
                'roles'   => [ '@' ],
                'actions' => [ 'registration', 'send-recovery-email', 'reset-password', 'check-reset-token' ],
                'scopes'  => [ Scope::_PUBLIC ],
            ],
        ];
    }

public function behaviors()
    {
     $behaviors = parent::behaviors();
     ...
     $behaviors = ArrayHelper::merge(
            $behaviors,
            [
                ...
                'access'            => [
                                         'class'      => AccessControl::className(),
                                         'rules'      => $this->accessRules(),
                                         'ruleConfig' => ['class' => AccessRule::class],
                ],
                ...
            ]
        );

        return $behaviors;
    }

/** @var array */
$errors = $model->getErrors();

throw new JsonHttpException(400, $errors);