2015-11-13 31 views
1

我得到了问题:我想允许用户和匿名查看网站,并且只允许用户采取某些行动(我已被覆盖)。事情是,某些路径(/帐户等)应该只能访问记录的用户。 我想真的很难配置我secure.php但是,无论是匿名可以访问/账户或我不能在任何地方访问登录的用户除/帐号/ ...允许用户和匿名查看网站,但保护其中的一部分

都尝试:

$app['security.firewalls'] = array(
'secured' => array(
    'pattern' => '/account', 
    'form' => array('login_path' => '/login', 'check_path' => '/account/login_check'), 
    'logout' => array('logout_path' => '/account/logout', 'invalidate_session' => true), 
    'users' => $app->share(function() use ($app) { 
     return new UserProvider($app['db']); 
    }), 
), 
'unsecured' => array(
    'pattern'=> '/', 
    'anonymous' => true, 
) 
); 

$app['security.firewalls'] = array(
'secured' => array(
    'pattern' => '/account', 
    'anonymous'=> true, 
    'form' => array('login_path' => '/login', 'check_path' => '/account/login_check'), 
    'logout' => array('logout_path' => '/account/logout', 'invalidate_session' => true), 
    'users' => $app->share(function() use ($app) { 
     return new UserProvider($app['db']); 
    }), 
), 

); 

回答

0

您需要在授权步骤中这样做,因此您必须配置security.access_rules key

你能够通过它的匿名和身份验证的用户,然后用访问规则与单一防火墙做到这一点,限制访问/账户的URI只允许通过认证的用户:

<?php 

$app['security.firewalls'] = array(
'secured' => array(
    'pattern' => '^.*$', 
    'anonymous' => true, 
    'form' => array('login_path' => '/login', 'check_path' => '/account/login_check'), 
    'logout' => array('logout_path' => '/account/logout', 'invalidate_session' => true), 
    'users' => $app->share(function() use ($app) { 
     return new UserProvider($app['db']); 
    }), 
); 
// By using authorization the access to the /account/* is protected to 
// users with the ROLE_USER (you can be more creative here if you want) 
// and with the second rule the whole site is allowed to non authenticated 
// users (remember the /login path must not be protected!) 
$app['security.access_rules'] = array(
    // this could be also array('^/account', 'ROLE_USER') 
    array('^/account', 'IS_AUTHENTICATED_FULLY'), 
    array('^.*$', 'IS_AUTHENTICATED_ANONYMOUSLY') 
); 

更多见Symfony doc有关授权的信息。此外,如果你想知道更多关于无角色访问控制check this out

+0

是的,昨天以相同的方式计算出来。即使寿,感谢您的时间和精力:) – Piteqqq

-1

最简单的方法是在页面头部设置会话。

if(!isset($_SESSION["logged_in"])){ 
    header("Location: http://www.example.com/"); 
} 

这是相当原始的 - 你有没有想过使用MVC框架?会为你节省很多时间。

为什么不创建控制器?

+0

那么,它是原始的,我也可以查看网站的内容之前的角色,但我知道有更多的“专业”和复杂的方式使用安全性。 。谢谢你的回答tho;) – Piteqqq

相关问题