sergmoro1/yii2-user

通过电子邮件进行用户注册并确认。用户管理。使用OAuth2凭据登录。预定义基于角色的访问控制。

安装: 131

依赖项: 1

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 0

开放问题: 0

类型:yii2-extension

v1.1.4 2020-03-11 18:09 UTC

This package is auto-updated.

Last update: 2024-09-19 03:12:32 UTC


README

优点

  • 注册,电子邮件确认,重置密码,认证;
  • 社交网络OAuth2认证(可获取头像);
  • 后端用户管理。

安装

安装此扩展的首选方法是通过composer。

运行以下命令:

composer require --prefer-dist sergmoro1/yii2-user

或者

"sergmoro1/yii2-user": "^1.1"

将其添加到您的composer.json文件的require部分。

运行迁移

php yii migrate --migrationPath=@vendor/sergmoro1/yii2-user/src/migrations

配置

将OAuth2和mailler组件的定义添加到配置文件使用的模块中。

return [
    ...
    'modules' => [
        'lookup' => ['class' => 'sergmoro1\lookup\Module'],
        'user'   => ['class' => 'sergmoro1\user\Module'],
    ],
    'components' => [
        'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'yandex' => [
                    'class' => 'yii\authclient\clients\Yandex',
                    'clientId' => 'YandexClientId',
                    'clientSecret' => 'YandexClientSecret',
                ],
            ],
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'viewPath' => '@vendor/sergmoro1/yii2-user/src/mail',
            /* Example of definition (Yandex)
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.yandex.ru',
                'username' => 'admin@your-site.ru',
                'password' => 'your-password',
                'port' => '465',
                'encryption' => 'ssl',
            ],
            */
        ],
    ],

重新声明 getAvatarImage(),例如(如果使用 sergmoro1/yii2-uploader),或者任何其他方式。

    public function getAvatarImage()
    {
        return $this->getImage('thumb');
    }

使用

将OAuth2认证的社交网络账户操作添加到控制器中。

namespace frontend\controllers;

use Yii;
use sergmoro1\user\traits\AuthTrait;

class SiteController extends Controller
{
    use AuthTrait;

    /**
     * Handler for EVENT_AFTER_LOGGED_IN. May be defined if needed.
     */
    public function init()
    {
        parent::init();
        $this->on(\sergmoro1\user\Module::EVENT_AFTER_LOGGED_IN, function($event) { Yii::$app->session->setFlash('success', 
            Yii::t('app', 'You are logged in as a commentator. You can leave a comment now.'));
        });
    }
    
    /**
     * inheritdoc
     */
    public function actions()
    {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'onAuthSuccess'],
            ],
        ];
    }

在视图中某处放置小部件。

<?= sergmoro1\user\widgets\SocialCredentials::widget() ?>

社交图标可以在 Yii::$app->params['icons'] 中定义。它们的使用由 widgets/views/socialCredentials.php 确定。