定制 cpliakas/doctrine-password 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

cpliakas/doctrine-password

最新稳定版本:1.0.0

Composer 安装命令:

composer require cpliakas/doctrine-password

包简介

Adds a password type to Doctrine that hashes and compares passwords using the phpass library.

README 文档

README

This project provides a password type for Doctrine that automatically hashes passwords using the PHP Password Library and provides a helper method to compare them to raw data submitted by end users. The primary goal is to make it stupid-simple to store hashed passwords in a database and check if passwords submitted by end users are valid.

Installation

This library can be installed with Composer. Define the following requirement in your project's composer.json file:

{
    "require": {
        "cpliakas/doctrine-password": "*"
    }
}

Then follow Composer's Installation / Usage guide to install this library.

Usage

This library assumes that the developer is familiar with Doctrine ORM, otherwise the code snippets below won't make much sense.

First, define your entity. Use the "password" type for the column storing passwords:

<?php
// src/User.php

/** @Entity **/
class User
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /** @Column(length=255, unique=true, nullable=false) **/
    private $email;

    /** @Column(type="password", nullable=false) **/
    private $password;

    public function setEmail($email)
    {
        $this->email = $email;
    }
    
    public function setPassword($password)
    {
        $this->password = $password;
    }
    
    public function getPassword()
    {
        $return this->password;
    }
}

Then write your code to obtain the EntityManager, and register the password type:

<?php
// bootstrap.php

use Doctrine\DBAL\Types\Type;

require_once 'vendor/autoload.php';

// .. (code to obtain the entity manager, refer to the Doctrine docs)

Type::addType('password', 'Cpliakas\Password\Doctrine\PasswordType');

Next, configure the command line tool and use it to create your schema:

php vendor/bin/doctrine orm:schema-tool:create

Now you are ready to add a user to the system. In the example below we will set the raw password, and the library will automatically hash it when written to the database.

<?php

// Replace with your own project's bootstrap file.
require_once 'bootstrap.php';

// Replace with your project's mechanism to retrieve the EntityManager.
$em = GetEntityManager();

$user = new User();
$user
    ->setEmail('myuser@example.com')
    ->setPassword('mypassword')
;

$em->persist($user);
$em->flush();

The password is now stored as a hash in the database. When retrieving the user from the database, the password is returned as an object that contains a helper method to compare raw passwords submitted by end users:

<?php

// Replace with your own project's bootstrap file.
require_once 'bootstrap.php';

// Replace with your project's mechanism to retrieve the EntityManager.
$em = GetEntityManager();

$repository = $em->getRepository('User');
$user = $repository->findOneBy(array('email' => 'myuser@example.com'));

// Returns true.
$user->getPassword()->match('mypassword');

// Returns false.
$user->getPassword()->match('badpassword');

统计信息

  • 总下载量: 9.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2013-06-16