elenyum/authorization
Composer 安装命令:
composer require elenyum/authorization
包简介
This bundle oauth authorization in controller.
关键字:
README 文档
README
ElenyumAuthorizationBundle provides a mechanism for creating the User entity, configuring user authorization, and adding attributes to controllers to manage access to methods.
Installation
Install the package using Composer:
composer require elenyum/authorization
Requirements
This package requires the following dependencies:
- PHP >= 8.1
- Symfony components:
symfony/console^5.4|^6.0|^7.0symfony/framework-bundle^5.4.24|^6.0|^7.0symfony/options-resolver^7.0symfony/property-info^7.0symfony/validator^7.0
zircote/swagger-php^4.2.15lexik/jwt-authentication-bundlev3.1.0
Configuration
No additional configuration is required. However, before use, you need to add configuration to doctrine.yaml to activate entity mapping:
doctrine: orm: mappings: ElenyumAuthorizationBundle: is_bundle: true alias: ElenyumAuthorizationBundle
Then run migrations to create the necessary tables:
php bin/console doctrine:migrations:migrate
Using the Auth Attribute
This package adds the Auth attribute, which can be used in controllers to restrict access:
use Elenyum\Authorization\Attribute\Auth; use App\Entity\Figure; #[Auth(name: 'Bearer', model: Figure::class)] public function someAction() { // Action logic }
name: The name of the authorization method (used in documentation).model: The entity class to which the access restriction will be applied based on roles.
Configuring Business Logic Access with Voter
For more flexible access rules to entities, it is recommended to use a Voter in Symfony. This allows you to implement checks that go beyond basic role verification and can consider additional business rules, such as restricting access to records created by the current user.
Example of creating a Voter to check record ownership:
namespace App\Security\Voter; use App\Entity\Figure; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\User\UserInterface; class FigureVoter extends Voter { private $security; public function __construct(Security $security) { $this->security = $security; } protected function supports(string $attribute, $subject): bool { return in_array($attribute, ['VIEW', 'EDIT']) && $subject instanceof Figure; } protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool { $user = $token->getUser(); if (!$user instanceof UserInterface) { return false; } // Check if the user is the owner of the record return $subject->getOwnerId() === $user->getId(); } }
Applying Voter
To use the Voter, call it via isGranted in the controller or configure the attribute for verification:
if (!$this->isGranted('EDIT', $figure)) { throw $this->createAccessDeniedException('Access denied.'); }
Using a Voter helps separate business access logic from the main authorization mechanism, adhering to the single responsibility principle and improving code readability and scalability.
elenyum/authorization 适用场景与选型建议
elenyum/authorization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auto」 「creator」 「generated-code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elenyum/authorization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elenyum/authorization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elenyum/authorization 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fork of greenlion/php-sql-parser made because we were having trouble getting a production needed fix accepted and merged into their repo. A pure PHP SQL (non validating) parser w/ focus on MySQL dialect of SQL
Auto deployment module for Zend Framework 2
Service auto install for Nette Framework
Laravel service creator
Add a factory management for Laramore
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-07