ybsisgood/module-user-management

此包的最新版本(1.0.2)没有可用的许可证信息。

ybsisgood 模块,以及一些修改

1.0.2 2024-05-12 18:46 UTC

This package is auto-updated.

Last update: 2024-09-12 20:40:00 UTC


README

原始仓库 : https://github.com/ybsisgood/user-management

更新

  • 兼容 Admin LTE 3
  • 支持 PHP >= 8.0

优点

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

安装

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

运行

composer require ybsisgood/module-user-management

或将

"ybsisgood/module-user-management": "^2"

添加到您的 composer.json 文件的 require 部分中。

配置

  1. 在您的 config/web.php 中
'components'=>[
	'user' => [
		'class' => 'ybsisgood\modules\UserManagement\components\UserConfig',

		// Comment this if you don't want to record user logins
		'on afterLogin' => function($event) {
				\ybsisgood\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
			}
	],
],

'modules'=>[
	'user-management' => [
		'class' => 'ybsisgood\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';
				};
			},
	],
],

有关事件的信息,请检查

AuthHelper::layoutHandler() 中的布局处理程序示例

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

  1. 在您的 config/console.php 中(此为迁移和与控制台一起工作所必需的)
'modules'=>[
	'user-management' => [
		'class' => 'ybsisgood\modules\UserManagement\UserManagementModule',
	        'controllerNamespace'=>'vendor\ybsisgood\modules\UserManagement\controllers', // To prevent yii help from crashing
	],
],
  1. 运行迁移
./yii migrate --migrationPath=vendor/ybsisgood/module-user-management/migrations/
  1. 在您的基控制器中
public function behaviors()
{
	return [
		'ghost-access'=> [
			'class' => 'ybsisgood\modules\UserManagement\components\GhostAccessControl',
		],
	];
}

您可以访问的地方

<?php
use ybsisgood\modules\UserManagement\components\GhostMenu;
use ybsisgood\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']],
			],
		],
	],
]);
?>

第一步

首先,您会看到上面菜单中的 2 个元素:“登录”和“注销”,因为您没有权限访问其他 URL,并且我们使用 GhostMenu::widget() 来渲染菜单。它只会渲染活跃用户可以访问的元素。

同样,GhostNav::widget()GhostHtml:a() 也有相同的功能

  1. 以 superadmin/superadmin 身份登录

  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)

事件

事件可以通过以下配置文件处理

'modules'=>[
	'user-management' => [
		'class' => 'ybsisgood\modules\UserManagement\UserManagementModule',
		'on afterRegistration' => function(UserAuthEvent $event) {
			// Here you can do your own stuff like assign roles, send emails and so on
		},
	],
],

支持的事件列表可以在 UserAuthEvent 类中找到

常见问题解答(FAQ)

问题:您有 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. 创建具有用户_id(与“用户”表相关联)的表和模型以用于个人资料

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

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

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

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