All Projects → cybercog → Laravel Ownership

cybercog / Laravel Ownership

Licence: mit
Laravel Ownership simplify management of Eloquent model's owner.

Projects that are alternatives of or similar to Laravel Ownership

Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (+33.8%)
Mutual labels:  eloquent, laravel, package, trait
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (+739.44%)
Mutual labels:  eloquent, laravel, package, trait
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (+705.63%)
Mutual labels:  eloquent, laravel, package, trait
Laravel Nullable Fields
Handles saving empty fields as null for Eloquent models
Stars: ✭ 88 (+23.94%)
Mutual labels:  eloquent, laravel, package
Schedule
Schedule is a package that helps tracking schedules for your models. If you have workers in a company, you can set schedules for them and see their availability though the time.
Stars: ✭ 155 (+118.31%)
Mutual labels:  eloquent, laravel, trait
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (+70.42%)
Mutual labels:  eloquent, laravel, package
Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (+359.15%)
Mutual labels:  eloquent, laravel, package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+178.87%)
Mutual labels:  eloquent, laravel, package
Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (+601.41%)
Mutual labels:  eloquent, laravel, package
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+778.87%)
Mutual labels:  eloquent, laravel, package
Eloquent Sortable
Sortable behaviour for Eloquent models
Stars: ✭ 914 (+1187.32%)
Mutual labels:  eloquent, laravel, trait
Laravel Imageup
Auto Image & file upload, resize and crop for Laravel eloquent model using Intervention image
Stars: ✭ 646 (+809.86%)
Mutual labels:  eloquent, laravel, trait
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (-8.45%)
Mutual labels:  eloquent, laravel, package
Picasso
Laravel Image Management and Optimization Package
Stars: ✭ 70 (-1.41%)
Mutual labels:  laravel, package
Laravel Dropbox Driver
A storage extension for Dropbox.
Stars: ✭ 42 (-40.85%)
Mutual labels:  laravel, package
Laravel Tags
Add tags and taggable behaviour to your Laravel app
Stars: ✭ 1,026 (+1345.07%)
Mutual labels:  eloquent, laravel
Laravel Packager
A cli tool for creating Laravel packages
Stars: ✭ 1,049 (+1377.46%)
Mutual labels:  laravel, package
Freelancers Market
Laravel Project to help freelance websites clients and freelancers to find each other.
Stars: ✭ 39 (-45.07%)
Mutual labels:  eloquent, laravel
History Tracker
Laravel Model history tracking made easy
Stars: ✭ 46 (-35.21%)
Mutual labels:  laravel, trait
Laravel Graphql
GraphQL implementation with power of Laravel
Stars: ✭ 56 (-21.13%)
Mutual labels:  eloquent, laravel

THIS PACKAGE IS NOT MAINTAINED ANYMORE

cog-laravel-ownership-3

Build Status StyleCI Releases License

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!

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.

$ composer require cybercog/laravel-ownership

And then include the service provider within app/config/app.php.

'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).

Strict ownership is useful when model can belongs 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.

Polymorphic ownership is useful when model can belongs to owners of different types. Example: Users and Organizations can upload applications to marketplace.

Prepare owner model

At the owner model use CanBeOwner contract and implement it:

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

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

Prepare ownable model with strict ownership

Use Ownable contract in model which will get ownership behavior and implement it or just use HasOwner trait.

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

class Article extends Model implements OwnableContract
{
    use HasOwner;
}

Ownable model with strict ownership must have in database additional nullable column to store owner relation:

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.

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

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

class Article extends Model implements OwnableContract
{
    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.

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

class Article extends Model implements OwnableContract
{
    use HasMorphOwner;
}

Ownable model with polymorphic ownership must have in database additional nullable columns to store owner relation:

Laravel 5.3.29 and newer

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

Laravel 5.3.28 and older

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

    $table->index([
        'owned_by_id',
        'owned_by_type',
    ]);
});

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.

Or provide concrete owner:

$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.

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

class Article extends Model implements OwnableContract
{
    use HasOwner;

    protected $withDefaultOwnerOnCreate = true;
}

To override strategy of getting default owner extend ownable model with resolveDefaultOwner method:

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

class Article extends Model implements OwnableContract
{
    use HasOwner;

    public $withDefaultOwnerOnCreate = true;

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

Changelog

Please see CHANGELOG for more information on what has changed recently.

Upgrading

Please see UPGRADING for detailed upgrade instructions.

Contributing

Please see CONTRIBUTING for details.

Testing

Run the tests with:

$ vendor/bin/phpunit

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

@antonkomarev
Anton Komarev

Laravel Ownership contributors list

Alternatives

Feel free to add more alternatives as Pull Request.

License

About CyberCog

CyberCog is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion.

CyberCog

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].