用虎/yii2-user

安装: 84

依赖项: 1

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

公开问题: 0

类型:yii2-extension


README

最基础的 user 模块。

Latest Stable Version Total Downloads Latest Unstable Version License

特性

  • User 模块直接从 Yii2 高级模板获取
  • 使用 i18n 进行国际化
  • 在前后端都使用 User 模块
  • mailer 布局和视图在 user 模块中
  • 在注册、重置密码等过程中使用 repassword
  • 在注册、登录、请求密码重置令牌、重置密码和后端登录中使用 captcha
  • 在注册、登录、请求密码重置令牌中使用 AJAX 验证
  • 通过电子邮件激活
  • OAuth 登录(如果没有用户,尝试注册)
  • 自动更新 OAuth 信息
  • User 模块的配置
  • 使用用户名或电子邮件登录
  • 密码和电子邮件验证
  • 用户账户
  • 用户账户 OAuth
  • 令牌发送者和处理器
  • 更改表单(ChangeUsernameForm, ChangeEmailForm, ChangePasswordForm)

依赖项

安装

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

运行以下命令:

php composer.phar require --prefer-dist yongtiger/yii2-user "*"

或添加以下内容到你的 composer.json 的 require 部分:

"yongtiger/yii2-user": "*"

配置

\common\config\main.php

'components' => [
	'mailer' => [
	    'class' => 'yii\swiftmailer\Mailer',

	    'viewPath' => '@common/mail',

	    ///[Yii2 uesr:mail]
	    'htmlLayout' =>'@yongtiger/user/mail/layouts/html',
	    'textLayout' =>'@yongtiger/user/mail/layouts/text',

	    // send all mails to a file by default. You have to set
	    // 'useFileTransport' to false and configure a transport
	    // for the mailer to send real emails.
	    'useFileTransport' => true,
	],
	// ...
]

\backend\config\main.php

