承接 michele-angioni/phalcon-auth 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

michele-angioni/phalcon-auth

Composer 安装命令:

composer require michele-angioni/phalcon-auth

包简介

A library which provide you with simple authentication.

README 文档

README

License Latest Stable Version Latest Unstable Version Build Status

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 7.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-06