neuronet/module-user-management

具有改进的RBAC的用户 | 基于 webvimark/module-user-management

2.0.3 2020-07-05 23:43 UTC

This package is auto-updated.

Last update: 2024-09-06 08:40:11 UTC


README

优点

  • 用户管理
  • 具有Web界面的RBAC(角色、权限等)
  • 注册、授权、密码恢复等
  • 访问日志
  • 优化(在正常用户工作流程中不进行数据库查询)
  • 像GhostMenu或GhostHtml::a这样的漂亮小部件,其中元素仅在用户有权访问它们所指向的路由时才可见

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

composer require webvimark/module-user-management

或添加以下内容到您的 composer.json 文件的 require 部分。

"webvimark/module-user-management": "@dev"

配置

在你的 config/web.php

  1. 要了解事件,请查看
'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

要查看完整选项列表,请检查 UserManagementModule 文件

在你的 config/console.php 中(这是迁移和与控制台一起工作所需的)

  1. 运行迁移
'modules'=>[
	'user-management' => [
		'class' => 'webvimark\modules\UserManagement\UserManagementModule',
	        'controllerNamespace'=>'vendor\webvimark\modules\UserManagement\controllers', // To prevent yii help from crashing
	],
],
  1. 在你的基本控制器中
./yii migrate --migrationPath=vendor/webvimark/module-user-management/migrations/
  1. 你可以去哪里
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() 也有相同的功能

以超级管理员/超级管理员登录

  1. 转到“权限”并玩那里

  2. 转到“角色”并玩那里

  3. 转到“用户”并玩那里

  4. 放松一下

  5. 使用方法

您的控制器可能有两个属性,可以使整个控制器或所选操作对所有用户可用

或者

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)。以下是无需修改此模块即可完成此操作的方法

  1. 创建具有 user_id 的表和模型(与“user”表相关联)

  2. 检查 AuthController::actionRegistration() 了解其工作方式(您可以跳过此部分)

  3. 为注册定义您的布局。检查 AuthHelper::layoutHandler() 中的示例。现在使用主题来更改注册.php 文件

  4. 定义您自己的UserManagementModule::$registrationFormClass。在这个类中,您可以执行任何您想做的事情,比如验证自定义表单和保存个人资料

  5. 创建您的控制器,用户可以在其中查看个人资料