dvdkrgr / module-user-management
改进了webvimark/module-user-management(混合模式本地数据库 + ldap)
Requires
- ikimea/browser: dev-master
- webvimark/bootstrap-switch: dev-master
- webvimark/components: dev-master
- webvimark/date-range-picker: dev-master
- webvimark/grid-bulk-actions: dev-master
- webvimark/grid-page-size: dev-master
- webvimark/helpers: dev-master
This package is not auto-updated.
Last update: 2024-09-28 18:22:24 UTC
README
该软件包的修改方式如下
可以为用户对象设置布尔属性ldap_user
$user->ldap_user = true
声明为ldap用户的用户将不会对本地数据库进行身份验证,而是将检查目录。
如果登录成功,用户将以本地数据库中提供的用户身份登录。
您可以在配置文件中设置多个LDAP服务器和多个LDAP域
'user' => [ 'class' => 'webvimark\modules\UserManagement\components\UserConfig', 'ldapServer' => ['10.11.12.13','1.2.3.4','99.99.99.99'], 'ldapDomain' => ['YOURDOMAIN','ANOTHERDOMAIN'], ]
登录过程将尝试使用给定的凭据尝试每个服务器/域组合。如果您想使用服务器端口,只需像这样声明LDAP服务器:12.13.14.16:9999
此插件的示例用法
您在数据库中有本地用户和密码(非ldap用户)。除此之外,您还希望将活动目录绑定到您的应用程序。
在这种情况下,您可以创建一个Yii2命令控制器,该控制器将按月/周/日/小时(您想要的任何时间)运行,并将ldap用户同步到您的数据库中,如下所示
$security = new \yii\base\Security(); $new_user = new \webvimark\modules\UserManagement\models\User; $new_user->id = NULL; $new_user->username = "newuser"; $new_user->password = md5($security->generateRandomString()); $new_user->email = "newuser@example.com"; $new_user->email_confirmed = true; $new_user->ldap_user = true; $new_user->save();
注意:我在md5()方法中使用$security->generateRandomString来在本地数据库中生成随机强密码。这只是为了创建用户,以便在网页上有一个由我们的ldap用户控制的用户对象。
Yii 2的用户管理模块
优点
- 用户管理
- RBAC(角色、权限等)与Web界面
- 注册、授权、密码恢复等
- 访问日志
- 优化(在常规用户工作流程中不进行数据库查询)
- 漂亮的组件,如GhostMenu或GhostHtml::a,其中元素仅在用户有权访问它们指向的路由时可见
安装
安装此扩展的首选方法是通过composer。
运行以下命令:
composer require --prefer-dist webvimark/module-user-management "*"
或
"webvimark/module-user-management": "*"
将其添加到您的composer.json
文件的require部分。
配置
- 在您的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', // 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', ], ],
- 运行迁移
./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()也有相同的功能
-
以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' => 'webvimark\modules\UserManagement\UserManagementModule', 'on afterRegistration' => function(UserAuthEvent $event) { // Here you can do your own stuff like assign roles, send emails and so on }, ], ],
支持的事件列表可以在 UserAuthEvent 类中找到
常见问题解答
问题:我想让用户用他们的电子邮件注册和登录!嗯嗯... 他们还需要确认!
回答:请参阅配置属性 $useEmailAsLogin 和 $emailConfirmationRequired
问题:我想为用户创建包含头像、生日等信息的个人资料。我该怎么做?
回答:个人资料是项目特定的,所以您需要自行实现(但您可以在这里找到示例 - https://github.com/webvimark/user-management/wiki/Profile-and-custom-registration)。以下是如何在不修改此模块的情况下进行操作:
-
创建包含 user_id(与 "user" 表相关联)的个人资料表和模型
-
检查 AuthController::actionRegistration() 的实现方式(您可以跳过这部分)
-
定义注册的布局。检查 AuthHelper::layoutHandler() 中的示例。现在使用主题更改 registration.php 文件
-
定义您自己的 UserManagementModule::$registrationFormClass。在这个类中,您可以执行任何操作,如验证自定义表单和保存个人资料
-
创建一个控制器,用户可以在其中查看个人资料