curly-deni/laravel-permission-model-attributes 问题修复 & 功能扩展

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

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

curly-deni/laravel-permission-model-attributes

最新稳定版本:v1.0.1

Composer 安装命令:

composer require curly-deni/laravel-permission-model-attributes

包简介

Add permission-aware attributes and static checks to Eloquent models using Laravel's authorization policies.

README 文档

README

Latest Version on Packagist Code Style Total Downloads

Laravel Permission Model Attributes adds permission-aware properties to your Eloquent models using native Laravel authorization. It provides a convenient way to check and expose model-level permissions through dynamic attributes like updatable and deletable, based on policies or internal rules.

✨ Features

  • ✅ Adds updatable and deletable model attributes
  • 🧠 Static permission checks: create, read, update, delete
  • 🔐 Seamless integration with Laravel’s native authorization system (policies)
  • ⚡ Attribute caching for performance
  • 🧩 Optional interface for strict typing

📦 Installation

Install via Composer:

composer require curly-deni/laravel-permission-model-attributes

Publish the config file:

php artisan vendor:publish --tag="permission-model-attributes-config"

⚙️ Configuration

return [
    'create' => true,
    'update' => true,
    'delete' => true,
    'read' => false,
];

Each flag enables permission checks for the corresponding action:

Key Description
create Enables permission check via create() policy method
read Enables permission check via read() policy method
update Enables permission check via update() policy method
delete Enables permission check via delete() policy method

✅ Only the enabled actions will be checked. If disabled, permission checks are skipped entirely.

🛡 Policy Integration

The package automatically uses Laravel’s policy system. You must define policy methods only for the enabled actions.

✏ Define Policy Methods

class PostPolicy
{
    public function create(User $user)
    {
        return $user->hasPermission('create-posts');
    }

    public function update(User $user, Post $post)
    {
        return $user->id === $post->user_id;
    }

    public function delete(User $user, Post $post)
    {
        return $user->id === $post->user_id;
    }

    public function read(User $user)
    {
        return $user->hasPermission('read-posts');
    }
}

⚠️ Note: The read() method accepts only the User object — no model instance is passed.

🚀 Usage

1. Add the Trait

use Aesis\PermissionModelAttributes\Traits\HasPermissionAttributes;

class Post extends Model
{
    use HasPermissionAttributes;
}

2. (Optional) Implement the Interface

use Aesis\PermissionModelAttributes\Contracts\PermissionAttributes;

class Post extends Model implements PermissionAttributes
{
    use HasPermissionAttributes;
}

3. Access in Code

$post = Post::find(1);

if ($post->updatable) {
    // show edit button
}

if (Post::isCreatableStatic()) {
    // show "New" button
}

📘 Trait Reference

Method / Attribute Type Description
updatable Attribute (bool) Returns true if the model can be updated
deletable Attribute (bool) Returns true if the model can be deleted
isUpdatable() Method Instance-level permission check for update
isDeletable() Method Instance-level permission check for delete
isCreatableStatic() Static Class-level permission check for create
isReadableStatic() Static Class-level permission check for read
isUpdatableStatic() Static Class-level permission check for update
isDeletableStatic() Static Class-level permission check for delete

👤 Author

📝 License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-03