michele-angioni/phalcon-auth
Composer 安装命令:
composer require michele-angioni/phalcon-auth
包简介
A library which provide you with simple authentication.
关键字:
README 文档
README
Introduction
Phalcon Auth provides you a fast way to register and authenticate your users.
Every application, every website needs its own User model with its own properties and methods. Phalcon Auth does not force you to use its own model nor create useless overhead by defining relationships with other models. You are a Phalcon user, so speed and simplicity is what you are looking for.
So, Phalcon Auth just requires your own User model to satisfy some requirements by implementing its interface. Basically, it just need a few methods:
- getId() : (int)
- getEmail() : (string)
- getPassword() : (string)
- setPassword($password) : (bool)
- getConfirmationCode() : (string)
- setConfirmationCode($confirmationCode) : (bool)
- confirm() : (bool)
- isConfirmed() : (bool)
- isBanned() : (bool)
Furthermore, if you want to use the "remember me" feature, the following remember token getter and setter are required
- getRememberToken() : (string)
- setRememberToken($token) : (bool)
Installation
Phalcon Auth can be installed through Composer, just include "michele-angioni/phalcon-auth": "~0.1" to your composer.json and run composer update or composer install.
Usage
Let's say you have a MyApp\Users model you want to make authenticatable.
The way to do it is very simple, i.e. it must implement the MicheleAngioni\PhalconAuth\Contracts\AuthableInterface or, if you want to use the remember me feature, the MicheleAngioni\PhalconAuth\Contracts\RememberableAuthableInterface.
An example can be the this one:
namespace MyApp;
class Users extends \Phalcon\Mvc\Model implements \MicheleAngioni\PhalconAuth\Contracts\RememberableAuthableInterface
{
protected $id;
protected $banned;
protected $confirmation_code;
protected $confirmed;
protected $email;
protected $password;
protected $remember_token;
public function getId()
{
return $this->id;
}
public function getConfirmationCode()
{
return $this->confirmation_code;
}
public function isConfirmed()
{
return (bool)$this->confirmed;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
return true;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
return true;
}
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($token)
{
$this->remember_token = $token;
return true;
}
public function isBanned()
{
return (bool)$this->banned;
}
}
We can then define the auth service in the Phalcon container in application bootstrap file, and pass the MyApp\Users model to it.
This way, it will be easily retrievable for example in the controllers
/**
* Authentication
*/
$di->setShared('auth', function () {
return \MicheleAngioni\PhalconAuthAuth(new \MyApp\Users());
});
Now we can define a simple controller for User registration, confirmation, login, logout and password reset:
<?php
namespace MyApp\Controllers;
use Phalcon\Mvc\Controller;
class AuthController extends Controller
{
public function registerAction()
{
$email = $this->request->getPost('email);
$password = $this->request->getPost('password);
// [..] Data validation
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Register the new user
try {
$user = $auth->register($email, $password);
} catch (\Exception $e) {
if ($e instanceof \UnexpectedValueException) {
// The email has already been taken, handle the exception
} else {
// Handle other exception
}
}
[...] // It is up to you to comunicate the confirmation code to the user
}
public function confirmAction($idUser, $confirmationCode)
{
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Confirm the user
try {
$user = $auth->confirm($idUser, $confirmationCode);
} catch (\Exception $e) {
if ($e instanceof \EntityNotFoundException) {
// User not found. Handle the exception
} else {
// Wrong confirmation code. Handle other exception
}
}
[...]
}
public function loginAction()
{
$email = $this->request->getPost('email);
$password = $this->request->getPost('password);
// [..] Data validation
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Perform login
try {
$user = $auth->attemptLogin($email, $password);
} catch (\Exception $e) {
if ($e instanceof \MicheleAngioni\PhalconAuth\Exceptions\EntityBannedException) {
// The user is banned. Handle exception
} else {
// Handle wrong credentials exception
}
}
[...]
}
public function logoutAction()
{
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Perform logout
$auth->logout();
[...]
}
public function getPasswordTokenAction($idUser)
{
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Get the reset password token
try {
$token = $auth->getResetPasswordToken($idUser);
} catch (\Exception $e) {
if ($e instanceof \MicheleAngioni\PhalconAuth\Exceptions\EntityNotFoundException) {
// User not found. Handle the exception
} else {
// Authable entity is not confirmed yet, it cannot reset the password. Handle the exception
}
}
[...]
}
public function resetPasswordAction($idUser, $resetToken)
{
$password = $this->request->getPost('newPassword);
// [..] Data validation
// Retrieve Auth Service
$auth = $this->getDI()->get('auth');
// Get the reset password token
try {
$token = $auth->resetPassword($idUser, $resetToken, $newPassword);
} catch (\Exception $e) {
if ($e instanceof \MicheleAngioni\PhalconAuth\Exceptions\EntityNotFoundException) {
// User not found. Handle the exception
} else {
// Authable entity is not confirmed or the token is wrong. Handle the exception
}
}
[...]
}
}
After the login, the user id and email will be saved in the session.
Advanced user registration
When registering a new user, you can pass an array of other parameters and an array of parameters you want to be unique in your user table
$auth->register($email, $password, $parameters = [], $uniqueParameters = [], $addConfirmationCode = true));
Customize login
You can customize the login settings by modifying the other method parameters
$auth->attemptLogin($email, $password, $saveSession = true, $rememberMe = false);
Logging in after the "remember me" has been set
After authenticating with a "remember me", just use the following method
if ($auth->hasRememberMe()) {
$auth->loginWithRememberMe();
}
Retrieve the logged user info from the session
$auth->getIdentity(); // Returns an array with 'id' and 'email' keys
Retrieve the authenticated user
$auth->getAuth();
Manually login through user id
$auth->authById($id);
Customize the behaviour
When defining the Auth service, you can can pass an options array. Below all available options are listed
/**
* Authentication
*/
$options = [
'rememberMeDuration' => 1096000 // Optional, default: 604800 (1 week)
];
$di->set('auth', function () {
return \MicheleAngioni\PhalconAuthAuth(new \MyApp\Users(), $options);
});
Contribution guidelines
Phalcon Auth follows PSR-1, PSR-2 and PSR-4 PHP coding standards, and semantic versioning.
Pull requests are welcome.
License
Phalcon Auth is free software distributed under the terms of the MIT license.
michele-angioni/phalcon-auth 适用场景与选型建议
michele-angioni/phalcon-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.4k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repositories」 「phalcon」 「repo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 michele-angioni/phalcon-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 michele-angioni/phalcon-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 michele-angioni/phalcon-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
StyleCI Git Is A Git Repository Manager
A Laravel package for the Repository Design Pattern.
Creating a Laravel Repositories
Simple, safe and flexible implementation of handling SQL entities and repositories as well as multi-table SQL queries while staying lightweight and straightforward.
Symfony integration of squirrelphp/entities - automatic integration of generated repositories for existing entities.
Uniondrug Trace Client Component for uniondrug/framework
统计信息
- 总下载量: 7.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-06