app-verk / user-bundle
Appverk用户包
3.2
2020-12-02 09:52 UTC
Requires
- php: ^7.2
- app-verk/components: ^2.0
- doctrine/doctrine-bundle: ^1.6 || ^2.0
- doctrine/orm: ^2.5
- egulias/email-validator: ^2.1
- incenteev/composer-parameter-handler: ^2.1
- jms/serializer-bundle: ^2.0 || ^3.3
- sensio/framework-extra-bundle: ^5.1
- stof/doctrine-extensions-bundle: ^1.2
- symfony/flex: ^1.1
- symfony/framework-bundle: 4.2.* || 4.4.*
README
为Symfony 3项目提供的简单轻量级用户包。支持用户和角色功能,带有访问控制列表(ACL)支持。
旧版本
如果您需要1.*分支的帮助,请访问v1.x文档。
安装
使用Composer安装包
$ php composer.phar require app-verk/user-bundle
配置
在AppKernel.php中注册包
// ./app/AppKernel.php
public function registerBundles()
{
$bundles = [
...
new AppVerk\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 AppVerk\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 AppVerk\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'); }
自定义访问解析器
在某些情况下,您需要创建自己的逻辑来决定对操作的访问。在这种情况下,您只需创建自定义访问解析器并放入您的逻辑
// ./src/AppBundle/Security/CustomAccessResolver.php ... use AppVerk\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