可持久化的用户对象

v4.7.2 2024-05-06 16:54 UTC

README

build status Code Coverage Scrutinizer Code Quality
Doctrine使用的可持久化User对象和服务。

安装

使用composer将此包安装到您的项目中

composer require delboy1978uk/user

配置

vendor/delboy1978uk/user/src/Entity添加到您的Doctrine实体路径,并更新您的数据库

使用

要创建用户服务,将您的Doctrine实体管理器和delboy1978uk/person Person服务传递给构造函数

use Del\Person\Service\PersonService;
use Del\Service\UserService;
use Doctrine\ORM\EntityManager;

// $entityManager = [get your Doctrine EntityManager here];
$personService = new PersonService($entityManager);
$userService = new UserService($entityManager, $personService);

用户服务

所有对用户对象的操作都通过UserService进行,该服务提供多种方法

<?php

$user = $svc->createFromArray($data); // Pass an array, get a User object
$array = $svc->toArray($user); // Pass an User object, get an array
$user = $svc->saveUser($user); // Inserts or updates a User in the DB
$user = $svc->findUserById($id); // Finds a User in the DB
$user = $svc->findUserByEmail($email); // Finds a User in the DB
$user = $svc->changePassword($user, $password); // Updates a password in the DB
$users = $svc->findByCriteria($criteria); // See below for more info
$user = $svc->findOneByCriteria($criteria); // See below for more info
$svc->deleteUser($user); // Deletes a User from the DB
$svc->setUserClass($className); // If you wish to extend this class with your own
$svc->checkPassword($user, $plainPassword); // Returns true or false
$svc->registerUser($data); // See below
$svc->authenticate($email, $password); // Returns the user's ID on success
$emailLink = $svc->generateEmailLink($user, $daysTillExpiry); // For emailing with a secure token
$emailLink = $svc->findEmailLink($email, $token); // Finds the email link for that user
$emailLink = $svc->deleteEmailLink($link); // Deletes from the DB

注册用户

传入一个包含键emailpasswordconfirm的数组,其中confirm是密码确认字段。

<?php
$user = $svc->registerUser($data);

用户将处于未激活状态。通常我们会给用户发送一个激活链接的电子邮件。为了获取安全令牌,请执行以下操作

<?php
$emailLink = $svc->generateEmailLink($user, 7); // Token expires in 7 days

现在您可以给用户发送电子邮件,当用户到达并激活账户时使用findEmailLink

<?php
$emailLink = $svc->findEmailLink($email, $token); 

然后您可以将用户的状态更新为激活并保存。

用户实体

用法如下。

<?php

use Del\Entity\User;
use Del\Person\Entity\Person;
use Del\Value\User\State;

$user = new User();
$user->setId(12345); // You shouldn't have to, the ORM will do this
$user->setEmail('a@b.com');
$user->setPassword($password); // Not encrypted - use the service which will in turn call this 
$user->setState(new State(State::STATE_ACTIVATED)); 
$user->setRegistrationDate($registrationDate); // A DateTime object
$user->setLastLogin($registrationDate); // A DateTime object
$user->setPerson(new Person()); // See delboy1978uk/person

用户库还使用了delboy1978uk/person,如果您喜欢,可以使用它来存储用户的一些个人详细信息。

用户集合

这是一个扩展了Doctrine\Common\Collections\ArrayCollection的数组。它具有通常的功能

<?php

while ($collection->valid()) {
    $user = $collection->current();
    // Do things to user
    $collection->next();
}

用户仓库

仓库类在服务内部,包含所有的数据库查询。

查询条件

您可以使用用户条件来细化返回的结果,如下所示

<?php

use Del\Criteria\UserCriteria;
use Del\Value\User\State;

$criteria = new UserCriteria();
$criteria->setState(State::STATE_UNACTIVATED); // We only want unactivated users
$users = $svc->findByCriteria($criteria);

您还可以使用findOneByCriteria将结果限制为一行。