unique / module-user-management
具有改进的RBAC
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
此仓库未积极维护。如果您想添加一些更改,只需将其分叉并在安全的情况下使用您的分叉。
优点
- 用户管理
- RBAC(角色、权限等)带有Web界面
- 注册、授权、密码恢复等
- 访问日志
- 优化(在常规用户工作流程中无需数据库查询)
- 像GhostMenu或GhostHtml::a这样的漂亮小部件,其中元素仅在用户有权访问它们指向的路由时才可见
安装
安装此扩展的首选方式是通过composer。
运行以下命令:
composer require webvimark/module-user-management
或者将以下内容添加到您的composer.json
文件的require部分:
"webvimark/module-user-management": "^1"
配置
在您的config/web.php中
- 有关事件的更多信息,请查看
'components'=>[ 'user' => [ 'class' => 'unique\modules\UserManagement\components\UserConfig', // Comment this if you don't want to record user logins 'on afterLogin' => function($event) { \unique\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id); } ], ], 'modules'=>[ 'user-management' => [ 'class' => 'unique\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' => 'unique\modules\UserManagement\UserManagementModule', 'controllerNamespace'=>'vendor\unique\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' => 'unique\modules\UserManagement\components\GhostAccessControl', ], ]; }
第一步
<?php use unique\modules\UserManagement\components\GhostMenu; use unique\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' => 'unique\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()中的示例。现在使用主题更改registration.php文件
-
定义自己的 UserManagementModule::$registrationFormClass。在这个类中,您可以执行任何操作,例如验证自定义表单和保存配置文件
-
创建一个控制器,让用户可以查看配置文件