yii2mod / yii2-user
用户模块
2.1
2017-06-08 14:56 UTC
Requires
- php: >=5.6
- yii2mod/yii2-enum: *
- yiisoft/yii2: >=2.0.8
- yiisoft/yii2-swiftmailer: *
Requires (Dev)
README
Yii2 用户扩展
为 Yii2 提供灵活的用户注册和认证模块
安装
安装此扩展的首选方法是通过 composer。
运行以下命令
php composer.phar require --prefer-dist yii2mod/yii2-user "*"
或
"yii2mod/yii2-user": "*"
将以下内容添加到您的 composer.json
文件的 require 部分中。
操作
此扩展提供了一些独立的行为类,提供特定的操作支持
- [[yii2mod\user\actions\LoginAction]] - 登录用户。以下为可选参数
view
- 需要渲染的视图名称。modelClass
- 登录模型类名。layout
- 应用到此视图的布局名称。returnUrl
- 成功后用户应重定向到的 URL。
- [[yii2mod\user\actions\LogoutAction]] - 注销当前用户。以下为可选参数
returnUrl
- 成功后用户应重定向到的 URL。
- [[yii2mod\user\actions\SignupAction]] - 注册用户。以下为可选参数
view
- 需要渲染的视图名称。modelClass
- 注册模型类名。returnUrl
- 成功后用户应重定向到的 URL。
- [[yii2mod\user\actions\RequestPasswordResetAction]] - 为用户请求密码重置。以下为可选参数
view
- 需要渲染的视图名称。modelClass
- 请求密码模型类。successMessage
- 邮件发送成功时给用户的消息。errorMessage
- 邮件未发送时给用户的错误消息。returnUrl
- 成功后用户应重定向到的 URL。
- [[yii2mod\user\actions\PasswordResetAction]] - 为用户重置密码。以下为可选参数
view
- 需要渲染的视图名称。modelClass
- 重置密码模型类。successMessage
- 成功时设置的消息。returnUrl
- 成功后用户应重定向到的 URL。
配置
- 如果您没有使用 base 模板,则需要通过以下命令执行迁移
php yii migrate/up --migrationPath=@vendor/yii2mod/yii2-user/migrations
- 您需要在项目配置中配置
params
部分
'params' => [ 'user.passwordResetTokenExpire' => 3600 ]
- 您需要创建一个继承自 UserModel 的 UserModel 类,并在项目配置中配置
identityClass
属性,例如
'user' => [ 'identityClass' => 'yii2mod\user\models\UserModel', // for update last login date for user, you can call the `afterLogin` event as follows 'on afterLogin' => function ($event) { $event->identity->updateLastLogin(); } ],
-
为了发送电子邮件,您需要在项目配置中配置
mailer
组件。 -
如果您项目邮件文件夹中没有
passwordResetToken.php
模板文件,则需要创建它,例如
<?php use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $user */ $resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/password-reset', 'token' => $user->password_reset_token]); ?> Hello <?php echo Html::encode($user->username) ?>, Follow the link below to reset your password: <?php echo Html::a(Html::encode($resetLink), $resetLink) ?>
此模板用于密码重置电子邮件。
- 添加到 SiteController(或通过
$route
参数在 urlManager 中配置)
/** * @return array */ public function actions() { return [ 'login' => [ 'class' => 'yii2mod\user\actions\LoginAction' ], 'logout' => [ 'class' => 'yii2mod\user\actions\LogoutAction' ], 'signup' => [ 'class' => 'yii2mod\user\actions\SignupAction' ], 'request-password-reset' => [ 'class' => 'yii2mod\user\actions\RequestPasswordResetAction' ], 'password-reset' => [ 'class' => 'yii2mod\user\actions\PasswordResetAction' ], ]; }
然后您可以通过以下 URL 访问这些行为
- http://localhost/site/login
- http://localhost/site/logout
- http://localhost/site/signup
- http://localhost/site/request-password-reset
- http://localhost/site/password-reset
- 一些行为会发送闪存消息,因此您应该在网站上使用 AlertWidget 来渲染闪存消息。
使用行为事件
您可以使用以下事件
/** * @return array */ public function actions() { return [ 'login' => [ 'class' => 'yii2mod\user\actions\LoginAction', 'on beforeLogin' => function ($event) { // your custom code }, 'on afterLogin' => function ($event) { // your custom code }, ], 'logout' => [ 'class' => 'yii2mod\user\actions\LogoutAction', 'on beforeLogout' => function ($event) { // your custom code }, 'on afterLogout' => function ($event) { // your custom code }, ], 'signup' => [ 'class' => 'yii2mod\user\actions\SignupAction', 'on beforeSignup' => function ($event) { // your custom code }, 'on afterSignup' => function ($event) { // your custom code }, ], 'request-password-reset' => [ 'class' => 'yii2mod\user\actions\RequestPasswordResetAction', 'on beforeRequest' => function ($event) { // your custom code }, 'on afterRequest' => function ($event) { // your custom code }, ], 'password-reset' => [ 'class' => 'yii2mod\user\actions\PasswordResetAction', 'on beforeReset' => function ($event) { // your custom code }, 'on afterReset' => function ($event) { // your custom code }, ], ]; }
控制台命令
设置
要启用控制台命令,您需要将模块添加到您的应用程序的控制台配置中。在 yii2-app-basic 模板中为 /config/console.php
,或在 yii2-app-advanced 中为 /console/config/main.php
。
return [ 'id' => 'app-console', 'modules' => [ 'user' => [ 'class' => 'yii2mod\user\ConsoleModule', ], ],
可用的控制台操作
- user/create - 创建新用户。
./yii user/create <email> <username> <password> - email (required): string - username (required): string - password (required): string
- user/role/assign - 将角色分配给用户。
./yii user/role/assign <roleName> <email> - roleName (required): string - email (required): string
- user/role/revoke - 从用户中撤销角色。
./yii user/role/revoke <roleName> <email> - roleName (required): string - email (required): string
- user/delete - 删除用户。
./yii user/delete <email> - email (required): string
- user/update-password - 将用户的密码更新为指定的密码。
./yii user/update-password <email> <password> - email (required): string - password (required): string
国际化
本扩展中引入的所有文本和消息均在“yii2mod.user”类别下可翻译。您可以使用本扩展内提供的翻译,通过以下应用程序配置实现:
return [ 'components' => [ 'i18n' => [ 'translations' => [ 'yii2mod.user' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@yii2mod/user/messages', ], // ... ], ], // ... ], // ... ];
支持我们
您的业务是否依赖于我们的贡献?通过Patreon联系我们并支持我们。所有承诺都将用于分配人力进行维护和新奇功能的开发。