webvimark/module-user-management

此包最新版本(1.0.13)没有提供许可信息。

具有改进的RBAC

1.0.13 2016-10-14 10:53 UTC

This package is auto-updated.

Last update: 2024-09-15 00:20:57 UTC


README

此仓库未积极维护。如果您想添加一些更改,只需将其分支出来并安全地使用您的分支。

优点

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

安装

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

运行以下命令

composer require webvimark/module-user-management

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

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

配置

  1. 在您的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',

		// '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' => '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']],
			],
		],
	],
]);
?>

第一步

首先,您只会看到上面的菜单中的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' => '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类中找到

常见问题解答

问题:您有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. 创建一个控制器,用户可以在其中查看个人资料