2017 © Pedro Peláez
 

library sl-assertions

Assertions for Laravel 5.1

image

spotonlive/sl-assertions

Assertions for Laravel 5.1

  • Wednesday, December 9, 2015
  • by nikolajlovenhardt
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1,686 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Assertions for Laravel 5.1

Latest Stable Version Total Downloads Latest Unstable Version License Code Climate Test Coverage, (*1)

THIS PACKAGE IS UNDER DEVELOPMENT, (*2)

Configuration

Installation

Run $ composer require spotonlive/sl-assertions, (*3)

config/app.php Insert the provider and helper alias into your application configuration, (*4)

    'providers' => [
        \SpotOnLive\Assertions\Providers\Services\AssertionServiceProvider::class,
        \SpotOnLive\Assertions\Providers\Helpers\AssertionHelperProvider::class,
    ]

    'aliases' => [
        'AssertionHelper' => \SpotOnLive\Assertions\Facades\Helpers\AssertionHelperFacade::class,
    ]

Configuration

run $ php artisan vendor:publish to create the configuration file. A configuration file is now available in config/assertions.php., (*5)

Assertions

To create new examples create a new assertion file implementing the assertion interface. For example:, (*6)

EditAssertion.php, (*7)

<?php

namespace App\Assertions\Users;

use SpotOnLive\Assertions\AssertionInterface;
use App\Entities\User;

class EditAssertion implements AssertionInterface
{
    /**
     * @param User $user
     * @param array $data
     * @return bool
     */
    public function assert($user, array $data = [])
    {
        /** @var User $userToEdit */
        $userToEdit = $data['user'];

        return $user == $userToEdit || $user->hasRole(['superadmin', 'admin']);
    }
}

And then register the assertion in your configuration file:, (*8)

config/assertions.php, (*9)

<?php

return [
    'users.edit' => \App\Assertions\Users\EditAssertion::class,
];

Usage

Service

Use the assertion service by injecting AssertionService., (*10)

app::make('AssertionService'), (*11)

Example:, (*12)

<?php

namespace App\Controllers;

use \SpotOnLive\Assertions\Services\AssertionServiceInterface;

class Controller
{
    /** @var AssertionServiceInterface **/
    protected $assertionService;

    public function __construct(AssertionServiceInterface $assertionService)
    {
        $this->assertionService = $assertionService;
    }

    public function admin()
    {
        if (!$this->assertionService->isGranted('admin.page', Auth::user())) {
            return redirect()->route('not-granted');
        }

        return view('admin.page');
    }
}

Helper

Use the AssertionHelper directly in your views., (*13)

Example: view.blade.php, (*14)

@if(AssertionHelper::isGranted('user.edit', Auth::user(), ['user' => $user]))
    <a href="{{URL::route('user.edit')}}">{{_('Edit user')}}</a>
@endif

Organization & authors

The Versions

09/12 2015

dev-master

9999999-dev https://github.com/spotonlive/sl-assertions

Assertions for Laravel 5.1

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

laravel authorization assertions

18/09 2015
10/09 2015

v0.0.1

0.0.1.0 https://github.com/spotonlive/sl-assertions

Assertions for Laravel 5.1

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

laravel authorization assertions