arjanwestdorp / exposable
一个用于安全方式暴露受保护模型的 Laravel 扩展包。
Requires
- php: >=7.0.13
- illuminate/support: ^5.4 || ^6.0
- league/uri: ^5.3
- nesbot/carbon: ^1.22 || ^2.0
Requires (Dev)
- laravel/framework: ^5.4.18
- mockery/mockery: ^0.9.5
- orchestra/database: ~3.4.0
- orchestra/testbench: ~3.4.7
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-09-05 19:55:19 UTC
README
这是一个用于安全方式暴露受保护模型的扩展包。你可能也遇到过这样的问题:你有一个存储在安全位置文件,你只想在用户登录或付费时暴露给他们。现在Laravel exposable将使这一过程变得更简单。
版本兼容性
安装
推荐通过 composer 安装 Exposable
composer require arjanwestdorp/exposable
接下来,你需要将服务提供者添加到你的 config/app.php
// config/app.php 'providers' => [ ... ArjanWestdorp\Exposable\ExposableServiceProvider::class, ];
现在你需要发布配置文件
php artisan vendor:publish --provider="ArjanWestdorp\Exposable\ExposableServiceProvider" --tag="config"
使用方法
将 Exposable
特性添加到你想要暴露的模型
namespace App; use ArjanWestdorp\Exposable\Exposable; use Illuminate\Database\Eloquent\Model; class File extends Model { use Exposable; }
接下来,你需要在模型上实现 expose
方法
namespace App; use ArjanWestdorp\Exposable\Exposable; use Illuminate\Database\Eloquent\Model; class File extends Model { use Exposable; /** * Expose the model. * * @return \Illuminate\Http\Response */ public function expose() { return response('My secure content'); } }
最后,你需要将模型添加到配置文件 config/exposable.php
并绑定到一个键
'exposables' => [ 'file' => App\File::class, ],
现在你的模型已经准备好暴露。只需使用 exposeUrl
方法即可获取模型可用的URL。
$file = File::first(); echo $file->exposeUrl(); // http://app.app/expose/file/1?expire=1483261800&guard=member&signature=716817ecaed63fa8b1b887b64ab9505d90cf065dc0677d8b011e3a8b014c43e0
配置
配置主要通过配置文件完成。尽管也可以在模型级别上进行偏差。
配置文件
以下是对配置文件中所有选项的解释。
key
用于签名暴露URL的密钥。默认情况下,它使用应用程序的Laravel密钥。
Default: config('app.key')
url-prefix
模型将暴露的URL的前缀。
Default: '/expose'
当暴露完整URL时,将看起来像
http://app.app/expose/file/1?expire=1483261800&guard=member&signature=716817ecaed63fa8b1b887b64ab9505d90cf065dc0677d8b011e3a8b014c43e0
中间件
在这里,你可以定义你想要包含在暴露URL中的应用程序的中间件。
Default: 'web'
lifetime
URL过期的时间。当给出一个整数时,时间以分钟为单位。任何有效的日期修改都可以使用,如 2 hours
、1 day
。有关所有允许的格式的详细信息,请参阅 https://php.ac.cn/manual/en/datetime.formats.relative.php。
Default: 10
exposables
包含所有你想要暴露的模型的数组。键用于在URL中检索相应的模型。
Default: []
guards
可以保护暴露项的守卫数组。这些与Laravel守卫不同。当您只想将模型暴露给已付费的认证用户时,自定义守卫非常有用。有关示例,请参阅自定义守卫。
Default: ['auth' => \ArjanWestdorp\Exposable\Guards\AuthGuard::class]
default-guard
用于检查模型是否可以暴露的默认守卫。如果需要,可以在暴露模型上覆盖此设置。如果设置为null,则不需要守卫检查。这只在require-guard
设置为false时有效。
Default: 'auth'
require-guard
定义是否在暴露模型时始终需要守卫。将此设置为false将允许您通过将default-guard
选项设置为null或模型上的$exposableGuard
来使用无守卫。
Default: true
模型配置
在模型上可以覆盖用于暴露的默认生命周期和守卫。
namespace App; use ArjanWestdorp\Exposable\Exposable; use Illuminate\Database\Eloquent\Model; class File extends Model { use Exposable; /** * The time the expose url will be valid. * * @var string */ protected $exposableLifetime = '2 hours'; /** * The guard to use when exposing this model. * * @var string */ protected $exposableGuard = 'member'; /** * Expose the model. * * @return \Illuminate\Http\Response */ public function expose() { return response('My secure content'); } }
自定义守卫
您可以为访问暴露的URL定义自己的自定义守卫。这些守卫需要在访问时进行检查。这些守卫需要实现ArjanWestdorp\Exposable\Guards\Guard
接口。使用自定义守卫的一个例子是检查用户不仅已认证,而且是成员。
namespace App\Guards; use ArjanWestdorp\Exposable\Guards\Guard; class MemberGuard implements Guard{ /** * Check if the user is authenticated and if he is a member. * * @return bool */ public function authenticate(){ if(!auth()->check()){ return false; } return auth()->user()->isMember(); } }
在配置文件config/exposable.php
中定义此守卫
'guards' => [ ... 'member' => \App\Guards\MemberGuard::class, ],
现在您已经准备好了,可以在配置文件中设置default-guard => 'member'
,或者在您的模型上设置protected $exposableGuard = 'member'
。
更新日志
有关最近更改的更多信息,请参阅更新日志
安全性
如果您发现任何安全问题,请通过电子邮件arjanwestdorp@gmail.com报告,而不是创建问题。
鸣谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅许可文件