'modules' => [
    'user' => [
        'class' => 'yongtiger\user\Module',

        ///Signup
        'enableSignup' => false,
        'disableSignupMessage' => false,    ///[v0.9.5 (backend disableSignupMessage)]
        'enableRecoveryPassword' => false,  ///[v0.9.7 (backend:enableRecoveryPassword)]

    // ...
],

'components' => [

    ///[Yii2 uesr]
    'user' => [
        'identityClass' => 'yongtiger\user\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
        'loginUrl' => ['user/security/login'], ///default is 'site/login'
    ],
    // ...
]

\frontend\config\main.php

'modules' => [
    'user' => [
        'class' => 'yongtiger\user\Module',

        ///Signup
        'enableSignup' => false,
        'disableSignupMessage' => Yii::t('user', 'This site has been closed registration.'),

        'enableSignupWithUsername' => false,
        'enableSignupWithRepassword' => false,
        'enableSignupWithEmail' => false,

        'enableSignupWithEmailActivation' => false,
        'signupWithEmailActivationExpire' => 600,
        'signupWithEmailActivationComposeHtml' => '@yongtiger/user/mail/activate-status-html',
        'signupWithEmailActivationComposeText' => '@yongtiger/user/mail/activate-status-text',
        'signupWithEmailActivationSetFrom' => ['support@brainbook.cc' => 'My Application robot'],

        'enableSignupAjaxValidation' => false,
        'enableSignupClientValidation' => false,
        'enableSignupValidateOnBlur' => false,
        'enableSignupValidateOnSubmit' => false,

        'enableSignupWithCaptcha' => false,

        ///Login
        'enableLogin' => false,
        'disableLoginMessage' => Yii::t('user', 'This site has been closed login.'),
        //////[Yii2 uesr:login with username or email]when both `enableLoginWithUsername` and `enableLoginWithEmail` are `true` 
        'enableLoginWithUsername' => false,
        'enableLoginWithEmail' => false,

        'enableLoginAjaxValidation' => false,
        'enableLoginClientValidation' => false,
        'enableLoginValidateOnBlur' => false,
        'enableLoginValidateOnSubmit' => false,

        'enableLoginWithCaptcha' => false,

        ///[Yii2 uesr:recovery]
        'enableRecoveryPassword' => true,  ///[v0.9.7 (backend:enableRecoveryPassword)]
        'recoveryPasswordExpire' => 0,
        'recoveryPasswordComposeHtml' => '@yongtiger/user/mail/recover-password-html',
        'recoveryPasswordComposeText' => '@yongtiger/user/mail/recover-password-text',
        'recoveryPasswordSetFrom' => ['support@brainbook.cc' => 'My Application robot'],

        ///[Yii2 uesr:account]
        'enableAccountChangeWithPassword' => false,
        'enableAccountChangePasswordWithRepassword' => false,
        'accountVerificatonExpire' => 600,
        'accountVerifyEmailComposeHtml' => '@yongtiger/user/mail/verify-email-html',
        'accountVerifyEmailComposeText' => '@yongtiger/user/mail/verify-email-text',
        'accountVerifyEmailSetFrom' => ['support@brainbook.cc' => 'My Application robot'],

        'enableAccountChangeAjaxValidation' => false,
        'enableAccountChangeClientValidation' => false,
        'enableAccountChangeValidateOnBlur' => false,
        'enableAccountChangeValidateOnSubmit' => false,

        'enableAccountChangeWithCaptcha' => false,

        ///[Yii2 uesr:token]
        'enableSendTokenWithoutLoad' => false,
        'enableSendTokenAjaxValidation' => false,
        'enableSendTokenClientValidation' => false,
        'enableSendTokenValidateOnBlur' => false,
        'enableSendTokenValidateOnSubmit' => false,

        'enableSendTokenWithCaptcha' => false,

        ///[Yii2 uesr:captcha]
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            // 'controller'=>'login',  ///The controller that owns this action
            // 'backColor'=>0xFFFFFF,  ///The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
            // 'foreColor'=>0x2040A0,  ///The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
            // 'padding' => 5,         ///Padding around the text. Defaults to 2.
            // 'offset'=>-2,           ///The offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.
            'height' => 36,         ///The height of the generated CAPTCHA image. Defaults to 50. need to be adjusted according to the specific verification code bit
            'width' => 96,          ///The width of the generated CAPTCHA image. Defaults to 120.
            'maxLength' =>6,        ///The maximum length for randomly generated word. Defaults to 7.
            'minLength' =>4,        ///The minimum length for randomly generated word. Defaults to 6.
            'testLimit'=>5,         ///How many times should the same CAPTCHA be displayed. Defaults to 3. A value less than or equal to 0 means the test is unlimited (available since version 1.1.2). Note that when 'enableClientValidation' is true (default), it will be invalid!
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,    ///The fixed verification code. When this property is set, getVerifyCode() will always return the value of this property. This is mainly used in automated tests where we want to be able to reproduce the same verification code each time we run the tests. If not set, it means the verification code will be randomly generated.
        ],
        'captchaActiveFieldWidget' => [
            'class' => 'yii\captcha\Captcha',
            'imageOptions' => ['alt' => 'Verification Code', 'title' => 'Click to change another verification code.'],
            'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
        ],

        ///[Yii2 uesr:oauth]
        'enableOauth' => false,
        'enableOauthSignup' => false,
        'enableOauthSignupValidation' => false,
        'authChoiceWidgetConfig' => [
            'baseAuthUrl' => new \yii\helpers\ReplaceArrayValue(['security/auth']),  ///cannot be `['security/auth']`! ArrayHelper::merge will get wrong result. instead, we use `ReplaceArrayValue`.
            // 'popupMode' => false,     ///defaults to true
            // 'options' => ['class'=>'control-label'], ///widget div options
            'clientOptions' => [
                'popup'=> [
                    'resizable'=>'yes',
                    'scrollbars'=>'yes',
                    // 'toolbar'=>'no',
                    // 'menubar'=>'no',
                    // 'location'=>'no',
                    // 'directories'=>'no',
                    // 'status'=>'yes',
                    // 'width'=>450,
                    // 'height'=>380,
                ]
            ],
        ],
        'auth' => [
            'class' => 'yii\authclient\AuthAction',
            // 'successCallback' => Yii::$app->user->isGuest ? [$this, 'authenticate'] : [$this, 'connect'],   ///cannot configure 'successCallback' here because of `$this`!!!
            ///Cannot use `Yii::$app` here! we will use `Yii::$app->urlManager->createUrl()` in module init() later
            ///Cannot be `['security/auth']`! ArrayHelper::merge will get wrong result. instead, we use `ReplaceArrayValue`.
            'successUrl' => new \yii\helpers\ReplaceArrayValue(['user/account/index']),
            'cancelUrl' => new \yii\helpers\ReplaceArrayValue(['user/security/login']),
        ]
    ],
],

