我想在后端创建一个新表单来创建新用户。我用这个函数创建了自己的AdminController:
public function createNewUsersEntity()
{
return $this->container->get('fos_user.user_manager')->createUser();
}
public function prePersistUsersEntity(User $user)
{
$this->get('fos_user.user_manager')->updatePassword($user);
$this->container->get('fos_user.user_manager')->updateUser($user, false);
}
public function preUpdateUsersEntity(User $user)
{
$this->get('fos_user.user_manager')->updatePassword($user);
$this->container->get('fos_user.user_manager')->updateUser($user, false);
}
我遇到的问题:密码未加密。
在我的config.yml文件中,我有这样的配置:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: AppBundle\Entity\User
use_listener: false
在我的security.yml文件中:
安全性: 编码器: FOS \ UserBundle \ Model \ UserInterface:bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
always_use_default_target_path: true
default_target_path: /admin
failure_path: /
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
这是我的用户实体中的构造:
public function __construct()
{
parent::__construct();
}
另一方面,当用户被添加到系统时,我需要使用ROLE_USER rol,但我不知道我可以做些什么来改变它。
两个问题:密码未加密且角色未定义。
答案 0 :(得分:2)
您在文档中找到的所有内容,我检查了项目中的所有说明,一切正常。
覆盖EasyAdminController方法(创建自己的控制器实现):
<?php
namespace AdminPanelBundle\Controller;
use JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController as EasyAdminController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
class AdminController extends EasyAdminController
{
/**
* @Route("/", name="easyadmin")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
return parent::indexAction($request);
}
public function createNewUserEntity()
{
return $this->get('fos_user.user_manager')->createUser();
}
public function prePersistUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
}
public function preUpdateUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
}
}
我的用户实体名称是“User”,如果您的名称不同,则更改方法名称。
将此代码添加到配置文件(config.yml):
easy_admin:
entities:
User:
class: AppBundle\Entity\User
form:
fields:
- username
- email
- enabled
- lastLogin
# if administrators are allowed to edit users' passwords and roles, add this:
- { property: 'plainPassword', type: 'text', type_options: { required: false } }
- { property: 'roles', type: 'choice', type_options: { multiple: true, choices: { 'ROLE_USER': 'ROLE_USER', 'ROLE_ADMIN': 'ROLE_ADMIN' } } }
现在打开您的routing.yaml,resources参数应指向新控制器:
admin_panel:
resource: "@AdminPanelBundle/Controller/"
type: annotation
prefix: /admin
我的代码在security.yml:
security:
encoders: #
AppNg\Symfony\AuthBundle\Entity\User: # Try add this...
algorithm: bcrypt #
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
admin:
pattern: ^/admin
logout:
path: /admin/logout
target: /admin/login
anonymous: ~
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
check_path: fos_user_security_check
always_use_default_target_path: true
default_target_path: '/admin'
csrf_token_generator: security.csrf.token_manager
access_control:
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout, role: ROLE_ADMIN }
- { path: ^/admin, role: ROLE_ADMIN }