eddmash / permission-bundle
权限和角色包
dev-master / 1.0.x-dev
2020-09-23 21:35 UTC
This package is auto-updated.
Last update: 2024-09-24 06:57:16 UTC
README
安装
composer require eddmash/permission-bundle
配置
eddmash_permission:
user_entity: 'App\Entity\User' # the user entity used by the app
fetch_admin_callback: 'fetchAdmin' # the method in the specified `user_entity` repositoy
# to use when get the application root admin whose
# granted all permissions.
设置实体权限
使用 @AccessRights
添加特定模型可用的权限
<?php
namespace App\Entity;
use Eddmash\PermissionBundle\Entity\Annotations\AccessRights;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @AccessRights(label="Account", tag="account")
* @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
*/
class Account
{
/**
* @ORM\Column(type="string", length=32,nullable=true)
*/
private $account_number;
/**
* @ORM\Column(type="datetime")
*
*
* @Gedmo\Timestampable(on="create")
*/
private $opening_date;
}
创建权限
运行以下命令以在数据库中创建权限
php bin/console eddmash:permission
对于上面的 Account
实体,将在数据库中拥有 4 个权限
- account-can_add
- account-can_update
- account-can_view
- account-can_delete
检查权限
此权限可以与 is_granted
一起使用,通过将连字符替换为下划线,因此 account-can_view
变为 account_can_view
检查用户是否有权限
在控制器中,您可以使用 is_granted
<?php
class Account extends AbstractController
{
/**
* @Route("/account/{id}", name="account_information", methods={"GET"})
* @Security("is_granted('account_can_view')")
*/
public function detail(Request $request, Account $account)
{
}
}
在 twig 中检查
{% if is_granted('account_can_view') %}
<li>
<a href="{{ url('account_information', {'id':account.id}) }}">Account Information</a>
</li>
{% endif %}