friendsofbabba / core
CakePHP 的 FriendsOfBabba/Core 插件
Requires
- php: >=7.2
- cakephp/authentication: ^2.0
- cakephp/authorization: ^2.0
- cakephp/cakephp: ^4.2
- firebase/php-jwt: ^5.5
- friendsofcake/crud: ^6.0
- friendsofcake/search: ^6.2
- phpoffice/phpspreadsheet: ^1.23
- salines/cakephp4-soft-delete: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.5 || ^9.3
- dev-main
- 4.x-dev
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 1.x-dev
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-bugfix/jwt-algo
- dev-bugfix/jwt-token-provider-algo
This package is auto-updated.
Last update: 2024-09-28 14:50:39 UTC
README
为 CakePHP 4.x 应用程序快速开发插件,包括
- 基本数据库架构迁移以启用
- 用户
- 角色和权限
- 语言(用于客户端应用程序本地化)
- 基于 JWT 的基本认证
- 可选的基于 SPID(意大利身份提供者)的认证
- 基本命令行界面来管理数据
- 基本工作流工厂来创建和管理实体工作流
- 基本扩展器基础设施以扩展 FOB 核心实体和表
- 基本 CRUD 工厂,收集和管理每个配置实体的 CRUD
- 基本通知系统发送电子邮件和推送通知
- 基本媒体工具来管理媒体文件
- 等等... 🎁
安装
运行以下命令安装插件
composer require friendsofbabba/core bin/cake plugin load FriendsOfBabba/Core
由于我们将使用 REST 服务,我们可以禁用中间件堆栈中的 CSRF 令牌保护。为此,我们需要将以下行注释掉我们的新创建的 Application.php 中的以下行
// ->add(new CsrfProtectionMiddleware([ // 'httponly' => true, // ]));
生成用于 JWT 认证的私有和公共密钥
openssl genrsa -out config/jwt.key 1024 openssl rsa -in config/jwt.key -outform PEM -pubout -out config/jwt.pem chown -R www-data:www-data config/
在 config/app.php 中的 App 配置部分,您必须添加以下行
... 'App' => [ 'name' => 'Babbàpp', 'logo' => false, 'dashboard' => '/dashboard/index.html#/', ] ...
这些信息是处理电子邮件通知所必需的。
将 FriendsOfBabba/Core/Error/AppExceptionRenderer.php 添加到 config/app.php
'Error' => [ 'exceptionRenderer' => \FriendsOfBabba\Core\Error\AppExceptionRenderer::class, ],
要完成安装,您必须执行此命令
bin/cake install fob
配置 SPID
要配置 SPID(如果尚未安装),请将其安装到数据库级别
bin/cake migrations migrate --plugin FrinedsOfBabba/Core --source Migrations/Spid
通过在 config/app.php 中的配置级别添加新的扩展器来扩展 UserProfile 实体和表
use FriendsOfBabba\Core\Model\Entity\Extender\SpidUserProfileExtender; use FriendsOfBabba\Core\Model\Table\Extender\SpidUsersTableExtender; use FriendsOfBabba\Core\Model\Table\Extender\SpidUserProfilesTableExtender; ... 'Model' => [ 'Entity' => [ 'UserProfile' => SpidUserProfileExtender::class ], 'Table' => [ 'Users' => SpidUsersTableExtender::class 'UserProfiles' => SpidUserProfilesTableExtender::class ] ] ...
在 config/app.php 中配置 Google Recaptcha 密钥,该密钥将在首次登录后验证注册表单
'Recaptcha' => ['secret' => 'Your Secret']
作为最后一步,您必须在 config/app.php 中配置 SPID 授权配置
'Spid' => [ // Configure simplesaml remote service URL required to receive SPID callbacks. 'endpoint' => 'https://spid.local/simplesaml/login', 'service' => 'yourservice', 'secret' => 'yoursecret', 'expires' => 300, // Table from which to retrieve the user data. 'table' => 'FriendsOfBabba/Core.Users', // Configure SPID access methods and fields to use. // Finder matching code for SPID. 'finder' => 'UserProfiles.fiscal_code', // Joins required to retrieve data based on finder. 'joins' => ['UserProfiles'], // List of entity fields to retrieve. 'contain' => ['UserProfiles', 'Roles'], // List of default roles to add for new users. 'roles' => ['user'], // Configure back link to return to the application after SPID login. 'back' => [ 'client' => "https://:3000/#/login" ] ],
在大多数情况下,您需要修改上一代码的前 3 行。
请注意,根据不同环境,将 back 值更改为您的应用程序的正确 URL。
语言
该插件提供一组对客户端应用程序(如 react-admin)有用的语言文件,如果您想使用数据库而不是静态文件来处理消息。基本语言文件在执行 bin/cake install 命令时安装。
您可以使用 CLI 做很多事情
bin/cake langauge export:根据数据库中保存的数据生成新的语言文件。bin/cake language import:从现有文件(放置在应用程序根目录中)导入数据。bin/cake language clear_cache:清除缓存的语言文件以重新创建它,此命令在您更改数据库中的本地化消息时非常有用。
权限
权限模块允许您定义在应用程序内部工作所需的一组权限。权限依赖于控制器操作,并且始终通过扫描您应用程序中配置的所有控制器和操作来生成。
要刷新权限
bin/cake permission scan
重新生成角色权限列表。
媒体
您可以使用 MediaBehavior 来指示实体具有媒体字段。插件的基本安装自动配置了处理媒体文件所需的表,您只需在实体中实现 Media 即可。
您可以使用两种不同类型的媒体
- 使用
belongsTo设置单个媒体文件。 - 使用
belongsToMany设置多个媒体文件。
配置 belongsTo
添加以下代码引用媒体文件的列
alter table table_name add column media_id integer unsigned not null; alter table table_name add constraint fk_table_name_media_id foreign key (media_id) references media(id);
打开您的 Table 文件,并添加以下代码以映射媒体
... class TableNameTables extends BaseTable { ... public function initialize(array $config) { $this->addBehavior('FriendsOfBabba/Core.Media', ['media']); $this->belongsTo('Media', [ 'className' => 'FriendsOfBabba/Core.Media', 'foreignKey' => 'media_id', 'joinType' => 'LEFT', 'propertyName' => 'media' ]); } ... }
打开您的 Entity 文件,并添加一个新的可访问字段
protected $_accessible = [ ... 'media' => true, ... ];
配置 belongsToMany
创建一个 多对多 关联关系表
create table table_name_media ( table_name_id integer unsigned not null, media_id integer unsigned not null, primary key (table_name_id, media_id), foreign key (table_name_id) references table_name(id), foreign key (media_id) references media(id) );
打开您的 Table 文件,并添加以下代码以映射媒体
class TableNameTables extends BaseTable { ... public function initialize(array $config) { $this->addBehavior('FriendsOfBabba/Core.Media', ['media'])); $this->belongsToMany('MediaCollection', [ 'className' => 'FriendsOfBabba/Core.Media', 'foreignKey' => 'media_id', 'joinType' => 'LEFT', 'propertyName' => 'media_collection' ]); } ... }
打开您的 Entity 文件,并添加一个新的可访问字段
protected $_accessible = [ ... 'media_collection' => true, ... ];
工作流程(s)
在执行任何操作之前,您需要创建您的表。表必须遵循以下规则
- 必须创建、修改和删除(此字段需要为可空)字段(所有日期时间)
- 必须始终有一个 id 字段(整数无符号非空)
- 不能有这些名称的字段(为工作流程保留)
- 备注
- 状态
- 是私有
- 是当前
- 事务
要创建新的工作流程,您可以使用 CLI
bin/cake workflow create <EntityName> \ -s <List of states separated by comma> \ -t <List of routes in from:to state format separated by comma>
该命令将执行许多操作(可以单独执行)
- 创建实体模型、表和筛选器(
bin/cake entity create <EntityName>) - 创建实体事务 数据库表(
bin/cake workflow create_transaction_table <EntityName>) - 创建实体事务 蛋糕模型、表和筛选器(
bin/cake entity create <EntityName>Transactions) - 创建工作流程文件(
bin/cake workflow create_files <EntityName> -s <States> -t <Transitions>) - 创建实体 API 控制器(
bin/cake api create <EntityName>)
注意:在创建工作流程时,核心将自动将配置的实体作为资源按标准路径:api/<EntityName> 加载,使用资源的 短横线 版本(假设您为名为 ResearchProjects 的实体创建了一个工作流程,您将使用 api/research-projects 访问资源)。
数据迁移
在某些情况下,您需要将数据从一个数据库移动到另一个数据库。假设您正在实施一个新应用程序,并希望使用旧应用程序中的数据。
在这种情况下,您可以使用预定义的脚本来组织数据迁移。
使用 CLI,您可以创建新的数据迁移脚本
bin/cake data-migration create <NameOfMigration>
此命令将在 src/Command/DataMigration 目录中创建一个新的迁移脚本。您必须打开并编辑脚本后再执行它。
要执行迁移脚本
bin/cake data-migration execute <NameOfMigration>