我试图在symfony2中创建一个简单的登录应用程序,我不明白。 我的路由文件:
shop_show_login_page:
path: /login
defaults: { _controller: ShopDesktopBundle:User:loginPage }
shop_login_user:
path: /loginUser
defaults: { _controller: ShopDesktopBundle:User:loginCheck }
我的控制器有这两种方法:
public function loginPageAction(){
return $this->render('ShopDesktopBundle:User:loginPage.html.twig');
}
public function loginCheckAction(Request $request){
$request = $this->get('request');
$password = $request->request->get('password');
$login = $request->request->get('username');
$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('ShopDesktopBundle:Customer');
$user = $repository->findOneBy(array('customer_login'=> $login, 'customer_password'=> $password));
if($user){
return $this->redirect($this->generateUrl('shop_desktop_homepage'));
}else{
return $this->render('ShopDesktopBundle:User:loginPage.html.twig',array('message_failed' => 'Error'));
}
}
我的表格:
<form action="{{ path('shop_login_user') }}" method="post">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" placeholder="Username" name="username">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="text" class="form-control" placeholder="Password" name="password">
</div>
</div>
<div class="form-group">
<button type="submit" class="button">Autentificare</button>
</div>
</form>
此系统在登录时工作正常,但问题出在layout.html.twig:
{% if app.user %}
<p>Test</p>
{% elseif not app.user %}
<span><a href="{{ path('shop_show_login_page') }}" style="color: #ffffff;">Login / Register</a></span>
{% endif %}
我的security.yml:
security:
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
encoders:
Symfony\Component\Security\Core\User\User: plaintext
# http://symfony.com/doc/current/book/security.html#hierarchical-roles
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
# the main part of the security, where you can set up firewalls
# for specific sections of your app
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
secured_area:
pattern: ^/
form_login:
check_path: shop_login_user
login_path: shop_show_login_page
logout:
path: _demo_logout
target: _demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
# with these settings you can restrict or allow access for different parts
# of your application based on roles, ip, host or methods
# http://symfony.com/doc/current/cookbook/security/access_control.html
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
所以我想在页面“Test”中看到用户是否已经使用succes进行身份验证,如果身份验证失败则登录/注册。我试过“if app.user”,但我不明白这个原则。
答案 0 :(得分:1)
有多种方法可以检查用户是否经过身份验证。
您尝试使用app.user
,就像这样:
{% if app.user is not empty %}
这将检查您的用户对象是否填充了数据。
您也可以使用is_granted()。
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
- 更新 -
您在评论中提供的错误可能是由于当您不在防火墙后面时尝试使用is_granted
功能。如果是这种情况,您可以在使用is_granted
致电if app.security.token is not empty
之前先检查是否有安全令牌。你会包含你的security.yml文件吗?
- 更新 -
您的安全文件需要正确设置才能使身份验证正常运行。默认行为设置为为您提供起点,但您必须配置防火墙。看一看,仔细阅读Symfony中关于Security的章节。