'components' => [

    ///[Yii2 uesr]
    'user' => [
        'identityClass' => 'yongtiger\user\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        'loginUrl' => ['user/security/login'],
    ],

    ///[Yii2 uesr:oauth]
    'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
            'google' => [
                'class' => 'yongtiger\authclient\clients\Google',
                'clientId' => '409149769406-aigbmsp0doiqtgj167c2oqtl294pdbsp.apps.googleusercontent.com',
                'clientSecret' => 'Y-PGLBHKJWM3XNyCyOXJCbdB',
                ///'scope' => 'profile email',
            ],
            'twitter' => [
                'class' => 'yongtiger\authclient\clients\Twitter',
                'consumerKey' => '8qsSIMpcPm0UWdBpHWl8bGNKY',
                'consumerSecret' => 'RgE1AGjTVMVxzzGn0W9VB6KinItwT7qySb0yNFyEK3zb7AErlw',
                'attributeParams' => [
                    'include_email' => 'true'
                ],
            ],
            'yandex' => [
                'class' => 'yongtiger\authclient\clients\Yandex',
                'clientId' => '6c630052d9d7452c802963caf10cc835',
                'clientSecret' => 'dce654019d944666a77856b5418a11e9',
            ],
            'vkontakte' => [
                'class' => 'yongtiger\authclient\clients\VKontakte',
                'clientId' => '5827650',
                'clientSecret' => '0h9bQ0Jznka53DOS1TjE',
            ],
            'facebook' => [
                'class' => 'yongtiger\authclient\clients\Facebook',
                'clientId' => '1821672494750455',
                'clientSecret' => '312e971fce4ec12c9966790c7a1704fa',
            ],
            'linkedin' => [
                'class' => 'yongtiger\authclient\clients\LinkedIn',
                'clientId' => '86ehrtbhkko1tl',
                'clientSecret' => 'wS39dshiAzr9Myrl',
                // 'scope' => 'r_basicprofile r_emailaddress'
            ],
            'github' => [
                'class' => 'yongtiger\authclient\clients\GitHub',
                'clientId' => 'd9bf109efa527c68d1a7',
                'clientSecret' => 'af919e40ce0fffdd1650e7bbab8c17bdb18560d4',
                // 'normalizeUserAttributeMap' => ['username'=>'login'],
                ///'scope' => 'user:email user',
                // 'viewOptions' => [
                //        'popupWidth' => 160,
                //        'popupHeight' => 480,
                // ],
            ],
            'live' => [
                'class' => 'yongtiger\authclient\clients\Live',
                'clientId' => '11a7aa93-7369-42aa-8477-426aca7d1839',
                'clientSecret' => '2YjcjkCBYwtiBHyp6mmvauh',
                ///'scope' => 'wl.basic wl.emails wl.contacts_emails wl.signin',
            ],

            // 'google-hybrid' => [   ///@see https://yiiframework.cn/doc-2.0/yii-authclient-clients-googlehybrid.html
            //     'class' => 'yongtiger\authclient\clients\GoogleHybrid',
            //     'clientId' => '***',
            //     'clientSecret' => '***',
            //     'scope' => 'email'
            //     'viewOptions' => [
            //         'widget' => [
            //             'class' => 'yii\authclient\widgets\GooglePlusButton',
            //             'buttonHtmlOptions' => [
            //                 'data-approvalprompt' => 'force'
            //             ],
            //         ],
            //     ],
            // ],

            'yahoo' => [
                'class' => 'yongtiger\authclient\clients\Yahoo',   ///@see https://github.com/dnshouse/yii2-authclient-extended/blob/master/Yahoo.php
                'clientId' => 'dj0yJmk9aVMxbnRvclppM1NmJmQ9WVdrOU4zZHlSMVJ3TXpJbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1iZg--',
                'clientSecret' => 'f17f0e8925bf17b8ff20a45517c628cd4f78dcdc',
                ///'scope' => 'openid profile'
            ],
            'amazon' => [
                'class' => 'yongtiger\authclient\clients\Amazon',   ///@see https://github.com/xjflyttp/yii2-oauth/blob/master/AmazonAuth.php
                'clientId' => 'amzn1.application-oa2-client.c3b583b90a274981a36d53f47234f111',
                'clientSecret' => 'd303910396688e088350570281d4896fbb641661bac06ce1ff5a68fdcc4a2717',
            ],
            'instagram' => [
                'class' => 'yongtiger\authclient\clients\Instagram',
                'clientId' => '70a18ce48a8c4ffb92b0d5b288bed466',
                'clientSecret' => '8b59f9def099438b9259b62fc23808fd',
            ],
            'reddit' => [
                'class' => 'yongtiger\authclient\clients\Reddit',   ///@see https://github.com/amnah/yii2-user/blob/master/components/RedditAuth.php
                'clientId' => 'rDzcE3ocGlxacg',
                'clientSecret' => '9SKzxseITtqTgwDQFksUkvVLzwA',
            ],

            'qq' => [
                'class' => 'yongtiger\authclient\clients\Qq',
                'clientId' => '101367642',
                'clientSecret' => '3fce443fcbf5789f8790d7f055de21da',
            ],
            'weixin' => [
                'class' => 'yongtiger\authclient\clients\Weixin',
                'clientId' => 'wx2634dbab565e2f27',
                'clientSecret' => 'aefe4921e37778bd475054b78a6eff30',
            ],
            'weibo' => [
                'class' => 'yongtiger\authclient\clients\Weibo',
                'clientId' => '812398870',
                'clientSecret' => 'e16fef081e8c717d4457434b630312be',
            ],
            // 'douban' => [
            //     'class' => 'yongtiger\authclient\clients\Douban',
            //     'clientId' => '***',
            //     'clientSecret' => '***',
            // ],
            // 'renren' => [
            //     'class' => 'yongtiger\authclient\clients\Renren',
            //     'clientId' => '***',
            //     'clientSecret' => '***',
            // ],

        ],
    ],
    // ...
]

