hampel / user

此包已被弃用,不再维护。未建议替代包。

为Laravel配置用户模型

2.1.2 2014-07-10 09:11 UTC

This package is auto-updated.

Last update: 2019-08-26 01:11:21 UTC


README

为Laravel项目使用的可配置基础用户模型。

允许您抽象数据库的实现细节,以便除了"密码"、"电子邮件"等字段之外还可以使用其他数据库字段 - 同时仍然使用这些默认字段名称来访问和设置模型属性。允许在不重新编码用户模型的情况下更改数据库实现。

当使用Eloquent时,数据库结构与您的代码之间存在强关联,因此对于您使用非标准字段(如用户名、密码等)的情况可能会存在问题。

例如,WordPress使用用户登录和用户密码字段进行身份验证,因此如果您的项目需要验证WordPress数据库,您发现自己必须在应用程序中硬编码这些字段名称。

当您考虑Laravel 4.2默认用户对象中引入的特质时,您可以看到它们期望用户名和密码字段被调用为...用户名和密码!当然,您当然可以创建自己的基础用户对象 - 但仅仅能够使用这些特质 - 代码复用等等。

此包提供了一个简单的可配置用户模型作为您自己的项目的基础。它基于Laravel提供的示例用户模型,并为定义用户名、密码、电子邮件和remember_token的字段名称以及模型使用的数据库连接名称提供了配置选项。

Simon Hampel提供。

版本

版本1.x和2.x之间的功能相同 - 版本2.x只是将基础模型更改为使用特质,因此仅与Laravel框架版本4.2(和PHP 5.4)及更高版本兼容。

安装

建议通过Composer安装可配置用户模型。

在您的composer.json文件中通过Composer要求此包。

对于Laravel版本4.1

:::json
{
    "require": {
        "hampel/user": "1.*"
    }
}

对于Laravel版本4.2

:::json
{
    "require": {
        "hampel/user": "2.*"
    }
}

运行Composer以更新新的需求。

:::bash
$ composer update

此包旨在与Laravel 4框架一起使用。

配置

打开您的Laravel配置文件app/config/app.php,并在$providers数组中添加以下服务提供者,如果它们还不存在

:::php
"providers" => array(

    ...

	'Hampel\Validate\Laravel\ValidateServiceProvider',
	'Hampel\User\UserServiceProvider',

),

当使用此模型进行身份验证时,您还应更改app/config/auth.php的配置,

  1. model更改为'Hampel\User\Models\ConfigurableUser',或者如果您已扩展此模型,指定您自己的模型的完全限定名称。
  2. 如果需要,将table更改为包含您的用户数据的表名称。请注意,如果您用于身份验证的数据库连接指定了表前缀,则不应在表名称中包含前缀。

接下来,使用以下命令发布用户管理配置

:::bash
$ php artisan user:config

(或者,您也可以使用命令php artisan config:publish hampel/user

配置app/config/packages/hampel/user/config.php

  1. connection设置为之前为用户管理数据库设置的数据库连接名称。如果将其设置为值'default',它将使用与您的应用程序相同的数据库连接。
  2. 如果使用自定义用户表,您可以指定用户名、电子邮件和密码字段名称

用法

ConfigurableUser模型基于Eloquent,因此您可以像通常使用任何Eloquent模型一样执行操作。

这个模型的魔力在于,即使数据库中的实际字段不同,你仍然可以使用'username'、'password'、'email'和'remember_token'属性。

以WordPress为例,相关的字段分别是'user_login'、'user_pass'和'user_email'。需要注意的是,虽然数据库列名不区分大小写,但PHP数组键是区分大小写的。由于模型中的属性存储在关联数组中,因此名为id的列实际上与ID不同。由于WordPress使用名为ID的主键列,我们必须为此变化做出调整。

因此,我们按以下方式调整配置文件:

:::php
'id_field' => 'ID',
'username_field' => 'user_login',
'email_field' => 'user_email',
'password_field' => 'user_pass',

现在我们可以像平常一样使用模型,使用'username'、'email'和'password'属性,无需担心实际的数据库字段。

:::php
$user = new ConfigurableUser();
$user->username = 'joe'; // note we just use username and not user_login
$user->email = 'joe@example.com'; // again, we are using email and not user_email
$user->password = 'password'; // the model automatically hashes passwords for us
$user->save(); // the data will be stored in the database using the associated field names we configured

在身份验证方面有一个小小的技巧——身份验证例程实际上使用我们传入的凭证信息直接与数据库进行比较,因此我们需要传入配置的用户名字段。为了混淆问题,我们必须将密码字段指定为'password',无论实际的密码列名是什么——身份验证例程将使用模型函数来获取密码,因此幕后的事情将会正常工作。

// for another example, let's authenticate using the data passed from a form
$username = Input::get('username');
$password = Input::get('password');

$username_field = Config::get('user::username_field');
$password_field = Config::get('user::password_field'); // we don't actually use this here!

$credentials = array(
	$username_field => $username,
	'password' => $password, // we must specify the key as 'password' !!
);

if (Auth::attempt($credentials))
{
	dd('logged in successfully');
}
else
{
	dd('login failed');
}

注意事项

当使用非默认连接进行身份验证时,尝试使用Laravel验证服务验证身份验证数据将失败。您必须显式设置验证器连接名称为用户模型配置的连接名称。

以下示例展示了如何验证从表单POST的username和password。请注意,我们正在使用来自包hampel/validate-laravel-auth的自定义"auth"验证规则。这是可选的,如果您选择,可以完全省略"auth"规则。我编写了这个包来帮助验证身份验证数据,这样在登录表单上您可以轻松地返回一个错误消息,显示提供的密码无效,并要求用户重新提交表单。

:::php
$username = Input::get('username'); // from our form
$password = Input::get('password'); // from our form

$user_table = Config::get('auth.table'); // from the Laravel auth configuration
$connection = Config::get('user::connection'); // from the User configuration

$username_field = Config::get('user::username_field'); // from the User configuration
$password_field = Config::get('user::password_field'); // from the User configuration

$userdata = array(
	'username' => $username, // the 'username' key corresponds to our form field name
	'password' => $password // the 'password' key corresponds to our form field name
);

// Declare the rules for the form validation.
$rules = array(
	'username'  => array('required', "exists:{$user_table},{$username_field}"), // key must match that used in $userdata
	'password'  => array('required', "auth:{$username_field},{$username}") // key must match that used in $userdata
);

$validator = Validator::make($userdata, $rules); // create our validator
$validator->getPresenceVerifier()->setConnection($connection); // <---- THIS IS THE MISSING MAGIC for when we are using a non-default connection

// Check if the form validates with success.
if ($validator->passes())
{
	$credentials = array(
		$username_field => $username,
		'password' => $password // password key must be 'password'
	);

	if (Auth::attempt($credentials))
	{
		dd("auth successful");
	}
	else
	{
		dd("auth failed");
	}
}
dd("validation failed");