php - What is the proper way to prevent not logged in users to acces specific pages -
php - What is the proper way to prevent not logged in users to acces specific pages -
i have next construction on website:
/login page, when goes website, automatically on page. not needed logged in.
when logs in, on /game/welcome page. there on can acces /game/account , such pages.
now when go straight /game/welcome, without logging in, can acces page. how can prevent this?
this security.yml file:
# can read more security in related section of documentation # http://symfony.com/doc/current/book/security.html security: # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password encoders: login\loginbundle\entity\user: sha512 #algorithm: sha1 #iterations: 1 #encode_as_base64: true #login\loginbundle\entity\user: sha512 # 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: user: entity: class: login\loginbundle\entity\user property: username #in_memory: #memory: #users: #user: { password: userpass, roles: [ 'role_user' ] } #admin: { password: adminpass, roles: [ 'role_admin' ] } # main part of security, can set firewalls # specific sections of app firewalls: secured_area: pattern: ^/ anonymous: ~ form_login: login_path: login check_path: login_check access_control: - { path: ^/login, roles: is_authenticated_anonymously }
typically checking role_user should suffice, though more safe check role is_authenticated_fully, set automatically security component authenticated users if want differentiate anonymous users.
instead of taking path of setting access_control
in security.yml other answers suggest, i'd recommend securing individual controllers instead.
this has advantage of not inadvertently disabling security when changing route url patterns, or making mistakes in regular expressions, see happening lot.
with sensioframeworkextrabundle
can secure controllers annotation:
use sensio\bundle\frameworkextrabundle\configuration\security; utilize symfony\bundle\frameworkbundle\controller\controller; class democontroller extends controller { /** * @security("has_role('is_authenticated_fully')") */ public function indexaction() { // ... } }
if don't annotations, can check in controller code follows (when extending default controller class):
use symfony\bundle\frameworkbundle\controller\controller; class democontroller extends controller { public function indexaction() { if (false === $this->get('security.context')->isgranted('is_authenticated_fully')) { throw $this->createaccessdeniedexception('unable access page!'); } // ... } }
php security symfony2
Comments
Post a Comment