Skip to content
This repository has been archived by the owner on Jan 3, 2020. It is now read-only.

faustbrian-archives/laravel-doorkeeper

Repository files navigation

Laravel Doorkeeper

Build Status PHP from Packagist Latest Version License

This package counts the amount of records on a relationship and compares them with a given set of limits. This can be useful to determine if a user has reached the limit of files he can upload or something similar.

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require artisanry/doorkeeper

Usage

Model

<?php
namespace App;

use Artisanry\Doorkeeper\Traits\Doorkeeper;
use Artisanry\Doorkeeper\Contracts\DoorkeeperContract;

class User extends Model implements DoorkeeperContract
{
    use Doorkeeper;


    public $limits = [
        'posts' => 5,
        'files' => 10,
    ];

    public function posts() {
        return $this->hasMany('App\Post');
    }

    public function files() {
        return $this->hasMany('App\File');
    }
}

Middleware

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class ReachedLimits
{

    public function handle($request, Closure $next)
    {
        $user = Auth::check();

        if ( $user->limits($user->subscription->limits)->fails() ) {
            return redirect()->route('billing');
        }

        return $next($request);
    }
}

Controller

<?php
namespace App\Http\Controllers;

class DashboardController
{

    public function __construct()
    {
        $this->middleware('reachedLimits');
    }


    public function index()
    {
        return view('dashboard');
    }
}

Methods

Perform checks with custom limits.

$user->limits($user->subscription->limits)->passes();
$user->limits($user->subscription->limits)->fails();

Perform a check and see if it passes.

$user->passes();

Perform a check and see if it fails.

$user->fails();

Get all limits.

$user->allowed();

Get a specific limit.

$user->allowed('posts');

Get the current counter.

$user->current();

Get a specific counter.

$user->current('posts');

Check if the overall limit has been reached.

$user->maxed();

Check if a specific limit has been reached.

$user->maxed('posts');

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@basecode.sh. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

Mozilla Public License Version 2.0 (MPL-2.0).