celebron/yii2-oauth2-social

安装: 37

依赖项: 0

建议者: 0

安全: 0

类型:yii2-extension

2.0.2 2023-08-19 10:26 UTC

This package is auto-updated.

Last update: 2024-09-29 20:41:27 UTC


README

Celebron Social Extension for Yii 2


此扩展为Yii 2.0框架提供HTTP客户端。

安装

composer require celebron/yii2-oauth2-social

配置

文件 frontend/config/main.php,示例

    ...,
    'bootstrap' => [..., 'social' ],
    'components'=>[
        'user' => [
            'identityClass' => \common\models\User::class,
            ...
        ],
        'social' => [
            'class' => Celebron\socialSource\Configuration::class,
            'socials' => [
                 [
                     'class' => Yandex::class, //Google::class и т.д.
                     'active' => true,
                     'clientId' => '...',
                     'clientSecret' => '...,
                ],
                ...  
            ],  
        ],
    ],
    ...

必须

  • bootstrap 中连接组件 Configuration::class,如示例所示;
  • 在组件 social 中设置变量 $socials,列出所有社交网络,按照 Yii::createObject() 规则;
  • 实现接口 SocialUserInterfaceIdentityInterface 并连接到组件 user
    • (如有必要)连接到 UserManagementTrait

Configuration::class

  [optional] string       $route ('social')   - роут для OAuth redirect path
  [optional] string|null  $paramsGroup (null) - ключ массива с настройками в \Yii::$app->params (null - не использовать)
  [optional] array        $socialEvents       - массив событий ['название-события' => \Closure]
  [required] Social[]     $socials            - список всех соц. сетей (Правило формирования \Yii::createObject())
  [optional] \Closure|null $paramsHandler      - настройка $params в Social классах
  • $socials 数组中,键可以省略,那么在注册时,键将是类的名称。
  • 如果类实现了接口 CustomRequestInterface,则键是必需的(会抛出异常)
  • 如果设置了 $paramsGroup,则可以从 \Yii::$app->params[$paramsGroup][{socialName}] 中获取 clientIdclientSecret 配置
  • 如果设置了 $paramsHandler,则可以从 callback-function 获取 clientIdclientSecret 配置
    function($socialName):array { /** @var Configure $this */ }
    

认证类

OAuth2::class (Google::class, Yandex::class, ...)

namespace Celebron\socials

[optional] bool     $activate (false)    - активировать механизм
[optional] string   $name                - название для Widget
[optional] string   $icon                - иконка для Widget
[optional] bool     $visible             - отображение для Widget
[required|optional] $clientId (null)     - OAuth clientId
[required|optional] $clientSecret (null) - OAuth clientSecret

如果未定义 $clientId$clientSecret,则将使用参数 $params['clientId']$params['clientSecret'],否则将抛出异常。取决于配置 Configure::$paramsGroupConfigure::$paramsHandler

社交网络控制台中的重定向链接(oauth2等)

https://сайт.ru/{route}/{social}

图例

{social} - название социальной сети (google, yandex и т.п.). Индекс массива Сonfigutation::$socials.
{route}  - Настройка в классе Configuration