cybercog/laravel-ownership 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cybercog/laravel-ownership

Composer 安装命令:

composer require cybercog/laravel-ownership

包简介

Laravel Ownership simplify management of Eloquent model's owner.

README 文档

README

cog-laravel-ownership

Discord Build 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

Register Package Manually (optional)

If you disabled package auto-discovery you can register it manually.

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

Polymorphic ownership is useful when model can belong 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 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.

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:

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

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:

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.

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 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:

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.

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 open@cybercog.su instead of using the issue tracker.

Credits

@antonkomarev
Anton Komarev
@soap
Prasit Gebsaap

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 the best solutions in product & software development is our passion.

CyberCog

cybercog/laravel-ownership 适用场景与选型建议

cybercog/laravel-ownership 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.16k 次下载、GitHub Stars 达 90, 最近一次更新时间为 2016 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「trait」 「laravel」 「domain」 「maker」 「eloquent」 「polymorphic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cybercog/laravel-ownership 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cybercog/laravel-ownership 我们能提供哪些服务?
定制开发 / 二次开发

基于 cybercog/laravel-ownership 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 29.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 90
  • 点击次数: 20
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 90
  • Watchers: 3
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-17