loadsys/cakephp-auth-userentity

一个增强 CakePHP 的 AuthComponent 以提供 userEntity() 方法的 CakePHP 3 插件。

安装次数: 9,903

依赖项: 0

建议者: 0

安全: 0

星标: 3

关注者: 11

分支: 0

开放问题: 0

类型:cakephp-plugin

1.0.1 2015-12-08 14:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:47:36 UTC


README

Latest Version Build Status Coverage Status Software License Total Downloads

一个增强 CakePHP 的 AuthComponent 以提供 userEntity() 方法的 CakePHP 3 插件。

要求

  • CakePHP 3.0.0+
  • PHP 5.6+

安装

使用 composer 将插件拖入您的项目中

$ composer require loadsys/cakephp-auth-userentity:~1.0

要在您的应用中使用此插件,您必须用此插件中的 AuthComponent 覆盖默认的 AuthComponent。这通常在 AppController::initialize() 中完成。

$this->loadComponent('Auth', [
	/**
	 * Name the plugin's Component as the class to use. This is **required**
	 * in order to use the plugin.
	 */
	'className' => 'AuthUserEntity.UserEntityAuth',

	/**
	 * Name the Entity class that will be used as the container for the
	 * array data in Auth->user(). Defaults to `\Cake\ORM\Entity`, which
	 * is safe for all apps, but will exclude any custom logic you may
	 * have defined in your app's "user" Entity class.
	 */
	'entityClass' => '\App\Model\Entity\User',

	/**
	 * Any options to pass to the new Entity when it is created. The defaults
	 * are:
	 *
	 *   [
	 *       'markClean' => true,    // Force the Entity to appear clean.
	 *       'source' => 'AuthUser', // The repository this record originated from.
	 *                               // We default to a fake name to make it clear
	 *                               // the Entity doesn't represent a "true" ORM
	 *                               // record.
	 *   ]
	 */
	'entityOptions' => [
		'associated' => ['Permissions', 'Groups'],
	],

	/**
	 * (The rest of your normal Auth configs follow here.)
	 */
	// ...
]);

用法

安装完成后,您将能够像这样从控制器中检索用户实体

	// Get the whole entity:
	$user = $this->Auth->userEntity();

	// Or to get a specific property (which will engage any
	// _getProperty() methods you have defined in your Entity class):
	$userEmail = $this->Auth->userEntity('email');

	// Internally, the Entity::get() interface is used, so you can pass
	// nested keys (but remember that this data must be loaded into the
	// Auth session data!):
	$groupName = $this->Auth->userEntity('group.name');

	// Or to call a function from your entity:
	if (!$this->Auth->userEntity()->isAdmin()) {
		$this->Flash->error('Only admins can use this method.');
		return $this->redirect('/');
	}

类似于默认 AuthComponent 的 ::user() 方法,当 Session 中没有可用的认证用户数据时,将返回 null。还需要注意的是,只有保存到 Auth 会话中的数据将在实体中可用,但您可以使用 实体类中的延迟加载 来按需获取额外的属性或 调整用于认证用户的查询,以便在 Auth 会话中获取关联数据。

还需要注意的是,当前只有顶层数据被 序列化,因此子属性将仅作为数组存在,而不是实体本身。

贡献

报告问题

请使用 GitHub Issues 列出任何已知的缺陷或问题。

开发

在开发此插件时,请进行分支操作并提交 PR 以进行任何新的开发。

许可证

MIT

版权

Loadsys Web Strategies 2015