stiks/yii2-eveonline-sso

EveOnline SSO 的 OAuth2 客户端

dev-master 2017-02-21 12:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:14 UTC


README

此扩展使用标准 yii2-authclient 允许通过 EveOnline 网站进行身份验证。基于 unti1x/yii2-eveonline-sso

依赖项

唯一需要的包是 yiisoft/yii2-authclient 版本 "2.1+"

安装

通过 Composer

在您的 composer.json 文件的 require-部分添加以下行 "stiks/yii2-eveonline-sso": "*",然后更新包

通过控制台

使用以下命令

composer require "stiks/yii2-eveonline-sso" "dev-master"

用法

配置,config/web.php

'components' => [

    # ...

    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'eve-online-sso' => [
                'class' => 'stiks\eveonline_sso\EveOnlineSSO',
                'clientId' => 'Your Client ID',
                'clientSecret' => 'Your Client Secret',
                'scope' => [
                    # your scope here
                    'characterAccountRead'
                ]
            ],
        ],
    ]

    # ...

]

您还可以在 User 模型中添加一些字段,如 character_idcharacter_nameowner_hash。最后一个字段强烈推荐,因为角色可以被转移到另一个账户,CCP 提供了一种方式来使用唯一代码进行检查(见 SSO 手册,“获取角色 ID”部分)。

然后在控制器中注册 auth 动作

    public function actions () {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'successCallback'],
            ],
        ];
    }

并实现 successCallback-方法,该方法应在用户成功授权时被调用。

您可以使用以下类似的内容

    public function successCallback ($client) {
        $attributes = $client->getUserAttributes();

        if (Yii::$app->user->isGuest) {
            # get user by hash
            $user = User::findOne (['owner_hash' => $attributes['CharacterOwnerHash']]);
            if($user) {
                Yii::$app->user->login ($user);

                return $this->->goHome ();
            }

            # new user found
            $user = new User ();
            $user->attributes = [
                  'character_id'   => $attributes['CharacterID'],
                  'character_name' => $attributes['CharacterName'],
                  'owner_hash'     => $attributes['CharacterOwnerHash']
            ];
            if (!$user->save ()) {
                Yii::error (print_r ($user->getErrors (), true));
            }

            Yii::$app->user->login ($user);
        }

        return $this->->goHome ();
    }

视图示例代码

<?php use yii\helpers\Url; ?>

<?php if(Yii::$app->user->isGuest): ?>
    <a href="<?= Url::toRoute('site/auth', ['authclient' => 'eve-online-sso']) ?>">
        <img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30" alt="SSO auth" />
    </a>
<?php else:?>
    <div class="user-avatar">
        <img src="//image.eveonline.com/Character/<?= Yii::$app->user->identity->character_id ?>_128.jpg" alt="avatar" />
    </div>
    <?= Yii::$app->user->identity->character_name ?>
<?php endif; ?>

您也可以使用内置小部件

<?php
use yii\authclient\widgets\AuthChoice
?>

<?php
$authAuthChoice = AuthChoice::begin (['id' => 'auth-choice', 'baseAuthUrl' => ['ccp/auth'], 'popupMode' => true]);
    foreach ($authAuthChoice->getClients () as $client) {
        echo $authAuthChoice->clientLink ($client, Yii::t ('app', 'Sign in with '.$client->getTitle ()), ['class' => 'btn btn-block btn-default']);
    }
AuthChoice::end();
?>

链接

许可证

CreativeCommons 知识共享署名-相同方式共享 4.0(友好法律