soft-passio / user-bundle
SoftPassio 用户包
3.1
2020-06-12 19:27 UTC
Requires
- php: ^7.2
- doctrine/orm: *
- egulias/email-validator: ^2.1
- jms/serializer-bundle: ^2.0 || ^3.3
- sensio/framework-extra-bundle: ^5.1
- soft-passio/components: ^2.0
- stof/doctrine-extensions-bundle: ^1.2
- symfony/flex: ^1.1
- symfony/framework-bundle: ^4.4
README
为 Symfony 3 项目提供的简单轻量级用户包。支持用户和角色功能,带有 ACL 支持。
旧版本
如果需要帮助1.*分支,请访问 v1.x 文档。
安装
使用 composer 安装包
$ php composer.phar require soft-passio/user-bundle
配置
在 AppKernel.php 中注册包
// ./app/AppKernel.php
public function registerBundles()
{
$bundles = [
...
new SoftPassio\UserBundle\UserBundle(),
...
];
}
添加新的配置文件,例如 user.yml
#./app/config/user.yml
user:
entities:
user_class: #E.g. AppBundle\Entity\User
acl:
enabled: #true|false defines to use or not to use ACL
access_denied_path: #route bame where user should be redirect when he dont have privileges to action
将 user.yml 文件导入 config.yml
imports:
...
- { resource: user.yml }
然后在您的包中创建两个实体(例如 AppBundle\Entity)
- 用户
<?php
namespace AppBundle\Entity;
use SoftPassio\UserBundle\Entity\User as AbstractUser;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User extends AbstractUser implements EntityInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
您可以使用您喜欢的配置格式(yml、xml、php 或注解)
运行
php bin/console doctrine:schema:update --force
现在您可以使用命令行创建管理员用户
php bin/console user:create:admin <username> <email> <password>
ACL
启用 ACL
#./app/config/user.yml
user:
acl
enabled: true
access_denied_path: #route name
使用注解定义受保护的操作
// ./src/AppBundle/Controller/DefaultController.php ... use SoftPassio\UserBundle\Annotation\AVSecurity; ... /** * ... * @AVSecurity(allow={"ROLE_ADMIN"}, disallow={"ROLE_X"}, name="list", group="default") */ public function listAction() { return $this->render('@App/controller/user/list.html.twig'); }
自定义 AccessResolver
在某些情况下,您需要创建自己的逻辑来决定对操作的访问。在这种情况下,您只需创建自定义 accessResolver 并放入您的逻辑
// ./src/AppBundle/Security/CustomAccessResolver.php ... use SoftPassio\UserBundle\Security\AccessResolverInterface; ... class SimpleAccessResolver implements AccessResolverInterface { public function resolve(RoleableInterface $user, $action): bool { // your own logic } }
将新的解析器插入配置文件
#./app/config/user.yml
user:
entities:
user_class: #E.g. AppBundle\Entity\User
acl:
enabled: #true|false defines to use or not to use ACL
access_denied_path: #route bame where user should be redirect when he dont have privileges to action
access_resolver_class: AppBundle\Security\CustomAccessResolver