New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

malhal/laravel-createdbypolicy

Simple security for Laravel models
8

Larablocks Analysis

malhal/laravel-createdbypolicy works with Laravel 5 (latest release v1.0.1, Oct 4, 2016), which reached end of life: no more fixes, even for security.

Recommendation checks

Not advised

Not advised: no release or commit in over 2 years.

  • No release or commit in over 2 years
  • Supports only Laravel 5, past end of life
  • No release in the last 12 months (last: Oct 4, 2016)
  • 0 downloads a month (needs 10,000)
  • 0 GitHub stars (needs 100)

State

Likely abandoned
  • No release or commit in over 2 years.
  • Last stable release: Oct 4, 2016.
  • No releases in the last 12 months.
  • First released: Sep 13, 2016.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 27, 2026.

Install
composer require malhal/laravel-createdbypolicy
Latest Version:v1.0.1
PHP:>=5.6.4
Laravel:5
License:MIT
Last Updated:Oct 4, 2016
Links: GitHub  ·  Packagist
Maintainer: malhal

Laravel-CreatedByPolicy

A trait that implements authorization using simple booleans in your policy classes. It provides create, write and read modes and world, authenticated and creator user-based security.

It requires Laravel-CreatedBy which provides the functionality of tracking which user created and updated models, so please follow its setup instructions including adding the table columns and relations for the created_by_id and updated_by_id.

Configuration

Enter the standard artisan command to make a policy (replacing the name with your model name):

php artisan make:policy VenuePolicy

As normal, add the policy to AuthServiceProvider policies array, e.g.

protected $policies = [
    'App\Venue' => 'App\Policies\VenuePolicy',
];   

Now for something different, in your project open the generated policy class and add the following trait:

use CreatedByPolicy;

The default security params are shown in the CreateByPolicy trait php file for easy access, and look like this:

// const WORLD_CREATE = false;
// const WORLD_READ = true;
// const WORLD_WRITE = false;
// const AUTHENTICATED_CREATE = true;
// const AUTHENTICATED_READ = false;
// const AUTHENTICATED_WRITE = false;
// const CREATOR_READ = false;
// const CREATOR_WRITE = true;

You can override the default security levels by implementing the constants, e.g. to prevent guests from reading:

const WORLD_READ = true;

Now you can use Laravel's built-in authorization methods as normal, exceptions will be thrown in the case of no access, and for creatorRead a global scope will be added to limit queries to only records created by the currently authenticated user, for example:

protected function create(Request $request){
    $this->authorize('create', Venue::class);
    //
}

Laravel has a limitation that policies only works if there is a user, since our policies include world read/write (guest access) we need to workaround it with a provider. In config/app.php add to the providers array:

Malhal\CreatedByPolicy\WorldServiceProvider::class

All this does is if Auth::user() is null it sets it to a new User which has no ID. This means that Auth::check() will no longer work as designed, and instead you have to check for guest with is_null(Auth::user()->getKey()).

You shouldn't need to but just in case, disable security checking on a query that alreay had the global scope applied remove it with:

$query->withoutCreatedByPolicy()

There is an issue when using the CreatedBy trait on the User model with auto-increment. MySQL cannot insert a new record and include a foreign key to the newly created ID. This issue doesn't occur if using UUID for keys, however you can still use the policy with auto-increment by implementing this method in your User model, which assumes user records are always created by the user:

public function getCreatedByForeignKey(){
    return $this->getKey();
}

Installation

PHP 5.6.4+ and Laravel 5.3+ are required.

To get the latest version of Laravel CreatedByPolicy, simply require the project using Composer:

$ composer require malhal/laravel-createdbypolicy dev-master

Alternatives to malhal/laravel-createdbypolicy

spatie/laravel-permission

Permission handling for Laravel 12 and up

141,297,795 13,001
mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

23,909,128 1,976
yajra/laravel-acl

Laravel ACL is a simple role, permission ACL for Laravel Framework.

156,001 113
alkhwlani/xss-middleware

A Laravel Middleware to filter user inputs from XSS and iframes and other embed...

536,631 5
kodeine/laravel-acl

Light-weight role-based permissions for Laravel 5 built in Auth system.

368,467 774

Version History

Version Released PHP Laravel License
v1.0.1 >=5.6.4 5.3.* MIT
v1.0.0 >=5.6.4 5.3.* MIT