2017 © Pedro Peláez
 

library laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

image

cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  • Wednesday, July 4, 2018
  • by a-komarev
  • Repository
  • 3 Watchers
  • 48 Stars
  • 4,317 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 11 Versions
  • 11 % Grown

The README.md

cog-laravel-ownership, (*1)

Discord Build StyleCI Releases License , (*2)

Introduction

Laravel Ownership simplify management of Eloquent model's owner. Group can be an owner of event, user can be an owner of chat room, organization can own licenses. It can be used for many cases not limited by authorship. Make any model as owner and create ownable models in a minutes!, (*3)

Contents

Features

  • Designed to work with Laravel Eloquent models
  • Using contracts to keep high customization capabilities
  • Each model can has owners of one type or use polymorphism
  • Option to auto-assigning current authenticated user on model creation as owner
  • Configurable auto-owner resolve strategy on model creation
  • Option to manually assign owner on model creation
  • Option to manually skip auto-assigning current user
  • Transfer ownership (change owner)
  • Make model orphaned (abandon owner)
  • Various ownership checks and query scopes
  • Following PHP Standard Recommendations:
  • Covered with unit tests

Installation

First, pull in the package through Composer., (*4)

composer require cybercog/laravel-ownership

Register Package Manually (optional)

If you disabled package auto-discovery you can register it manually., (*5)

Include the service provider within app/config/app.php., (*6)

'providers' => [
    Cog\Laravel\Ownership\Providers\OwnershipServiceProvider::class,
];

Usage

Laravel Ownership allows model to have strict owner model type (HasOwner trait) or use polymorphic relation (HasMorphOwner trait)., (*7)

Strict ownership is useful when model can belong to only one model type. Attempt to set owner of not defined model type will throw an exception InvalidOwnerType. Example: Only users allowed to create posts., (*8)

Polymorphic ownership is useful when model can belong to owners of different types. Example: Users and Organizations can upload applications to marketplace., (*9)

Prepare owner model

At the owner model use CanBeOwner contract and implement it:, (*10)

use Cog\Contracts\Ownership\CanBeOwner as CanBeOwnerInterface;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements CanBeOwnerInterface
{
    // ...
}

Prepare ownable model with strict ownership

Use Ownable contract in model which will get ownership behavior and implement it or just use HasOwner trait., (*11)

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;
}

Ownable model with strict ownership must have in database additional nullable column to store owner relation:, (*12)

Schema::table('articles', function (Blueprint $table) {
    $table->integer('owned_by_id')->unsigned()->nullable();

    $table->index('owned_by_id');
});

Overwrite strict ownership owner's foreign key

By default, owner model will be the same as config('auth.providers.users.model') provides., (*13)

To override default owner model in strict ownership, it's primary key or foreign key extend your ownable model with additional attributes:, (*14)

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    protected $ownerModel = Group::class;
    protected $ownerPrimaryKey = 'gid';
    protected $ownerForeignKey = 'group_id';
}

Prepare ownable model with polymorphic ownership

Use Ownable contract in model which will get polymorphic ownership behavior and implement it or just use HasMorphOwner trait., (*15)

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasMorphOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasMorphOwner;
}

Ownable model with polymorphic ownership must have in database additional nullable columns to store owner relation:, (*16)

Schema::table('articles', function (Blueprint $table) {
    $table->nullableMorphs('owned_by');
});

Available methods

Get owner relation

$article->ownedBy();
$article->owner();

Get model owner

$article->getOwner();
$article->ownedBy;
$article->owner;

Change (set) owner

$article->changeOwnerTo($owner);

Abandon (unset) owner

$article->abandonOwner();

Check if has owner

$article->hasOwner();

Check if owned by owner

$article->isOwnedBy($owner);

Check not owned by owner

$article->isNotOwnedBy($owner);

Manually define default owner on model creation

$article = new Article;
$article->withDefaultOwner()->save();

Will use resolveDefaultOwner() method under the hood., (*17)

Or provide concrete owner:, (*18)

$user = User::where('name', 'admin')->first();
$article = new Article;
$article->withDefaultOwner($user)->save();

Skip defining default owner on model creation

$article = new Article;
$article->withoutDefaultOwner()->save();

Scopes

Scope models by owner

Article::whereOwnedBy($owner)->get();

Scope models by not owned by owner

Article::whereNotOwnedBy($owner)->get();

Set authenticated user as owner automatically

To set currently authenticated user as owner for ownable model create - extend it with attribute withDefaultOwnerOnCreate. It works for both strict and polymorphic ownership behavior., (*19)

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    protected $withDefaultOwnerOnCreate = true;
}

To override strategy of getting default owner extend ownable model with resolveDefaultOwner method:, (*20)

use Cog\Contracts\Ownership\Ownable as OwnableInterface;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements OwnableInterface
{
    use HasOwner;

    public $withDefaultOwnerOnCreate = true;

    /**
     * Resolve entity default owner.
     * 
     * @return \Cog\Contracts\Ownership\CanBeOwner|null
     */
    public function resolveDefaultOwner()
    {
        return \App\User::where('name', 'admin')->first();
    }
}

Changelog

Please see CHANGELOG for more information on what has changed recently., (*21)

Upgrading

Please see UPGRADING for detailed upgrade instructions., (*22)

Contributing

Please see CONTRIBUTING for details., (*23)

Testing

Run the tests with:, (*24)

vendor/bin/phpunit

Security

If you discover any security related issues, please email open@cybercog.su instead of using the issue tracker., (*25)

Credits

@antonkomarev
Anton Komarev
@soap
Prasit Gebsaap

Laravel Ownership contributors list, (*26)

Alternatives

Feel free to add more alternatives as Pull Request., (*27)

License

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research the best solutions in product & software development is our passion., (*28)

CyberCog, (*29)

The Versions

04/07 2018

dev-master

9999999-dev https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

08/02 2018

5.1.0

5.1.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

13/09 2017

5.0.0

5.0.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

09/09 2017

4.0.0

4.0.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

30/08 2017

3.1.0

3.1.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

10/04 2017

3.0.0

3.0.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

07/02 2017

2.2.0

2.2.0.0 https://github.com/cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain eloquent trait copyright polymorphic creator morph maker cog cybercog master ownable ownership author owner proprietor owned

21/12 2016

2.1.0

2.1.0.0

Laravel Eloquent models ownership.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain trait copyright creator maker master ownable ownership author owner proprietor

17/12 2016

2.0.0

2.0.0.0

Laravel Eloquent models ownership.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain trait copyright creator maker master ownable ownership author owner proprietor

17/12 2016

1.0.1

1.0.1.0

Laravel Eloquent models ownership.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain trait copyright creator maker master ownable ownership author owner proprietor

17/12 2016

1.0.0

1.0.0.0

Laravel Eloquent models ownership.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel domain trait copyright creator maker master ownable ownership author owner proprietor