gobline/auth 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gobline/auth

Composer 安装命令:

composer require gobline/auth

包简介

Authentication component

README 文档

README

Most oftenly, your server needs a way to identify the user making the current request. The authentication component provides a means for representing the user making the request and for authenticating this user against a DB or other datasource.

  • the CurrentUserInterface allows the server to identify the user making the current request.
  • the AuthenticatorInterface allows the server to authenticate the user with provided credentials.

Usage

In order to authenticate a user, you will first need to create a CurrentUser object.

$user = new Gobline\Auth\CurrentUser();

Once we have a user, we can authenticate the latter using an adapter implementing AuthenticatorInterface. One implementation is already provided, authenticating the user against a database: DbAuthenticator.

Firstly, we will have to set up the adapter. The DbAuthenticator requires a PDO instance and an instance of TableMetadata wrapping some metadata about the DB table we are authenticating against.

use Gobline\Auth\Authenticator\Db\TableMetadata;
use Gobline\Auth\Authenticator\Db\DbAuthenticator;

$authenticator = new DbAuthenticator($pdo, new TableMetadata('users'));

By default, TableMetadata assumes that

  • the column id contains the id of the user
  • the column email contains the login of the user
  • the column password contains the password of the user
  • the password encryption is bcrypt (possible values are: bcrypt, md5, sha1 or clear)

Customize these metadata for your database.

use Gobline\Auth\Authenticator\Db\TableMetadata;
use Gobline\Auth\Authenticator\Db\DbAuthenticator;

$metadata = new TableMetadata('users');
$metadata
    ->setColumnId('user_id')
    ->setColumnLogin('user_email')
    ->setColumnRole('user_type')
    ->setColumnPassword('user_password')
    ->setPasswordEncryption('md5');

$authenticator = new DbAuthenticator($pdo, $metadata);

You will also notice that besides the id, login and password, it is possible to specify a column containing the role (or group, type) of the user. This is especially useful when combining the authentication component with an ACL component (which is usually the case).

Now that we have set up our adapter, it's time to authenticate the user.

$authenticator->setIdentity('mdecaffmeyer@gmail.com');
$authenticator->setCredential('123456');

$user->isAuthenticated(); // returns false

if ($authenticator->authenticate($user)) {
	echo $user->getId(); // prints the user id from DB

	$user->isAuthenticated(); // returns true
}

The authenticator sets the id and the login of the user. It can additionally add the user's role (for ACL) and other column data.

An authenticated user is a user whose id is not null.

Persist the user's data among requests

While the above works perfectly for the first request, the user's data will be lost on subsequent requests. One way to achieve persistence is to store the user data in session. Gobline\Auth\Persistence\CurrentUser acts like a decorator over Gobline\Auth\CurrentUser (or any Gobline\Auth\CurrentUserInterface implementation), allowing to persist the data in session.

$user = new Gobline\Auth\CurrentUser();
$user = new Gobline\Auth\Persistence\CurrentUser($user);

Installation

You can install Gobline Auth using the dependency management tool Composer. Run the require command to resolve and download the dependencies:

composer require gobline/auth

gobline/auth 适用场景与选型建议

gobline/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Authentication」 「auth」 「authenticator」 「gobline」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gobline/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gobline/auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 gobline/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 140
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-12-03