neuronet / module-user-management
具有改进的RBAC的用户 | 基于 webvimark/module-user-management
Requires
- ikimea/browser: 1.12.0
- webvimark/bootstrap-switch: ^1.0.0
- webvimark/components: ^1.0.0
- webvimark/date-range-picker: ^1.0.0
- webvimark/grid-bulk-actions: ^1.0.0
- webvimark/grid-page-size: ^1.0.0
- webvimark/helpers: ^1.0.0
README
优点
- 用户管理
- 具有Web界面的RBAC(角色、权限等)
- 注册、授权、密码恢复等
- 访问日志
- 优化(在正常用户工作流程中不进行数据库查询)
- 像GhostMenu或GhostHtml::a这样的漂亮小部件,其中元素仅在用户有权访问它们所指向的路由时才可见
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
composer require webvimark/module-user-management
或添加以下内容到您的 composer.json
文件的 require 部分。
"webvimark/module-user-management": "@dev"
配置
在你的 config/web.php
- 要了解事件,请查看
'components'=>[ 'user' => [ 'class' => 'webvimark\modules\UserManagement\components\UserConfig', // Comment this if you don't want to record user logins 'on afterLogin' => function($event) { \webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id); } ], ], 'modules'=>[ 'user-management' => [ 'class' => 'webvimark\modules\UserManagement\UserManagementModule', // 'enableRegistration' => true, // Add regexp validation to passwords. Default pattern does not restrict user and can enter any set of characters. // The example below allows user to enter : // any set of characters // (?=\S{8,}): of at least length 8 // (?=\S*[a-z]): containing at least one lowercase letter // (?=\S*[A-Z]): and at least one uppercase letter // (?=\S*[\d]): and at least one number // $: anchored to the end of the string //'passwordRegexp' => '^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$', // Here you can set your handler to change layout for any controller or action // Tip: you can use this event in any module 'on beforeAction'=>function(yii\base\ActionEvent $event) { if ( $event->action->uniqueId == 'user-management/auth/login' ) { $event->action->controller->layout = 'loginLayout.php'; }; }, ], ],
https://yiiframework.cn/doc-2.0/guide-concept-events.html
- https://yiiframework.cn/doc-2.0/guide-concept-configurations.html#configuration-format
- 在 AuthHelper::layoutHandler() 中的布局处理程序示例
要查看完整选项列表,请检查 UserManagementModule 文件
在你的 config/console.php 中(这是迁移和与控制台一起工作所需的)
- 运行迁移
'modules'=>[ 'user-management' => [ 'class' => 'webvimark\modules\UserManagement\UserManagementModule', 'controllerNamespace'=>'vendor\webvimark\modules\UserManagement\controllers', // To prevent yii help from crashing ], ],
- 在你的基本控制器中
./yii migrate --migrationPath=vendor/webvimark/module-user-management/migrations/
- 你可以去哪里
public function behaviors() { return [ 'ghost-access'=> [ 'class' => 'webvimark\modules\UserManagement\components\GhostAccessControl', ], ]; }
第一步
<?php use webvimark\modules\UserManagement\components\GhostMenu; use webvimark\modules\UserManagement\UserManagementModule; echo GhostMenu::widget([ 'encodeLabels'=>false, 'activateParents'=>true, 'items' => [ [ 'label' => 'Backend routes', 'items'=>UserManagementModule::menuItems() ], [ 'label' => 'Frontend routes', 'items'=>[ ['label'=>'Login', 'url'=>['/user-management/auth/login']], ['label'=>'Logout', 'url'=>['/user-management/auth/logout']], ['label'=>'Registration', 'url'=>['/user-management/auth/registration']], ['label'=>'Change own password', 'url'=>['/user-management/auth/change-own-password']], ['label'=>'Password recovery', 'url'=>['/user-management/auth/password-recovery']], ['label'=>'E-mail confirmation', 'url'=>['/user-management/auth/confirm-email']], ], ], ], ]); ?>
从上面的菜单开始,你将只看到两个元素:“登录”和“登出”,因为你没有权限访问其他URL,并且我们使用 GhostMenu::widget() 来渲染菜单。它只渲染活动用户可以访问的元素。
同样,GhostNav::widget() 和 GhostHtml:a() 也有相同的功能
以超级管理员/超级管理员登录
-
转到“权限”并玩那里
-
转到“角色”并玩那里
-
转到“用户”并玩那里
-
放松一下
-
使用方法
您的控制器可能有两个属性,可以使整个控制器或所选操作对所有用户可用
或者
public $freeAccess = true;
以下是一些有用的辅助器。有关详细说明,请参阅相应的函数。
public $freeAccessActions = ['first-action', 'another-action'];
角色、权限和路由都具有以下方法
User::hasRole($roles, $superAdminAllowed = true) User::hasPermission($permission, $superAdminAllowed = true) User::canRoute($route, $superAdminAllowed = true) User::assignRole($userId, $roleName) User::revokeRole($userId, $roleName) User::getCurrentUser($fromSingleton = true)
事件
Role::create($name, $description = null, $groupCode = null, $ruleName = null, $data = null) Role::addChildren($parentName, $childrenNames, $throwException = false) Role::removeChildren($parentName, $childrenNames)
可以通过以下配置文件处理事件
支持的完整事件列表可以在 UserAuthEvent 类中找到
'modules'=>[ 'user-management' => [ 'class' => 'webvimark\modules\UserManagement\UserManagementModule', 'on afterRegistration' => function(UserAuthEvent $event) { // Here you can do your own stuff like assign roles, send emails and so on }, ], ],
常见问题解答
问题
问题:您有API文档吗?
答案:查看这个http://opensource.id5.com.br/webvimark/doc/index.html(感谢lukBarros)
问题:我想让用户用他们的电子邮件注册和登录!嗯嗯... 他们还应该确认它!
答案:请参阅配置属性 $useEmailAsLogin 和 $emailConfirmationRequired
问题:我想为用户创建一个有头像、生日等信息的个人资料。我该怎么做?
答案:个人资料是项目特定的,因此您需要自己实现它们(但您可以在以下位置找到示例 - https://github.com/webvimark/user-management/wiki/Profile-and-custom-registration)。以下是无需修改此模块即可完成此操作的方法
-
创建具有 user_id 的表和模型(与“user”表相关联)
-
检查 AuthController::actionRegistration() 了解其工作方式(您可以跳过此部分)
-
为注册定义您的布局。检查 AuthHelper::layoutHandler() 中的示例。现在使用主题来更改注册.php 文件
-
定义您自己的UserManagementModule::$registrationFormClass。在这个类中,您可以执行任何您想做的事情,比如验证自定义表单和保存个人资料
-
创建您的控制器,用户可以在其中查看个人资料