2015-11-13 41 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

相關問題