mauztor/module-user-management

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

改进的 RBAC 用户

安装: 17

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 163

类型:yii2-extension

v1.0.17 2018-04-22 18:26 UTC

README

Yii 2 用户管理模块

===== forked from webvimark/user-management

优点

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

安装

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

运行以下命令之一

composer require webvimark/module-user-management

"mauztor/module-user-management": "^1"

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

配置

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

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

'modules'=>[
	'user-management' => [
		'class' => 'mauztor\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' => 'mauztor\modules\UserManagement\UserManagementModule',
	        'controllerNamespace'=>'vendor\mauztor\modules\UserManagement\controllers', // To prevent yii help from crashing
	],
],
  1. 运行迁移
./yii migrate --migrationPath=vendor/mauztor/module-user-management/migrations/
  1. 在您的基本控制器中
public function behaviors()
{
	return [
		'ghost-access'=> [
			'class' => 'mauztor\modules\UserManagement\components\GhostAccessControl',
		],
	];
}

你可以去哪里

<?php
use mauztor\modules\UserManagement\components\GhostMenu;
use mauztor\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. 以 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' => 'mauztor\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)。以下是无需修改此模块即可实现它的方法

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

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

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

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

  5. 创建一个用户可以查看个人资料的控制器