delboy1978uk / user
A persistable user object
Installs: 1 488
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^8.2
- delboy1978uk/bone-console: ^1.5
- delboy1978uk/person: ^6.6
- laminas/laminas-crypt: ^3.3
Requires (Dev)
- codeception/codeception: ^5.0
- codeception/module-asserts: ^3.0
- roave/security-advisories: dev-master
- dev-master
- v4.7.2
- v4.7.1
- v4.7.0
- v4.6.3
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.1
- v4.5.0
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.1
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-dev-master
This package is auto-updated.
Last update: 2024-11-06 17:58:26 UTC
README
A persistable User object and service for use with Doctrine.
installation
Install via composer into your project:
composer require delboy1978uk/user
setup
Add vendor/delboy1978uk/user/src/Entity
to your Doctrine entity paths and update your DB
usage
To create the user service, pass your Doctrine entity mananger and the delboy1978uk/person
Person service into the constructor:
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);
The User Service
All manipulation of our User objects happens through the UserService, which has a variety of methods available:
<?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
Registering a user
Pass in an array with keys email
, password
, and confirm
, confirm being the password confirmation field.
<?php $user = $svc->registerUser($data);
The user will be in an unactivated state. Usually we would email the user with an activation link. To get a secure token, do the following:
<?php $emailLink = $svc->generateEmailLink($user, 7); // Token expires in 7 days
You can now email your user, and use findEmailLink when they arrive to activate their account:
<?php $emailLink = $svc->findEmailLink($email, $token);
You can then update the users state to active and save.
The User Entity
Usage as follows.
<?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
The User lib also uses delboy1978uk/person
, which you can use to store some personal details of the user, if you like.
The User Collection
This is just a fancy array, extending Doctrine\Common\Collections\ArrayCollection
. It has the usual stuff:
<?php while ($collection->valid()) { $user = $collection->current(); // Do things to user $collection->next(); }
The User Repository
The Repository class is within the service, and contains all the database queries.
Query Criteria
You can use a UserCriteria to refine the results returned like so:
<?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);
You can also use findOneByCriteria
to restrict results to one row.