注意:使用后端模块的行为,禁止模块(如 user)的控制器(可用于前后端)。你可以添加自己的后端模块/控制器,否则在后台无法访问。

注意:在实际应用中使用你自己的后端视图!

国际化设置(可选)

在此扩展中引入的所有文本和消息均可在以下类别下进行翻译:

'extensions/yongtiger/yii2-user/*'

默认的 basePath 为 '@vendor/yongtiger/yii2-user/src/messages'。

如果你想自定义自己的翻译,请使用以下应用程序配置:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'extensions/yongtiger/yii2-user/*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',
                    'basePath' => '<your custumized message path>',    ///custumize your own translations
                    'fileMap' => [
                        'extensions/yongtiger/yii2-user/user' => 'user.php',
                    ],
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

前端使用

  • 访客
/user
/user/security
/user/security/login
/user/security/logout
/user/registration/signup
/user/token/send-token&type=recovery
/user/token/send-token&type=activation
  • 登录后
/user/account/index
/user/account/change&item=username
/user/account/change&item=email
/user/account/change&item=password
/user/security/disconnect&provider=<auth-client>
/user/security/auth&authclient=<auth-client>

后端使用


注意

文档

另请参阅

待办事项

开发路线图

许可证

Yii2-user 在 MIT 许可证下发布,有关详细信息,请参阅 LICENSE 文件。