uogsoe/hash-model-id 问题修复 & 功能扩展

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

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

uogsoe/hash-model-id

Composer 安装命令:

composer require uogsoe/hash-model-id

包简介

Very basic trait to add a HashID ->id_hash attribute to models

README 文档

README

This package gives you a very basic trait which will return a 6+char hashid for a $model->id_hash attribute. It's primarily for our use when doing error reporting to minimise leaking 'real' ids. Eg, could be useful for GDPR purposes when reporting to a 3rd party service like Rollbar.

The package is designed to be zero-config and simple to use - it's to cover our use-case of simply obfuscating the id's for reporting purposes. If you need something more configurable or secure - don't use it :-)

Installation

composer require uogsoe/hash-model-id

Usage

In your model (usually your App\User) add the CanHashUserIds trait :

<?php

namespace App;

use UoGSoE\ModelHashIds\CanHashUserIds;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    use CanHashUserIds;

    // ...
}

Then you can call $user->id_hash to get a unique hashid for that model. So for instance if you are using Rollbar, then you can set it to use the hash for the person attribute in the error report, eg :

// app/Exceptions/Handler.php

    public function report(Exception $exception)
    {
        if (Auth::check() && $this->shouldReport($exception)) {
            $user = Auth::user();
            \Log::error($exception->getMessage(), [
                'person' => ['id' => $user->id_hash]
            ]);
        }
        parent::report($exception);
    }

To decode the hash back into a models id, you'll have to do something like (in tinker for instance) :

(new UoGSoE\ModelHashIds\Hasher)->decode("the-hash");

There's deliberatly no 'easy' way of just doing $model->decode('the-hash') to reduce the chance of leakage.

Under the hood

The hashids are based on your .env APP_KEY string - so if you decide to change that then you will get different hash's back from the ->id_hash calls. The hashes are a minimum of six characters long as, well, basically six chars is easy to hit with a mouse when you're copy'n'pasting ;-)

The trait is just a very, very simple wrapper around the original hashids library for php - all credit to the original developers.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固