maize-tech / laravel-legal-consent
Laravel 法律同意
Requires
- php: ^8.0
- ext-json: *
- illuminate/database: ^9.0|^10.0|^11.0
- illuminate/http: ^9.0|^10.0|^11.0
- illuminate/routing: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
- vimeo/psalm: ^4.20|^5.22
README
Laravel 法律同意
轻松集成法律文件(如隐私政策、使用条款等)到您的应用程序中。
安装
您可以通过 composer 安装此包
composer require maize-tech/laravel-legal-consent
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="legal-consent-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="legal-consent-config"
这是发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Legal document model |-------------------------------------------------------------------------- | | Here you may specify the fully qualified class name of the legal document model. | */ 'legal_document_model' => Maize\LegalConsent\Models\LegalDocument::class, /* |-------------------------------------------------------------------------- | Legal consent model |-------------------------------------------------------------------------- | | Here you may specify the fully qualified class name of the legal consent model. | */ 'legal_consent_model' => Maize\LegalConsent\Models\LegalConsent::class, /* |-------------------------------------------------------------------------- | Legal document finder |-------------------------------------------------------------------------- | | Here you may specify the fully qualified class name of the legal document finder class. | */ 'legal_document_finder' => Maize\LegalConsent\DefaultLegalDocumentFinder::class, /* |-------------------------------------------------------------------------- | Route configurations |-------------------------------------------------------------------------- | | Here you may specify whether routes should be enabled or not. | You can also customize the routes prefix and middlewares. | */ 'routes' => [ 'enabled' => true, 'prefix' => 'legal', 'name' => 'legal', 'middleware' => ['api'], 'endpoints' => [ 'show' => [ 'middleware' => [], ], 'consent' => [ 'middleware' => ['auth:api'], ], ], ], /* |-------------------------------------------------------------------------- | Allowed document types |-------------------------------------------------------------------------- | | Here you may specify the list of accepted legal document types | for all requests. | */ 'allowed_document_types' => [ // ], /* |-------------------------------------------------------------------------- | Allowed acceptable values |-------------------------------------------------------------------------- | | Here you may specify the list of accepted values for each legal document | consent request. | */ 'allowed_acceptable_values' => [ 'yes', 'on', '1', 1, true, 'true', ], /* |-------------------------------------------------------------------------- | Cache |-------------------------------------------------------------------------- | | Here you may specify the amount of time, in seconds, where each legal | document is cached to avoid multiple database queries. | */ 'cache' => [ 'document_ttl' => 3600, 'document_user_ttl' => 3600, ], ];
用法
基本用法
要使用此包,将 Maize\LegalConsent\HasLegalConsent
特性添加到您想要处理的任何 Authenticatable 模型中。
以下是一个将 HasLegalConsent
特性添加到 User 和 Admin 模型的示例
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Maize\LegalConsent\HasLegalConsent; class User extends Authenticatable { use HasLegalConsent; protected $fillable = [ 'fist_name', 'last_name', 'email', ]; }
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Maize\LegalConsent\HasLegalConsent; class Admin extends Authenticatable { use HasLegalConsent; protected $fillable = [ 'fist_name', 'last_name', 'email', ]; }
完成之后,您必须通过将它们添加到 config/legal-consent.php
中的 allowed_document_types
列表来定义允许的文档类型列表。
'allowed_document_types' => [ 'privacy-policy', 'terms-of-use', ],
然后,您可以从数据库创建一个或多个文档,或者如果您愿意,您可以使用 CMS 处理创建。
以下是需要填写的字段
- type:文档类型名称
- body:文档内容
- noted:显示文档的附加说明
- published_at:给定文档的发布日期
假设我们创建一个于 2021-01-01 发布的隐私政策文档:以下是模型实体
$legalDocument = [ "id" => 1, "type" => "privacy-policy", "body" => "The privacy policy's very long text", "notes" => "", "published_at" => "2021-01-01", "updated_at" => "2021-01-01", "created_at" => "2021-01-01", ];
现在,您可以调用自定义 API 获取并接受当前文档,这些可以在 config/legal-consent.php
中自定义
GET - /legal/documents/privacy-policy
此端点使用给定的标准检索当前法律文档
- 文档类型必须是
privacy-policy
- published_at 日期必须早于
now()
然后,根据 published_at 日期对文档条目进行排序,以选择最新发布的文档。
响应包含文档 id(用于 POST 路由)以及所有对渲染有用的信息。以下是一个示例响应体
{ "data": { "id": 1, "type": "privacy-policy", "body": "The privacy policy's very long text", "notes": "", "published_at": "2021-01-01" } }
POST - /legal/documents/{id}
此端点存储当前认证用户对给定文档的同意。
法律文档监听器
您最终可以使用我们的监听器接受所有活动法律文档。
例如,当您的应用程序处理用户注册,并且他们必须通过复选框接受所有法律文档才能继续时,这很有用。
在这种情况下,您只需将监听器添加到 EventServiceProvider
中的 Registered
事件即可
use Maize\LegalConsent\Listeners\AcceptLegalDocumentListener; /** * The event listener mappings for the application. * * @var array */ protected $listen = [ Registered::class => [ AcceptLegalDocumentListener::class, // all currently active legal documents will be accepted SendEmailVerificationNotification::class, ], ];
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。