承接 faaren-tech/faaren-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

faaren-tech/faaren-sdk

Composer 安装命令:

composer require faaren-tech/faaren-sdk

包简介

A sdk to interact with our authorization service

README 文档

README

Installation

composer require faaren-tech/faaren-sdk

// choose FaarenTech/AuthSdk/AuthSdkServiceProvider
php artisan vendor:publish

Customize the service_url parameter in config auth-sdk.php or set the value FAAREN_AUTH_SERVICE_URL in your .env file.

Usage

Important! If you use this library in a docker context FAAREN_AUTH_SERVICE_URL has to be the IP address of your machine. Otherwise it will try to find a host in its own docker network. As an alternative you can add an external_hosts option to your docker-compose-container.

You can use the package everywhere by simply initializing it:

use FaarenTech\FaarenSDK\FaarenSdk;

class SomeClass {
    
    public function someFunction()
    {
        $sdk = FaarenSdk::init("yourApiToken");
        $token = $sdk->apiToken()->details();
        $permissions = $sdk->apiToken()->permissions();
    }
    
}

The Token-Object contains the following details, for example:

{
    "uuid": "tok_22j8HPsYoOKXEMIPHNpHQeBdhKFP",
    "subsidiary_uuid": "subsi_3mz4r520mPr770ZUYtCRdJtFHz",
    "user_uuid": "null OR user_uuid",
    "name": "null OR name",
    "type": "APP_TOKEN OR PERSONAL_TOKEN",
    "permissions": [
        "user:delete",
        "apiToken:create",
        "apiToken:read",
        "apiToken:list",
        "user:update"
    ],
    "plainTextToken": "55|mySecretToken"
}

The listed permissions, subsidiary_uuid and user_uuid can be used to make your app-specific authorization.

Notifications

Mailings

You can simply trigger notifications via the Notification Service:

use FaarenTech\FaarenSDK\FaarenSdk;

class SomeClass {
    
    public function someFunction()
    {
        $sdk = FaarenSdk::init("yourApiToken");
        $mailing = $sdk
            ->notification()
            ->mail()
            ->setMailing('example')
            ->setMailData([
                "to" => "fabian@faaren.com",
                "from" => "example@faaren.com",
                "bcc" => "it@faaren.com",
                "whitelabel_config" => [
                    "foo" => "bar"
                ]
            ])
            ->send();
    }
    
}

If an error occurs while calling the Notification Service, a \FaarenTech\FaarenSDK\Exceptions\NotificationServiceException is thrown. The exception message will contain a hint what was going wrong, e.g. an validation error:

"message": "Status 422: Notification could not be sent because: The to field is required.",

The used tokens and endpoints can be customized via you .env file. The available keys are listed in /config/faaren-sdk.php within the vendor directory or, if you have published the config, in your applications config-folder.

Middlewares

AcceptsJsonMiddleware

HasValidTokenMiddleware

You can add the middleware group faaren to all routes you like. This middleware group contains the following middlewares:

  • HasValidTokenMiddleware => Checks if the given token is valid and attaches the token details to the current request. The details are available with $request->api_token.

Using this middleware makes it easy to work with tokens. You can access the token details everywhere via the request object:

// Route
// Assign middleware
Route::middleware('faaren')
    ->get('/your-endpoint', [SomeController::class, 'someAction']);

// Access in SomeController.php
class SomeController {
    public function someAction(\Illuminate\Http\Request $request) 
    {
        // The provided api token is valid
        // The ApiToken can be accessed via
        $token = $request->api_token;
    }
}

// Access in SomeFormRequest.php
class SomeFormRequest extends \Illuminate\Foundation\Http\FormRequest {
    public function authorize()
    {
        $token = $this->api_token;
    }
}

// Or everywhere via request() helper

Form Requests

This package provides a BaseRequest, the FaarenRequest. This Request implementes the Illuminate\Contracts\Auth\Access\Authorizable via the Illuminate\Foundation\Auth\Access\Authorizable trait. It can be used for any FormRequest you will create in your service.

This way it is possible to authorize incoming requests directly in your form request:

use FaarenTech\FaarenSDK\Request\FaarenRequest;

class YourRequest extends FaarenRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->can('index', SomeModel::class);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

In your policy you won't pass in an instance of your User. Instead you pass in an instance of your form request. The underlying policy:

class YourPolicy
{
    use HandlesAuthorization;

    public function index(TemplateIndexRequest $request)
    {
        // Here you can access the methods from the FaarenRequest
        // Access the AppToken via $request->api_token;
        return true;
    }

    public function show(TemplateIndexRequest $request, SomeModel $someModel)
    {
        return true;
    }
}

Define Resources

Since version 1.1.0 we use "ResponseCollection" and "ResourceCollection" in our Resource-definitions.

To use your own Resource with our Schema, you need to extend your Resource from "ResponseResource" and not from "JsonResource" Then you need to change your "toArray" method to "toPayload" Example:

class YourAwesomeResource extends ResponseResource
{
    /**
     * @return array[]
     */
    public function toPayload()
    {
        return [
            'awesome' => true
        ];
    }
}

Resource Collection:

So your Resource gets mapped as YourAwesomeResourceCollection

class YourAwesomeResourceCollection extends \FaarenTech\FaarenSDK\Resources\ResponseCollection
{
    public $collects = YourAwesomeResource::class;
}

Validation as Json in our Response-Schema

Your class needs to be extend from "FaarenRequest", and every validation error uses the schema

use FaarenTech\FaarenSDK\Request\FaarenRequest;

class ShowVehiclePoolRequest extends FaarenRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

}

Handle Exceptions as Json

Simple add the AcceptsJsonMiddleware to the relevant middleware groups or as a global middleware.

Using the custom Handler is deprecated and will cause errors when you use it!

faaren-tech/faaren-sdk 适用场景与选型建议

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

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

围绕 faaren-tech/faaren-sdk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-16