decodelabs/disciple

Take control of your users

v0.4.4 2024-08-27 18:29 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Take control of your users

Disciple offers a set of simple interfaces that allows third party code to define reliable entry points to user state and data.

Get news and updates on the DecodeLabs blog.

Installation

Install via Composer:

composer require decodelabs/disciple

Usage

Importing

Disciple uses Veneer to provide a unified frontage under DecodeLabs\Disciple. You can access all the primary functionality via this static frontage without compromising testing and dependency injection.

Implementation

An implementation of Disciple revolves around an Adapter - this acts as the primary mediator between the Disciple Veneer frontage and your system's user management infrastructure.

namespace DecodeLabs\Disciple;

interface Adapter
{
    public function isLoggedIn(): bool;

    public function getIdentity(): ?string;
    public function getProfile(): Profile;

    public function isA(string ...$signifiers): bool;
}

Your adapter should be registered during your app's bootstrap process:

use DecodeLabs\Disciple;
use My\App\DiscipleAdapter;

Disciple::setAdapter(new DiscipleAdapter(
    $myUserManager
));

Then at any future point, queries can be made against the current user:

use DecodeLabs\Disciple;

if(Disciple::isLoggedIn()) {
    echo 'Yay, you\'re logged in';
} else {
    echo 'Boo, nobody loves me';
}

Profile

A registered Adapter should be able to provide an instance of a Profile, representing core data about the current user, such as name, email address, locale, etc.

namespace DecodeLabs\Disciple;

interface Profile
{
    public function getId(): ?string;
    public function getEmail(): ?string;
    public function getFullName(): ?string;
    public function getFirstName(): ?string;
    public function getSurname(): ?string;
    public function getNickName(): ?string;

    public function getRegistrationDate(): ?DateTime;
    public function getLastLoginDate(): ?DateTime;

    public function getLanguage(): ?string;
    public function getCountry(): ?string;
    public function getTimeZone(): ?string;

    public function getSignifiers(): array;
}

The Veneer frontage can interface directly with this profile information, allowing quick access of user data:

use DecodeLabs\Disciple;

if(Disciple::isLoggedIn()) {
    echo 'Hello '.Disciple::getFullName();
} else {
    echo 'You should probably log in first';
}

Client

An Adapter should also be able to provide a Client object which can report details of how a user is interfacing with the system.

Currently, that entails the following, but with more to follow in future versions:

namespace DecodeLabs\Disciple;

interface Client
{
    public function getProtocol(): string;
    public function getIpString(): string;
    public function getAgent(): ?string;
}

Signifiers

The Disciple interfaces define the concept of signifiers - string keys that users can be categorised and identified by.

It is the responsibility of the Adapter implementation to define how signifiers are stored and distributed, however the definition of this interface allows for a powerful, quick access mechanism for high level structures in your application.

if(Disciple::isA('admin')) {
    echo 'You can see the fun stuff';
} else {
    echo 'You should go home now';
}

Licensing

Disciple is licensed under the MIT License. See LICENSE for the full license text.