uzzielpelawak / module-user-management
uzzielpelawak模块,以及一些修改
Requires
- ikimea/browser: 1.12.0
- uzzielpelawak/bootstrap-switch: ^2.0.0
- uzzielpelawak/components: 2.0.1
- uzzielpelawak/date-range-picker: ^2.0.0
- uzzielpelawak/grid-bulk-actions: ^2.0.0
- uzzielpelawak/grid-page-size: ^2.0.0
- uzzielpelawak/helpers: ^2.0.0
README
原始仓库 : https://github.com/uzzielpelawak/user-management
更新
- 兼容 Admin LTE 3
- 支持 PHP >= 8.0
优点
- 用户管理
- 基于Web界面的RBAC(角色、权限等)
- 注册、授权、密码恢复等
- 访问日志
- 优化(在常规用户工作流程中无需查询数据库)
- 如GhostMenu或GhostHtml::a等美观的控件,其中元素仅在用户有权访问它们指向的路由时可见
安装
安装此扩展的首选方法是通过composer。
运行以下命令之一:
composer require uzzielpelawak/module-user-management
或者添加
"uzzielpelawak/module-user-management": "^2"
到您的composer.json
文件的require部分。
配置
- 在您的config/web.php中
'components'=>[ 'user' => [ 'class' => 'uzzielpelawak\modules\UserManagement\components\UserConfig', // Comment this if you don't want to record user logins 'on afterLogin' => function($event) { \uzzielpelawak\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id); } ], ], 'modules'=>[ 'user-management' => [ 'class' => 'uzzielpelawak\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' => 'uzzielpelawak\modules\UserManagement\UserManagementModule', 'controllerNamespace'=>'vendor\uzzielpelawak\modules\UserManagement\controllers', // To prevent yii help from crashing ], ],
- 运行迁移
./yii migrate --migrationPath=vendor/uzzielpelawak/module-user-management/migrations/
- 在您的基控制器中
public function behaviors() { return [ 'ghost-access'=> [ 'class' => 'uzzielpelawak\modules\UserManagement\components\GhostAccessControl', ], ]; }
您可以去哪里
<?php use uzzielpelawak\modules\UserManagement\components\GhostMenu; use uzzielpelawak\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()也有相同的功能
-
以superadmin/superadmin的身份登录
-
转到“权限”并开始操作
-
转到“角色”并开始操作
-
转到“用户”并开始操作
-
放松一下
用法
您控制器可能有两个属性,可以使整个控制器或所选操作对所有人可用
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)
事件
可以通过以下配置文件处理事件
'modules'=>[ 'user-management' => [ 'class' => 'uzzielpelawak\modules\UserManagement\UserManagementModule', 'on afterRegistration' => function(UserAuthEvent $event) { // Here you can do your own stuff like assign roles, send emails and so on }, ], ],
支持的事件列表可以在UserAuthEvent类中找到
常见问题解答
问题:您有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。在这个类中,你可以做任何你想做的事情,比如验证自定义表单和保存配置文件
-
创建一个控制器,让用户可以查看个人资料