定制 arjanwestdorp/exposable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

arjanwestdorp/exposable

Composer 安装命令:

composer require arjanwestdorp/exposable

包简介

A laravel package to expose protected model in a secure way.

README 文档

README

Latest Stable Version License Build Status Quality Score Coverage StyleCI

This is a package to expose your protected models in a secure way. You maybe also ran into the problem that you have a file stored in a secure location and you only want to expose it to your users when they are logged in or payed for it. Laravel exposable will make this much easier for you now.

Version Compatibility

Laravel Exposable
5.3 1.0.x
5.4 1.1.x

Installation

The recommended way to install Exposable is through composer:

composer require arjanwestdorp/exposable

Next, you'll have to add the service provider to your config/app.php

// config/app.php
'providers' => [
    ...
    ArjanWestdorp\Exposable\ExposableServiceProvider::class,
];

Now you'll need to publish the config file:

php artisan vendor:publish --provider="ArjanWestdorp\Exposable\ExposableServiceProvider" --tag="config"

Usage

Add the Exposable trait to the model(s) you want to expose:

namespace App;
    
use ArjanWestdorp\Exposable\Exposable;
use Illuminate\Database\Eloquent\Model;
    
class File extends Model {
    
    use Exposable;

}

Next, you will need to implement the expose method on your model:

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');
    }
}

Finally you'll need to add the model to the config file config/exposable.php and bind it to a key:

'exposables' => [
    'file' => App\File::class,
],

Now your model is ready to expose. Simply use the exposeUrl method to get the url on which the model will be available.

$file = File::first();
echo $file->exposeUrl();
    
// http://app.app/expose/file/1?expire=1483261800&guard=member&signature=716817ecaed63fa8b1b887b64ab9505d90cf065dc0677d8b011e3a8b014c43e0

Configuration

Configuration is mainly done through the config file. Although there is the option to deviate on model level.

Config file

Below an explanation of all options in the config file.

key

The key which is used to sign the expose urls. By default it uses the Laravel key of you application.

Default: config('app.key')

url-prefix

The prefix of the url on which the models will be exposed.

Default: '/expose'

When exposing the complete url would look like:

http://app.app/expose/file/1?expire=1483261800&guard=member&signature=716817ecaed63fa8b1b887b64ab9505d90cf065dc0677d8b011e3a8b014c43e0

middleware

Here you can define middleware of your application you want to include for the expose url.

Default: 'web'

lifetime

The time after which the url expires. When an integer is given the time is in minutes. Any valid date modification can be used like 2 hours, 1 day. See http://php.net/manual/en/datetime.formats.relative.php for all allowed formats.

Default: 10

exposables

Array containing all the models you want to expose. The key is used in the url to retrieve the corresponding model.

Default: []

guards

Array of guards which can protect the exposables. These are NOT the same as the Laravel guards. A custom guard can be very usefull when you want to expose a model only to authenticated users that have payed for the content for example. See Custom guards for an example.

Default: ['auth' => \ArjanWestdorp\Exposable\Guards\AuthGuard::class]

default-guard

The default guard that is used to check if the model can be exposed. You can override this settings on the exposable model if needed. Set to null if you don't want a guard check. That only works when the require-guard is set to false.

Default: 'auth'

require-guard

Define if a guard is always required when exposing a model. Settings this to false will give you the option to use no guard by setting the default-guard option to null or $exposableGuard on a model.

Default: true

Model configuration

On a model you can override the default lifetime and the guard which are used to expose:

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');
    }
}

Custom guards

You can define your own custom guards which will be checked when accessing the expose url. These guards will need to implement the ArjanWestdorp\Exposable\Guards\Guard interface. An example of using a custom guard can be when checking if a user is not only authenticated, but also a member:

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();
    }
}

Define this guard in the config/exposable.php config file:

'guards' => [
    ...
    'member' => \App\Guards\MemberGuard::class,
],

Now you're good to go and set either the default-guard => 'member' in your config file or set the protected $exposableGuard = 'member' on your model.

Changelog

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

Security

If you discover any security issues, please email arjanwestdorp@gmail.com instead of creating an issue.

Credits

License

The MIT License (MIT). Please see License File for more information.

arjanwestdorp/exposable 适用场景与选型建议

arjanwestdorp/exposable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.88k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 arjanwestdorp/exposable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-02